好文章转载!
sleep:Thread的静态方法,当前线程休眠一段时间,时间到了再恢复可运行状态,时间到了不一定就执行吧,还得竞争CPU呢.
join:这个方法其实就是特殊的wait,wait方法一般都需要别人notify(当然也可以设置超时),但是join方法就不需要别人notify了,一直等到这个线程死亡(就相当于这个线程临时前告诉那些在等它的人:你们进来吧!)
本人不是很会举例子,还是两个人公用一个卫生间吧,这回不刷牙了,改洗澡吧,总不能两个人同时洗澡吧!就算可以,这里假设不可以吧.情况时这样的:A在洗澡,B要等。
第一种情况:
B很聪明的,A洗澡可能要20分钟到1小时,我就先睡10分钟看看好了没有,没有好就再睡10分钟,最多多等10分钟而已吧.
class Syn
{
public static void main(String[] args) throws Exception
{
Thread a=new Bathing();
a.start();
//B
int time=0;
while(a.isAlive()){
Thread.sleep(10000);
time+=10;
System.out.println("B has waited "+time+" minutes");
}
System.out.println("B can bath now!");
}
}
class Bathing extends Thread
{
public void run(){
bathing();
}
private void bathing() {
System.out.println("A is bathing !");
try{Thread.sleep(20000);}catch(InterruptedException e){e.printStackTrace();}
//延迟20秒看效果
System.out.println("A has bathed !");
}
};
这里连同步都不需要,不过B可能需要多等一段时间,因为它可能刚好去敲门A还没有洗完,于是他又去睡,结果刚睡下A就洗完了.
第二种情况:
B变得更加聪明了,这样等我不是亏了,如果我10分钟敲一次门,我可能要多等10分钟,但是如果我每秒敲一次我也没法睡呀,于是想了一个高招,装了一个机关,当A出来的时候机关就会按响门铃,这样B就可以高枕无忧了。
class Syn
{
public static void main(String[] args) throws Exception
{
Thread a=new Bathing();
a.start();
//B
int time=0;
a.join();
System.out.println("B can bath now!");
}
}
class Bathing extends Thread
{
public void run(){
bathing();
}
private void bathing() {
System.out.println("A is bathing !");
try{Thread.sleep(20000);}catch(InterruptedException e){e.printStackTrace();}
//延迟20秒看效果
System.out.println("A has bathed !");
}
};
这样只要A一洗完,B就会被唤醒,这里A并没有去notify他,但是还是间接的通知了B,当然这里也可以用wati和notify实现,不过就显得不好了。
class Syn
{
public static void main(String[] args) throws Exception
{
Thread a=new Bathing();
a.start();
//B
int time=0;
synchronized(a){a.wait();}
System.out.println("B can bath now!");
}
}
class Bathing extends Thread
{
public void run(){
synchronized(this){
bathing();
notify();
}
}
private void bathing() {
System.out.println("A is bathing !");
try{Thread.sleep(20000);}catch(InterruptedException e){e.printStackTrace();}
//延迟20秒看效果
System.out.println("A has bathed !");
}
};
对于一般的对象就要用wait和notify了,但是对于一个线程来说,join方法有时候更加方便。
分享到:
相关推荐
1.这两个方法来自不同的类,sleep是Thread类的方法,而wait是Object类的方法; 2.执行sleep方法后不会释放锁,而执行wait方法后会释放锁; 3.wait,notify和notifyAll只能在同步方法或同步代码块中调用,而sleep可以...
25 3:ServicorTo 和 ServicorFrom 互换................................................................................................................25 2.3.3.1. 2.4.1. 如何确定垃圾 ......................
25 3:ServicorTo 和 ServicorFrom 互换................................................................................................................25 2.3.3.1. 2.4.1. 如何确定垃圾 ......................
非流类(正确答案) java中用于创建文件对象的类是() [单选题] * A.File(正确答案) B.Object C.Thread D.Frame 若要将一个类对象能被整体写入文件,则定义该类时必须实现下列哪个接口() [单选题] * A....
10.4.3 catch和throw同时使用 371 10.4.4 异常链 373 10.5 Java的异常跟踪栈 374 10.6 异常处理规则 376 10.6.1 不要过度使用异常 377 10.6.2 不要使用过于庞大的try块 378 10.6.3 避免使用Catch All语句 378 ...
Waiting:具有指定等待时间的等待线程的线程状态(sleep、wait、join、parkNanos、parkUntil) 6.Terminated:终止线程的线程状态。线程正常执行完成或出现异常 代码:Demo01 线程切换状态图 线程终止 不正确的线程...
线程状态和sleep/yield/join/stop/destroy方法 170 新生状态 170 就绪状态 170 运行状态 170 死亡状态 170 终止线程的典型方法(重要!!!) 171 阻塞状态(sleep/yield/join方法) 171 线程基本信息和优先级别 173 ...
LeetCode判断字符串是否循环 知识点总结 java基础 1、使用迭代器和for each循环查看集合元素时只能获得元素的值,不能改变元素 ...object的方法:wait(notify,notifyall),thread的方法:(sleep、join),结束
Index (4) Data 1: Object ID; Data 2: sub-resource; Data 3: Index ID Table (5) Data 1: Object ID; Data 2: sub-resource; Data 3: 0. Page (6) Data 1: Page Number; Data 3: 0. Key (7) Data 1: Object ID; ...
线程操作:isAlive()、isInterrupted()、join()、sleep()、stop()、interrupted()、setDaemon()、setPriority()、yield() 同步与死锁、Object类对线程的支持 泛型、通配符、受限泛型、泛型接口、泛型方法、泛型数字...
《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...