自從java1.5以后,官網(wǎng)就推出了executor這樣一個類,這個類,可以維護我們的大量線程在操作臨界資源時的穩(wěn)定性。
先上一段代碼吧:
testrunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class testrunnable implements runnable { private string name; public testrunnable(string name) { this .name = name; } @override public void run() { while ( true ) { if (main.surplus < 0 ) return ; main.surplus--; system.out.println(name + " " + main.surplus); } } } |
main入口
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(string[] args) { testrunnable runnable = new testrunnable( "runnable1" ); testrunnable runnable2 = new testrunnable( "runnable2" ); thread t1 = new thread(runnable); thread t2 = new thread(runnable2); t1.start(); t2.start(); } |
這樣,我們就看到了,數(shù)據(jù)肯定是亂了的,當(dāng)然這個時候我們可以加上一個synchronized的關(guān)鍵字,但是這樣也會出現(xiàn)點小問題的
下面我打算采用一種java內(nèi)置的線程管理的機制,來解決這個問題,解決這個問題的思路大概就是,我們維護了一個線程池,當(dāng)有請求操作的時候統(tǒng)統(tǒng)進入線程池,并且我們只開了一個線程,可以讓請求順序執(zhí)行,順序調(diào)用臨界資源,就很安全了。
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import java.util.concurrent.callable; import java.util.concurrent.executionexception; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.future; public class main { public static int surplus = 10 ; private executorservice executor = executors.newsinglethreadexecutor(); void addtask(runnable runnable) { executor.execute(runnable); } <v> v addtask(callable<v> callable) { future<v> submit = executor.submit(callable); try { return submit.get(); } catch (interruptedexception e) { system.out.println( "interruptedexception" + e.tostring()); } catch (executionexception e) { system.out.println( "executionexception" + e.tostring()); } return null ; } public void testaddtask(string name) { addtask( new runnable() { @override public void run() { for ( int i = 0 ; i < 3 ; i++) { if (main.surplus <= 0 ) return ; main.surplus--; system.out.println(name + " " + main.surplus); } } }); } public void testaddtask2(string name) { int count = addtask( new callable<integer>() { @override public integer call() throws exception { for ( int i = 0 ; i < 3 ; i++) { if (main.surplus <= 0 ) return 0 ; main.surplus--; system.out.println(name + " " + main.surplus); } return main.surplus; } }); } public void close() { executor.shutdown(); } public static void main(string[] args) { main main = new main(); main.testaddtask( "task1" ); main.testaddtask2( "task2" ); main.testaddtask( "task3" ); main.testaddtask2( "task4" ); main.close(); } } |
在這里,我們定義了兩種方法,分別是addtask,具有泛型的addtask,這兩種方法實現(xiàn)原理都是一樣的,其中一個是有回調(diào)的,一個是沒有回調(diào)的,就看項目需求了吧。
然后分別調(diào)用這兩個方法咯,就可以看到結(jié)果是非常有序,且不會混亂的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.jianshu.com/p/ccdb616723ab