一、标志变量 + 互斥锁
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
private static int globalTag = 0;
private static final Lock LOCK = new ReentrantLock();
public static void main(String[] args) {
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
LOCK.lock();
while (myTag != globalTag) {
LOCK.unlock();
Thread.yield();
LOCK.lock();
}
System.out.print(myTag);
globalTag = (globalTag + 1) % 3;
} finally {
LOCK.unlock();
}
}
}
}
}
二、标志变量 + 互斥锁 + 条件变量
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
private static int globalTag = 0;
private static final Lock LOCK = new ReentrantLock();
private static final Condition[] conditions = new Condition[3];
public static void main(String[] args) {
conditions[0] = LOCK.newCondition();
conditions[1] = LOCK.newCondition();
conditions[2] = LOCK.newCondition();
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
LOCK.lock();
if (myTag != globalTag) {
conditions[myTag].await();
}
System.out.print(myTag);
globalTag = (myTag + 1) % 3;
conditions[globalTag].signal();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
LOCK.unlock();
}
}
}
}
}
三、标志变量 + synchronized
public class Main {
private static int globalTag = 0;
public static void main(String[] args) {
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
while (true) {
synchronized (MyRunnable.class) {
if (myTag == globalTag) {
break;
}
}
Thread.yield();
}
System.out.print(myTag);
globalTag = (myTag + 1) % 3;
}
}
}
}
public class Main {
private static int globalTag = 0;
public static void main(String[] args) {
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (MyRunnable.class) {
while (myTag != globalTag) {
try {
MyRunnable.class.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.print(myTag);
globalTag = (myTag + 1) % 3;
MyRunnable.class.notifyAll();
}
}
}
}
}
四、原子变量
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger globalTag = new AtomicInteger(0);
public static void main(String[] args) {
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
while (globalTag.get() != myTag) {
Thread.yield();
}
System.out.print(myTag);
globalTag.set((myTag + 1) % 3);
}
}
}
}
五、volatile
public class Main {
private static volatile int globalTag = 0;
public static void main(String[] args) {
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
while (globalTag != myTag) {
Thread.yield();
}
System.out.print(myTag);
globalTag = (myTag + 1) % 3;
}
}
}
}
六、信号量
import java.util.concurrent.Semaphore;
public class Main {
private static Semaphore[] semaphores = new Semaphore[3];
public static void main(String[] args) {
semaphores[0] = new Semaphore(1);
semaphores[1] = new Semaphore(0);
semaphores[2] = new Semaphore(0);
new Thread(new MyRunnable(0)).start();
new Thread(new MyRunnable(1)).start();
new Thread(new MyRunnable(2)).start();
}
private static class MyRunnable implements Runnable {
private int myTag;
public MyRunnable(int myTag) {
this.myTag = myTag;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
semaphores[myTag].acquire();
System.out.print(myTag);
semaphores[(myTag + 1) % 3].release();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
