java thread

package com.yuan.test;

public class LiftOff implements Runnable {
	 protected int countDown = 10; // Default
	 private static int taskCount = 0;
	 private final int id = taskCount++;
	 public LiftOff() {}
	 public LiftOff(int countDown) {
	 this.countDown = countDown;
	 }
	 public String status() {
	 return "#" + id + "(" +
	 (countDown > 0 ? countDown : "Liftoff!") + "), ";
	 }
	 public void run() {
	 while(countDown-- > 0) {
	 System.out.print(status());
	 Thread.yield();
	 }
	 }
	 public static void main(String[] args){
		 LiftOff lo=new LiftOff();
		 lo.status();
		 lo.run();
		 LiftOff lo1=new LiftOff();
		 lo1.status();
		 lo1.run();
		 
	 }
	} ///:~

The identifier id distinguishes between multiple instances of the task. It is final because it is
not expected to change once it is initialized.

上述ID 是用来区分线程的个数的,而且因为它是final 类型的所以一旦实例化了就不会被修改了。
A task’s run( ) method usually has some kind of loop that continues until the task is no
longer necessary, so you must establish the condition on which to break out of this loop (one
option is to simply return from run( )).

Often, run( ) is cast in the form of an infinite loop,which means that, barring some factor that causes run( ) to terminate, it will continue forever (later in the chapter you’ll see how to safely terminate tasks).

The call to the static method Thread.yield( ) inside run( ) is a suggestion to the thread
scheduler (the part of the Java threading mechanism that moves the CPU from one thread to
the next) that says,

Thread.yields()是一个静态的方法,用来给CPU提一下建议说,“您已经帮我完成最重要的工作了,你要是忙,你就先走吧”

"I’ve done the important parts of my cycle and this would be a good time
to switch to another task for a while."

It’s completely optional, but it is used here because it tends to produce more interesting output in these examples: You’re more likely to see evidence of tasks being swapped in and out.

public class TestThread {

	public static void main(String[] args) {
		 Thread t = new Thread(new LiftOff());
		 t.start();
		 System.out.println("Waiting for LiftOff1");
		 Thread t1 = new Thread(new LiftOff());
		 t1.start();
		 System.out.println("Waiting for LiftOff2");
	}

}

output:

Waiting for LiftOff1

Waiting for LiftOff2

#1(9), #0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1), #0(Liftoff!), #1(8), #1(7), #1(6), #1(5), #1(4), #1(3), #1(2), #1(1), #1(Liftoff!),

When main( ) creates the Thread objects, it isn’t capturing the references for any of them.
With an ordinary object, this would make it fair game for garbage collection, but not with a
Thread. Each Thread "registers" itself so there is actually a reference to it someplace, and
the garbage collector can’t clean it up until the task exits its run( ) and dies. You can see
from the output that the tasks are indeed running to conclusion, so a thread creates a
separate thread of execution that persists after the call to start( ) completes.

时间: 2024-08-25 06:53:09

java thread的相关文章

java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)

 *java多线程--等待唤醒机制:经典的体现"生产者和消费者模型 *对于此模型,应该明确以下几点: *1.生产者仅仅在仓库未满的时候生产,仓库满了则停止生产. *2.消费者仅仅在有产品的时候才能消费,仓空则等待. *3.当消费者发现仓储没有产品可消费的时候,会唤醒等待生产者生产. *4.生产者在生产出可以消费的产品的时候,应该通知等待的消费者去消费. 下面先介绍个简单的生产者消费者例子:本例只适用于两个线程,一个线程生产,一个线程负责消费. 生产一个资源,就得消费一个资源. 代码如下: pub

java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)

1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以可以被任意对象调用的方法,定义在Object基类中. wait()方法:对此对象调用wait方法导致本线程放弃对象锁,让线程处于冻结状态,进入等待线程的线程池当中.wait是指已经进入同步锁的线程,让自己暂时让出同步锁,以便使其他正在等待此锁的线程可以进入同步锁并运行,只有其它线程调用notify方

java基础知识回顾之java Thread类学习(六)--java多线程同步函数用的锁

