本文實(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é)果:
可以看到線程的啟動(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