2011-4-24java多线程基础

本文深入探讨了Java多线程状态的四个阶段:新建、可运行、运行和阻塞,并通过实例展示了线程从新建到死亡的过程,包括线程间交互及线程阻塞的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.");

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值