一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例

Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例

2021-07-10 14:30Leon-Zheng Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式,包括通過共享對象鎖加上可見變量,通過主線程Join()以及通過線程執(zhí)行時(shí)Join()等三種實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例講述了java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式。分享給大家供大家參考,具體如下:

方法一:通過共享對象鎖加上可見變量來實(shí)現(xià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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class myservice {
  private volatile int ordernum = 1;
  public synchronized void methoda() {
    try {
      while (ordernum != 1) {
        wait();
      }
      for (int i = 0; i < 2; i++) {
        system.out.println("aaaaa");
      }
      ordernum = 2;
      notifyall();
    } catch (interruptedexception e) {
      e.printstacktrace();
    }
  }
  public synchronized void methodb() {
    try {
      while (ordernum != 2) {
        wait();
      }
      for (int i = 0; i < 2; i++) {
        system.out.println("bbbbb");
      }
      ordernum = 3;
      notifyall();
    } catch (interruptedexception e) {
      e.printstacktrace();
    }
  }
  public synchronized void methodc() {
    try {
      while (ordernum != 3) {
        wait();
      }
      for (int i = 0; i < 2; i++) {
        system.out.println("ccccc");
      }
      ordernum = 1;
      notifyall();
    } catch (interruptedexception e) {
      e.printstacktrace();
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
import service.myservice;
public class threadaa extends thread {
  private myservice dbtools;
  public threadaa(myservice dbtools) {
    super();
    this.dbtools = dbtools;
  }
  @override
  public void run() {
    dbtools.methoda();
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
import service.myservice;
public class threadbb extends thread {
  private myservice dbtools;
  public threadbb(myservice dbtools) {
    super();
    this.dbtools = dbtools;
  }
  @override
  public void run() {
    dbtools.methodb();
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
import service.myservice;
public class threadcc extends thread {
  private myservice dbtools;
  public threadcc(myservice dbtools) {
    this.dbtools = dbtools;
  }
  @override
  public void run() {
    dbtools.methodc();
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import extthread.threadcc;
import service.myservice;
import extthread.threadaa;
import extthread.threadbb;
public class run {
  public static void main(string[] args) {
    myservice myservice = new myservice();
    for (int i = 0; i < 2; i++) {
      threadbb output = new threadbb(myservice);
      output.start();
      threadaa input = new threadaa(myservice);
      input.start();
      threadcc threadcc = new threadcc(myservice);
      threadcc.start();
    }
  }
}

執(zhí)行結(jié)果:

Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例

可以看到線程的啟動(dòng)按順序執(zhí)行了。共享對象鎖,可以保證每個(gè)方法只能同時(shí)有一個(gè)線程進(jìn)入,配合wait和notifyall方法,可以啟動(dòng)或者喚醒線程。

方法二:通過主線程join()

?
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
class t11 extends thread {
  public void run() {
    system.out.println("in t1");
  }
}
class t22 extends thread {
  public void run() {
    system.out.println("in t2");
  }
}
class t33 extends thread {
  public void run() {
    system.out.println("in t3");
  }
}
public class test2 {
  public static void main(string[] args) throws interruptedexception {
    t11 t1 = new t11();
    t22 t2 = new t22();
    t33 t3 = new t33();
    t1.start();
    t1.join();
    t2.start();
    t2.join();
    t3.start();
  }
}

方法三:通過線程執(zhí)行時(shí)join()

?
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class t1 extends thread {
  public void run(){
    random random = new random();
    try {
      thread.sleep(random.nextint(1000));
    } catch (interruptedexception e) {
      e.printstacktrace();
    }
    system.out.println("in t1");
  }
}
class t2 extends thread{
  private thread thread;
  public t2(thread thread) {
    this.thread = thread;
  }
  public void run(){
    try {
      thread.join();
    } catch (interruptedexception e) {
      e.printstacktrace();
    }
    system.out.println("in t2");
  }
}
class t3 extends thread{
  private thread thread;
  public t3(thread thread) {
    this.thread = thread;
  }
  public void run(){
    try {
      thread.join();
    } catch (interruptedexception e) {
      e.printstacktrace();
    }
    system.out.println("in t3");
  }
}
public class test {
  public static void main(string[] args) throws interruptedexception {
    t1 t1 = new t1();
    t2 t2 = new t2(t1);
    t3 t3 = new t3(t2);
    t2.start();
    t1.start();
    t3.start();
  }
}

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

原文鏈接:https://blog.csdn.net/difffate/article/details/63684290

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人aa | 国产精品每日在线观看男人的天堂 | 色播开心网 | 美女撒尿无遮挡免费中国 | 成人精品第一区二区三区 | 桃乃木香奈作品在线 | 久久久免费热线精品频 | 四虎最新紧急更新地址 | 国产欧美视频高清va在线观看 | 性色xxx | 91次元成年破解版 | 人人爱天天做夜夜爽88 | 五月天精品视频播放在线观看 | 亚洲国产精久久久久久久 | 国产精品2 | 日本深夜视频 | 色哟哟国产成人精品 | bedfriend泰剧全集免费观看 | 亚洲天堂免费观看 | 国产亚洲sss在线观看 | 国产成人高清精品免费5388密 | 天天色色色 | 性欧美videofree中文字幕 | 午夜精品久久久久久久99蜜桃i | 亚洲一成人毛片 | 奇米影视久久 | 欧美一级在线视频 | 高h巨肉play 高h短篇辣肉各种姿势bl | 大团圆免费阅读全文 | 婷婷在线网站 | crdy在线看亚洲 | 精品在线免费观看 | 初尝黑人巨大h文 | 国产成人yy精品1024在线 | 国产经典一区 | 波多野结衣中文字幕乱七八糟 | 亚欧视频在线观看 | 色综合合久久天天综合绕视看 | 97自拍视频在线观看 | 成年人免费在线看的惊悚动作片 | 外国xxx |