1.信號量Semaphore
先說說Semaphore,Semaphore可以控制某個資源可被同時訪問的個數,通過acquire()獲取一個許可,如果沒有就等待,而release()釋放一個許可。一般用于控制并發線程數,及線程間互斥。另外重入鎖ReentrantLock也可以實現該功能,但實現上要復雜些。
功能就類似廁所有5個坑,假如有10個人要上廁所,那么同時只能有多少個人去上廁所呢?同時只能有5個人能夠占用,當5個人中的任何一個人讓開后,其中等待的另外5個人中又有一個人可以占用了。另外等待的5個人中可以是隨機獲得優先機會,也可以是按照先來后到的順序獲得機會。
單個信號量的Semaphore對象可以實現互斥鎖的功能,并且可以是由一個線程獲得了“鎖”,再由另一個線程釋放“鎖”,這可應用于死鎖恢復的一些場合。
例子:
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
|
/** * @Description: * @param @param args * @return void 返回類型 */ public static void main(String[] args) { // 線程池 ExecutorService exec = Executors.newCachedThreadPool(); // 只能5個線程同時訪問 final Semaphore semp = new Semaphore( 5 ); // 模擬20個客戶端訪問 for ( int index = 0 ; index < 20 ; index++) { final int NO = index; Runnable run = new Runnable() { public void run() { try { // 獲取許可 semp.acquire(); System.out.println( "獲得Accessing: " + NO); Thread.sleep(( long ) (Math.random() * 10000 )); // 訪問完后,釋放 semp.release(); System.out.println( "剩余可用信號-----------------" + semp.availablePermits()); } catch (InterruptedException e) { e.printStackTrace(); } } }; exec.execute(run); } // 退出線程池 exec.shutdown(); } |
輸出結果(可以想想為什么會這樣輸出):
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
|
獲得Accessing: 1 獲得Accessing: 5 獲得Accessing: 2 獲得Accessing: 3 獲得Accessing: 0 剩余可用信號----------------- 1 獲得Accessing: 4 剩余可用信號----------------- 1 獲得Accessing: 9 剩余可用信號----------------- 1 獲得Accessing: 8 剩余可用信號----------------- 1 獲得Accessing: 6 剩余可用信號----------------- 1 獲得Accessing: 10 剩余可用信號----------------- 1 獲得Accessing: 11 剩余可用信號----------------- 1 獲得Accessing: 12 剩余可用信號----------------- 1 獲得Accessing: 13 剩余可用信號----------------- 1 獲得Accessing: 7 剩余可用信號----------------- 1 獲得Accessing: 15 剩余可用信號----------------- 1 獲得Accessing: 16 剩余可用信號----------------- 1 獲得Accessing: 17 剩余可用信號----------------- 1 獲得Accessing: 14 剩余可用信號----------------- 1 獲得Accessing: 18 剩余可用信號----------------- 1 獲得Accessing: 19 剩余可用信號----------------- 1 剩余可用信號----------------- 2 剩余可用信號----------------- 3 剩余可用信號----------------- 4 剩余可用信號----------------- 5 |
2.使用PIPE作為線程間通信橋梁
Pipe有一個source通道和一個sink通道。數據會被寫到sink通道,從source通道讀取。一進一出。先作為初步了解怎么使用。
值得注意的是該類在java.nio.channels下,說明該類屬于nio方式的數據通信方式,那就使用Buffer來緩沖數據。
Pipe原理的圖示:
Pipe就是個空管子,這個空管子一頭可以從管子里往外讀,一頭可以往管子里寫
操作流程:
1.首先要有一個對象往這個空管子里面寫。寫到哪里呢?這個空管子是有一點空間的,就在這個管子里。
寫的時候就是寫到管子本身包含的這段空間里的。這段空間大小是1024個字節。
2.然后另一個對象才能將這個裝滿了的管子里的內容讀出來。
上代碼
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
|
package com.jx.test; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Pipe; public class testPipe { /** * @Description: * @param @param args * @return void 返回類型 * @throws IOException */ public static void main(String[] args) throws IOException { // 創建一個管道 Pipe pipe = Pipe.open(); final Pipe.SinkChannel psic = pipe.sink(); // 要向管道寫數據,需要訪問sink通道 final Pipe.SourceChannel psoc = pipe.source(); // 從讀取管道的數據,需要訪問source通道 Thread tPwriter = new Thread() { public void run() { try { System.out.println( "send....." ); // 創建一個線程,利用管道的寫入口Pipe.SinkChannel類型的psic往管道里寫入指定ByteBuffer的內容 int res = psic.write(ByteBuffer .wrap( "Hello,Pipe!測試通訊....." .getBytes( "utf-16BE" ))); System.out.println( "send size:" + res); } catch (Exception e) { e.printStackTrace(); } } } ; Thread tPreader = new Thread() { public void run() { int bbufferSize = 1024 * 2 ; ByteBuffer bbuffer = ByteBuffer.allocate(bbufferSize); try { System.out.println( "recive....." ); // 創建一個線程,利用管道的讀入口Pipe.SourceChannel類型的psoc將管道里內容讀到指定的ByteBuffer中 int res = psoc.read(bbuffer); //數據未 System.out.println( "recive size:" +res+ " Content:" + ByteBufferToString(bbuffer)); } catch (Exception e) { e.printStackTrace(); } } } ; tPwriter.start(); tPreader.start(); } /** *ByteBuffer--> String的轉換函數 */ public static String ByteBufferToString(ByteBuffer content) { if (content == null || content.limit() <= 0 || (content.limit() == content.remaining())) { System.out.println( "不存在或內容為空!" ); return null ; } int contentSize = content.limit() - content.remaining(); StringBuffer resultStr = new StringBuffer(); for ( int i = 0 ; i < contentSize; i += 2 ) { resultStr.append(content.getchar(i)); } return resultStr.toString(); } } |
總結
以上就是本文關于Java編程線程間通信與信號量代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:https://segmentfault.com/a/1190000012297609