Java实现指定线程执行顺序的三种方式示例

2025-05-29 0 94

本文实例讲述了java实现指定线程执行顺序的三种方式。分享给大家供大家参考,具体如下:

方法一:通过共享对象锁加上可见变量来实现。

?

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();

}

}

}

执行结果:

Java实现指定线程执行顺序的三种方式示例

可以看到线程的启动按顺序执行了。共享对象锁,可以保证每个方法只能同时有一个线程进入,配合wait和notifyall方法,可以启动或者唤醒线程

方法二:通过主线程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();

}

}

方法三:通过线程执行时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程序设计有所帮助。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现指定线程执行顺序的三种方式示例 https://www.kuaiidc.com/109709.html

相关文章

发表评论
暂无评论