使用Lock,wait/notify,Semaphore三種方式實現多執行緒通訊
阿新 • • 發佈:2019-02-17
java的多執行緒通訊有Lock,wait/notify,Semaphore三種方式,以一道常見面試題來簡單演示這三種多執行緒通訊方式。
兩個執行緒迴圈間隔列印指定內容,一個列印從1到52的數字,一個列印從A到Z的字母,列印輸出如下:
1
2
A
3
4
B
......
51
52
Z
使用Lock實現程式碼如下:
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- publicclass ThreadCommunicationTest {
- privatefinal Lock lock = new ReentrantLock();
- privatefinal Condition conditionA = lock.newCondition();
-
privatefinal Condition conditionB = lock.newCondition();
- privatestaticchar currentThread = 'A';
- publicstaticvoid main(String[] args) {
- ThreadCommunicationTest test = new ThreadCommunicationTest();
- ExecutorService service = Executors.newCachedThreadPool();
- service.execute(test.new RunnableA());
-
service.execute(test.new
- service.shutdown();
- }
- privateclass RunnableA implements Runnable {
- publicvoid run() {
- for (int i = 1; i <= 52; i++) {
- lock.lock();
- try {
- while (currentThread != 'A') {
- try {
- conditionA.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(i);
- if (i % 2 == 0) {
- currentThread = 'B';
- conditionB.signal();
- }
- } finally {
- lock.unlock();
- }
- }
- }
- }
- privateclass RunnableB implements Runnable {
- publicvoid run() {
- for (char c = 'A'; c <= 'Z'; c++) {
- lock.lock();
- try {
- while (currentThread != 'B') {
- try {
- conditionB.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(c);
- currentThread = 'A';
- conditionA.signal();
- } finally {
- lock.unlock();
- }
- }
- }
- }
- }
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- publicclass ThreadCommunicationTest2 {
- privatestaticchar currentThread = 'A';
- privatefinal Object t = new Object(); //使用一個同步物件保證兩個執行緒之間每一時刻只有一個執行緒工作
- publicstaticvoid main(String[] args) {
- ThreadCommunicationTest2 test = new ThreadCommunicationTest2();
- ExecutorService service = Executors.newCachedThreadPool();
- service.execute(test.new RunnableA());
- service.execute(test.new RunnableB());
- service.shutdown();
- }
- privateclass RunnableA implements Runnable {
- publicvoid run() {
- for (int i = 1; i <= 52; i++) {
- synchronized (t) {
- if(currentThread != 'A'){
- try {
- t.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(i);
- if (i % 2 == 0) {
- currentThread = 'B';
- t.notifyAll();
- }
- }
- }
- }
- }
- privateclass RunnableB implements Runnable {
- publicvoid run() {
- for (char c = 'A'; c <= 'Z'; c++) {
- synchronized (t) {
- if(currentThread != 'B'){
- try {
- t.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(c);
- currentThread = 'A';
- t.notifyAll();
- }
- }
- }
- }
- }
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Semaphore;
- publicclass ThreadCommunicationTest3 {
- privatefinal Semaphore semap = new Semaphore(1);//建立一個只有1個許可的訊號量,保證兩個執行緒間每一時刻只有一個在工作
- privatestaticchar currentThread = 'A';
- publicstaticvoid main(String[] args) {
- ThreadCommunicationTest3 test = new ThreadCommunicationTest3();
- ExecutorService service = Executors.newCachedThreadPool();
- service.execute(test.new RunnableA());
- service.execute(test.new RunnableB());
- service.shutdown();
- }
- privateclass RunnableA implements Runnable {
- publicvoid run() {
- for (int i = 1; i <