多线程的线程状态转换
常见的函数
① sleep():使线程静止,不会释放对象锁.
② wait():使线程释放对象锁,需与 synchronized() ,notify().
③ notify():唤醒处于 同步块 wait() 的一段线程使其运行.
①②③Demo: ↓
public class WaitThread {
public WaitThread()
{
// TODO Auto-generated constructor stub
}
public static void main(String[] args)
{
ThreadB b = new WaitThread().new ThreadB();
b.start();
System.out.println("b is start....");
synchronized (b) // 括号里的b是什么意思,起什么作用?
{
try
{
System.out.println("Waiting for b to complete...");
System.out.println("Completed.Now back to main thread");
b.wait(); // 这一句是什么意思,究竟让谁wait?
}
catch (InterruptedException e)
{
}
}
System.out.println("Total is :" + b.total);
}
class ThreadB extends Thread
{
int total;
public void run()
{