1.验证同步函数使用的锁----普通方法使用的锁 思路:创建两个线程,同时操作同一个资源,还是用卖票的例子来验证.创建好两个线程t1,t2,t1线程走同步代码块操作tickets,t2,线程走同步函数封装的代码操作tickets,同步代码块中的锁我们可以指定.假设我们事先不知道同步函数用的是什么锁:如果在同步代码块中指定的某个锁(测试)和同步函数用的锁相同,就不会出现线程安全问题,如果锁不相同,就会发生线程安全问题. 看下面的代码:t1线程用的同步锁是obj,t2线程在操作同步函数的资源,假设不

java基础知识回顾之java Thread类学习(五)--java多线程安全问题(锁)同步的前提

这里举个例子讲解,同步synchronized在什么地方加,以及同步的前提: * 1.必须要有两个以上的线程,才需要同步. * 2.必须是多个线程使用同一个锁. * 3.必须保证同步中只能有一个线程在运行,锁加在哪一块代码 那么我们要思考的地方有:1.知道我们写的哪些是多线程代码 2.明确共享数据 3.明确多线程运行的代码中哪些语句是操作共享数据的.. 4.要确保使用同一个锁. 下面的代码:需求:两个存户分别往银行存钱,每次村100块,分三次存完. class bank{ private int

Java Thread.interrupt方法

部分内容引用CSDNdr8737010 比如你在main线程中,开启了一个新的线程new Thread 首先,每个线程内部都有一个boolean型变量表示线程的中断状态,true代表线程处于中断状态,false表示未处于中断状态. 而interrupt()方法的作用只是用来改变线程的中断状态(把线程的中断状态改为true,即被中断). A线程调用wait,sleep,join方法,这时B线程调用了A的interrupt方法而抛出的InterruptedException是wait,sleep,j

性能分析之-- JAVA Thread Dump 分析综述

性能分析之-- JAVA Thread Dump 分析综述 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工具.每一个Java虚拟机都有及时生成所有线程在某一点状态的thread-dump的能力,虽然各个 Java虚拟机打印的thread dump略有不同,但是大多都提供了当前活动线程的快照,及JVM中所有Java线程的堆栈跟踪信息,堆栈信息一般包含完整的类名及所执行的方法,如果可能的话还有源代码的行数. 1.2 T

Java thread中对异常的处理策略

转载:http://shmilyaw-hotmail-com.iteye.com/blog/1881302 前言 想讨论这个话题有一段时间了.记得几年前的时候去面试,有人就问过我一个类似的问题.就是java thread中对于异常的处理情况.由于java thread本身牵涉到并发.锁等相关的问题已经够复杂了.再加上异常处理这些东西,使得它更加特殊. 概括起来,不外乎是三个主要的问题.1. 在java启动的线程里可以抛出异常吗? 2. 在启动的线程里可以捕捉异常吗? 3. 如果可以捕捉异常,对于

三个实例演示 Java Thread Dump 日志分析

jstack Dump 日志文件中的线程状态 dump 文件里,值得关注的线程状态有: 死锁,Deadlock(重点关注)  执行中,Runnable 等待资源,Waiting on condition(重点关注) 等待获取监视器,Waiting on monitor entry(重点关注) 暂停,Suspended 对象等待中,Object.wait() 或 TIMED_WAITING 阻塞,Blocked(重点关注)   停止,Parked 下面我们先从第一个例子开始分析,然后再列出不同线程

Java Thread 相关的函数

构造方法摘要 Thread()          分配新的 Thread 对象. Thread(Runnable target)          分配新的 Thread 对象. Thread(Runnable target, String name)          分配新的 Thread 对象. Thread(String name)          分配新的 Thread 对象. Thread(ThreadGroup group, Runnable target)          分

java基础知识回顾之java Thread类学习(四)--java多线程安全问题(锁)

上一节售票系统中我们发现,打印出了错票,0,-1,出现了多线程安全问题.我们分析为什么会发生多线程安全问题? 看下面线程的主要代码: @Override public void run() { // TODO Auto-generated method stub while(true){ if(ticket > 0){//当线程0被调起的时候,当执行到这条判断语句的时候,线程1被调起抢了CPU资源,线程0进入冻结状态. try { Thread.sleep(100);//中断当前活跃的线程,或者