`

2011-4-24java多线程基础

阅读更多
package multithreading;

import java.io.IOException;

/**
 * thread state can be divided into following 4 states:
 * 1. new  new Thread();
 * 2.runnable t1.start;
 * 3.running execute code;
 * 4.blocked just like join or sleep or something else;
 * 5.dead the run method is completely executed or get some exception
 * tips:
 * this difference between start() and run()
 * start is to start a thread ,this is mean that a new thread join into the cpu
 * run is just a method.
 * @author Master.Roshi
 *
 */

public class ThreadState implements Runnable {
	
	public void run()
	{
		for(int i=0;i<10;i++)
		System.out.println(Thread.currentThread().getName()+":"+i);
		
	}
	public static void main(String[] args) throws IOException
	{
		Thread t1=new Thread(new ThreadState());
		Thread t2=new Thread(new ThreadState());
        t1.start();
		//when a thread meet I/O manipulate,it will be blocked.
		int data=System.in.read();
		//after running t1 is dead.
		System.out.println("t1 is alive?:"+t1.isAlive());
		System.out.println("the main dead.");
	
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics