wait 和 sleep 区别?
1,wait可以指定时间也可以不指定。
sleep必须指定时间。
2,在同步中时,对cpu的执行权和锁的处理不同。
wait:释放执行权,释放锁。
join:
1,wait可以指定时间也可以不指定。
sleep必须指定时间。
2,在同步中时,对cpu的执行权和锁的处理不同。
wait:释放执行权,释放锁。
sleep:释放执行权,不释放锁。
class Demo
{
void show()
{
synchronized(this)//
{
wait();//t0 t1 t2
}
}
void method()
{
synchronized(this)//t4
{
//wait();
notifyAll();
}//t4
}
}
class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
join:
class Demo implements Runnable
{
public void run()
{
for(int x=0; x<50; x++)
{
System.out.println(Thread.currentThread().toString()+"....."+x);
Thread.yield();
}
}
}
class JoinDemo
{
public static void main(String[] args) throws Exception
{
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
t1.start();
t2.start();
// t2.setPriority(Thread.MAX_PRIORITY);
// t1.join();//t1线程要申请加入进来,运行。临时加入一个线程运算时可以使用join方法。
for(int x=0; x<50; x++)
{
// System.out.println(Thread.currentThread()+"....."+x);
}
}
}