您现在的位置是:首页 >

javawait加时间 java中的sleep和wait的区别

火烧 2022-08-01 19:04:21 1050
java中的 lee 和wait的区别 java中的 lee 和wait的区别对于 lee 方法,我们首先要知道该方法是属于Thread类中的。而wait 方法,则是属于O ject类中的。 lee

java中的sleep和wait的区别  

java中的sleep和wait的区别

对于sleep()方法,我们首先要知道该方法是属于Thread类中的。而wait()方法,则是属于Object类中的。
sleep()方法导致了程式暂停执行指定的时间,让出cpu该其他执行绪,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复执行状态。
在呼叫sleep()方法的过程中,执行绪不会释放物件锁。
而当呼叫wait()方法的时候,执行绪会放弃物件锁,进入等待此物件的等待锁定池,只有针对此物件呼叫notify()方法后本执行绪才进入物件锁定池准备
获取物件锁进入执行状态。
什么意思呢?
举个列子说明:
1 /**
2 *
3 */
4 package .b510.test;
5
6 /**
7 * java中的sleep()和wait()的区别
8 * @author Hongten
9 * @date 2013-12-10
10 */
11 public class TestD {
12
13 public static void main(String[] args) {
14 new Thread(new Thread1()).start();
15 try {
16 Thread.sleep(5000);
17 } catch (Exception e) {
18 e.printStackTrace();
19 }
20 new Thread(new Thread2()).start();
21 }
22
23 private static class Thread1 implements Runnable{
24 @Override
25 public void run(){
26 synchronized (TestD.class) {
27 System.out.println("enter thread1...");
28 System.out.println("thread1 is waiting...");
29 try {
30 呼叫wait()方法,执行绪会放弃物件锁,进入等待此物件的等待锁定池
31 TestD.class.wait();
32 } catch (Exception e) {
33 e.printStackTrace();
34 }
35 System.out.println("thread1 is going on ....");
36 System.out.println("thread1 is over!!!");
37 }
38 }
39 }
40
41 private static class Thread2 implements Runnable{
42 @Override
43 public void run(){
44 synchronized (TestD.class) {
45 System.out.println("enter thread2....");
46 System.out.println("thread2 is sleep....");
47 只有针对此物件呼叫notify()方法后本执行绪才进入物件锁定池准备获取物件锁进入执行状态。
48 TestD.class.notify();
49 ==================
50 区别
51 如果我们把程式码:TestD.class.notify();给注释掉,即TestD.class呼叫了wait()方法,但是没有呼叫notify()
52 方法,则执行绪永远处于挂起状态。
53 try {
54 sleep()方法导致了程式暂停执行指定的时间,让出cpu该其他执行绪,
55 但是他的监控状态依然保持者,当指定的时间到了又会自动恢复执行状态。
56 在呼叫sleep()方法的过程中,执行绪不会释放物件锁。
57 Thread.sleep(5000);
58 } catch (Exception e) {
59 e.printStackTrace();
60 }
61 System.out.println("thread2 is going on....");
62 System.out.println("thread2 is over!!!");
63 }
64 }
65 }
66 }
执行效果:
enter thread1...
thread1 is waiting...
enter thread2....
thread2 is sleep....
thread2 is going on....
thread2 is over!!!
thread1 is going on ....
thread1 is over!!!
如果注释掉程式码:
1 TestD.class.notify();
执行效果:
enter thread1...
thread1 is waiting...
enter thread2....
thread2 is sleep....
thread2 is going on....
thread2 is over!!!
且程式一直处于挂起状态。

折扣的折的拼音

折扣_词语解释
【拼音】:zhé kòu
【解释】:1.买卖货时,照原标价减去的成数。

冬眠的绸的拼音

冬眠的绸
拼音[dōng] [mián] [dí] [chóu]。

javawait加时间 java中的sleep和wait的区别

渠的形近字的组词

芙蕖

喜讯的讯的反义词

原词: 喜讯( 注释: 1.好讯息;使人高兴的讯息。 )
反义词: 噩耗( 注释: 指亲近的人死亡的讯息。 )

这人男的女的?

男的,但是是gay,我有朋友认识的。

双人的COS的角色

镜音双子,本来我和我姐姐要扮演的呢,我和我姐是双胞胎哦,拍这个的视讯话,恶系列不错的,很好看,网上有真人版的,也是双胞胎来扮演的,女的,蛮好看的呢,试试把

v factory的beautiful girl的歌词

it's just another rainy day
somehow i feel a different way
you caught me by surprise
it's hard for me to believe
that it's ing over me
it's in every word you say
now i'm running to forever
wonder where the story goes
cos only you can make me better
this i know
you are the air that i breathe
you are my soul when i'm in need
you are the one that i see
visions of you in my dreams
won't let you leave, not time to go
wherever i am i will know
i will love you, i will love you
my beautiful girl
every time i look into your eyes
see the wonder of your pretty smile
captures me in every way
wanna hold you til the morning light
keep you safe through the night
whisper all i wanna say
now i'm running to forever
wonder where the story goes
cos only you can make me better
this i know
you are the air that i breathe
you are my soul when i'm in need
you are the one that i see
visions of you in my dreams
won't let you leave, not time to go
wherever i am i will know
i will love you, i will love you
my beautiful girl
now i'm running to forever
wonder where the story goes
cos only you can make me better
this i know
you are the air that i breathe
you are the one that i see
visions of you in my dreams
won't let you leave, not time to go
wherever i am i will know
i will love you, i will love you
my beautiful girl
my beautiful girl..

蚕的问题【蚕结的茧】

老龄蚕宝宝(幼虫):雌蚕第八九节腹面,有四个透明小点,雄蚕第九节腹面中央,有一个透明小点。 蛹:雌蛹的腹部第八九节腹面中央,有一条直线,把第八九节从中分隔开来,而雄蛹的腹部第九节腹面中央,有一对小突起。 蛾:雌蛾的交配孔附近有一片黑色的骨板,而雄蛾的外生殖有成对的抱握器。
蚕个大、粗,屁股圆的为母蚕,公蚕个稍小,屁股细尖。
蛹更好认公母,母大公小

我的故乡的作文

冬天的阳泉换上了朴素的雪白长袍.街道上到处是雪,电线杆上也结了冰.下雪了,雪像柳絮一样,像芦花一样飘落下来,美极了.雪停了,村子的屋顶上,篱笆上,木棚上,到处都覆蓋著一层雪.这真是一幅美丽的雪景.
人们来来往往,脸上洋溢着欣慰的笑容,不时与街坊邻居聊上两句,暖意凝聚成一只只无形的手,抚慰著那些冰封的心,暖了神,暖了情
《原创》

  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码