激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 代碼分析Java中線(xiàn)程的等待與喚醒

代碼分析Java中線(xiàn)程的等待與喚醒

2021-06-02 15:46Java教程網(wǎng) Java教程

本篇文章給大家分享了關(guān)于Java中線(xiàn)程的等待與喚醒的知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。

我們先來(lái)看一下實(shí)例代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class threada extends thread{
 
  public threada(string name) {
    super(name);
  }
 
  public void run() {
    synchronized (this) {
      system.out.println(thread.currentthread().getname()+" call notify()");
      notify();
    }
  }
}
public class waittest {
  public static void main(string[] args) {
    threada t1 = new threada("t1");
    synchronized(t1) {
      try {
        // 啟動(dòng)“線(xiàn)程t1”
        system.out.println(thread.currentthread().getname()+" start t1");
        t1.start();
 
        // 主線(xiàn)程等待t1通過(guò)notify()喚醒。
        system.out.println(thread.currentthread().getname()+" wait()");
        t1.wait();
 
        system.out.println(thread.currentthread().getname()+" continue");
      } catch (interruptedexception e) {
        e.printstacktrace();
      }
    }
  }
}

輸出結(jié)果:main start t1 -> main wait() -> t1 call notify() -> main continue

其實(shí)調(diào)用t1.start(),t1為就緒狀態(tài),只是main方法中,t1被main線(xiàn)程鎖住了,t1.wait()的時(shí)候,讓當(dāng)前線(xiàn)程等待,其實(shí)是讓main線(xiàn)程等待了,然后釋放了t1鎖,t1線(xiàn)程執(zhí)行,打印t1 call notify(),然后喚醒main線(xiàn)程,最后結(jié)束;

這里說(shuō)一下wait()與sleep()的區(qū)別,他們的共同點(diǎn)都是讓線(xiàn)程休眠,但是wait()會(huì)釋放對(duì)象同步鎖,而sleep()不會(huì);下面的代碼t1結(jié)束之后才會(huì)運(yùn)行t2;能夠證實(shí)這一點(diǎn);

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class sleeplocktest{
  private static object obj = new object();
  public static void main(string[] args){
    threada t1 = new threada("t1");
    threada t2 = new threada("t2");
    t1.start();
    t2.start();
  }
  static class threada extends thread{
    public threada(string name){
      super(name);
    }
    public void run(){
      synchronized (obj) {
        try {
          for(int i=0; i <10; i++){
            system.out.printf("%s: %d\n", this.getname(), i);
            // i能被4整除時(shí),休眠100毫秒
            if (i%4 == 0)
              thread.sleep(100);
          }
        } catch (interruptedexception e) {
          e.printstacktrace();
        }
      }
    }
  }
}

 

延伸 · 閱讀

精彩推薦
908
主站蜘蛛池模板: 国产一区二区三区影视 | 日本视频在线免费观看 | 成人在线免费观看小视频 | 亚洲一区国产二区 | 男女污污视频网站 | www国产成人免费观看视频 | 91av视频大全| 亚洲情视频 | 美女久久久久 | 女教师~淫辱の动漫在线 | 久久久人成影片一区二区三区 | 欧美一区二区精品夜夜嗨 | 19禁国产精品福利视频 | www日韩在线观看 | 中文在线日韩 | 欧美日韩电影 | 在线日韩亚洲 | 九九久久视频 | 日韩视频在线一区二区三区 | 中文日韩| 99久久精品免费看国产四区 | 亚洲第五色综合网 | 亚洲成人免费视频在线 | 国产精品午夜未成人免费观看 | 精品亚洲视频在线观看 | 欧美性生活久久 | 亚洲乱妇19p | 国产午夜亚洲精品理论片大丰影院 | 国产成人精品区 | 长泽雅美av| 国产69精品99久久久久久宅男 | 国产精品自拍av | 九一免费版在线观看 | 国产精品啪一品二区三区粉嫩 | 视频一区免费观看 | 色视频在线播放 | 日本aaaa片毛片免费观蜜桃 | 精品国产一区二区三区蜜殿 | 国产91九色在线播放 | 这里精品 | 国产成人精品区一区二区不卡 |