ロック、wait/notify、Semaphoreの3つの方法でマルチスレッド通信を実現
6304 ワード
JAvaのマルチスレッド通信にはロック,wait/notify,Semaphoreの3つの方式があり,この3つのマルチスレッド通信方式を一般的な面接問題で簡単に実証している.
2つのスレッドは指定された内容を循環間隔で印刷し、1つは1から52までの数字を印刷し、1つはAからZまでのアルファベットを印刷し、印刷出力は以下の通りである.
1 2 A 3 4
B
......
51 52 Z
ロック実装コードを使用すると、次のようになります.
wait/notify実装コードを使用すると、次のようになります.
Semaphore信号量を使用するコードは次のとおりです.
2つのスレッドは指定された内容を循環間隔で印刷し、1つは1から52までの数字を印刷し、1つはAからZまでのアルファベットを印刷し、印刷出力は以下の通りである.
1 2 A 3 4
B
......
51 52 Z
ロック実装コードを使用すると、次のようになります.
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;
public class ThreadCommunicationTest {
private final Lock lock = new ReentrantLock();
private final Condition conditionA = lock.newCondition();
private final Condition conditionB = lock.newCondition();
private static char currentThread = 'A';
public static void main(String[] args) {
ThreadCommunicationTest test = new ThreadCommunicationTest();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(test.new RunnableA());
service.execute(test.new RunnableB());
service.shutdown();
}
private class RunnableA implements Runnable {
public void 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();
}
}
}
}
private class RunnableB implements Runnable {
public void 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();
}
}
}
}
}
wait/notify実装コードを使用すると、次のようになります.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadCommunicationTest2 {
private static char currentThread = 'A';
private final Object t = new Object(); //
public static void main(String[] args) {
ThreadCommunicationTest2 test = new ThreadCommunicationTest2();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(test.new RunnableA());
service.execute(test.new RunnableB());
service.shutdown();
}
private class RunnableA implements Runnable {
public void 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();
}
}
}
}
}
private class RunnableB implements Runnable {
public void 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();
}
}
}
}
}
Semaphore信号量を使用するコードは次のとおりです.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class ThreadCommunicationTest3 {
private final Semaphore semap = new Semaphore(1);// 1 ,
private static char currentThread = 'A';
public static void main(String[] args) {
ThreadCommunicationTest3 test = new ThreadCommunicationTest3();
ExecutorService service = Executors.newCachedThreadPool();
service.execute(test.new RunnableA());
service.execute(test.new RunnableB());
service.shutdown();
}
private class RunnableA implements Runnable {
public void run() {
for (int i = 1; i <= 52; i++) {
try {
semap.acquire();
while (currentThread != 'A') {
semap.release();
}
System.out.println(i);
if (i % 2 == 0) {
currentThread = 'B';
}
} catch (Exception e) {
e.printStackTrace();
} finally {
semap.release();
}
}
}
}
private class RunnableB implements Runnable {
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
try {
semap.acquire();
while (currentThread != 'B') {
semap.release();
}
System.out.println(c);
currentThread = 'A';
} catch (Exception e) {
e.printStackTrace();
} finally {
semap.release();
}
}
}
}
}