问题:
让2个线程或者3个线程轮流顺序打印
1、synchrnized 和 共享变量 实现
static int state = 0;
static class SynState implements Runnable{
// 锁
private final Object prev;
// 第几个线程
private final int a;
// 总共开启的线程数量
private final int n;
SynState(Object prev, int a, int n){
this.prev = prev;
this.a = a;
this.n = n;
}
@Override
public void run() {
for (int i = 0; i < 4; i++) {
synchronized (prev){
// 当前线程执行完,轮到下一线程
while(state % n != a){
try {
// 阻塞
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "||" + i);
// 下一执行线程
state++;
// 通知
prev.notifyAll();
}
}
}
}
测试:
public static void main(String[] args){
Object prev = new Object();
// 线程的数量
int n = 3;
for (int i = 0; i < n; i++) {
new Thread(new SynState(prev, i, n)).start();
}
}
输出:
Thread-0||0
Thread-1||0
Thread-2||0
Thread-0||1
Thread-1||1
Thread-2||1
Thread-0||2
Thread-1||2
Thread-2||2
Thread-0||3
Thread-1||3
Thread-2||3
2、Lock 和 共享变量 实现
static ReentrantLock lock = new ReentrantLock();
static int state = 0;
static class LockState extends Thread{
// 第几个线程
private final int a;
// 总共开启的线程数量
private final int n;
LockState(int a, int n){
this.a = a;
this.n = n;
}
@Override
public void run(){
for (int i = 0; i < 4;) {
try{
lock.lock();
while(state % n == a){
System.out.println(Thread.currentThread().getName() + "||" + i);
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}
测试:
public static void main(String[] args){
int n = 3;
for (int i = 0; i < n; i++) {
new LockState(i, n).start();
}
}
输出:
Thread-0||0
Thread-1||0
Thread-2||0
Thread-0||1
Thread-1||1
Thread-2||1
Thread-0||2
Thread-1||2
Thread-2||2
Thread-0||3
Thread-1||3
Thread-2||3
3、Lock 和 Condition 实现
static ReentrantLock lock = new ReentrantLock();
static int state = 0;
static Condition A = lock.newCondition();
static Condition B = lock.newCondition();
static Condition C = lock.newCondition();
static class ThreadA extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try{
lock.lock();
// 等待到该线程执行的时候
while(state % 3 != 0){
A.await();
}
state++;
System.out.println(Thread.currentThread().getName() + "||" + i);
B.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
}
static class ThreadB extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try{
lock.lock();
// 等待到该线程执行的时候
while(state % 3 != 1){
B.await();
}
state++;
System.out.println(Thread.currentThread().getName() + "||" + i);
C.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
}
static class ThreadC extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try{
lock.lock();
// 等待到该线程执行的时候
while(state % 3 != 2){
C.await();
}
state++;
System.out.println(Thread.currentThread().getName() + "||" + i);
A.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
}
测试:
public static void main(String[] args){
new ThreadA().start();
new ThreadB().start();
new ThreadC().start();
}
输出:
Thread-0||0
Thread-1||0
Thread-2||0
Thread-0||1
Thread-1||1
Thread-2||1
Thread-0||2
Thread-1||2
Thread-2||2
Thread-0||3
Thread-1||3
Thread-2||3
4、Semaphore实现
实现:
/**
* 使用 Semaphore
* */
static Semaphore a = new Semaphore(1);
static Semaphore b = new Semaphore(0);
static Semaphore c = new Semaphore(0);
static class Thread1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try {
a.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "||" + i);
b.release();
}
}
}
static class Thread2 extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try {
b.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "||" + i);
c.release();
}
}
}
static class Thread3 extends Thread{
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try {
c.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "||" + i);
a.release();
}
}
}
测试:
public static void main(String[] args){
new Thread1().start();
new Thread2().start();
new Thread3().start();
}
输出:
Thread-0||0
Thread-1||0
Thread-2||0
Thread-0||1
Thread-1||1
Thread-2||1
Thread-0||2
Thread-1||2
Thread-2||2
Thread-0||3
Thread-1||3
Thread-2||3