java多线程交替输出

实现Java多线程交替输出

要求:线程 1 输出 a 5 次,线程 2 输出 b 5 次,线程 3 输出 c 5 次。现在要求输出 abcabcabcabcabc
1、wait notify 版
class SyncWaitNotify {
	private int flag;
	private int loopNumber;
	public SyncWaitNotify(int flag, int loopNumber) {
		this.flag = flag;
		this.loopNumber = loopNumber;
	}
	public void print(int waitFlag? int nextFlag, String str) {
	
		for (int i = 0; i < loopNumber; i++) {
			synchronized (this) {
				while (this.flag != waitFlag) {
					try {
						this.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			System.out.print(str);
			flag = nextFlag;
			this.notifyAll();
			}
		}
	}
}
SyncWaitNotify SyncWaitNotify = new SyncWaitNotify(1, 5); 
new Thread(() -> {
	SyncWaitNotify.print(1, 2, "a");
}).start();
new Thread(() -> {
	SyncWaitNotify.print(2, 3, "b");
}).start();
new Thread(() -> {
	SyncWaitNotify.print(3, 1, "c");
}).start();
2 Lock 条件变量版
class AwaitSignal extends ReentrantLock { 
	public void start(Condition first) { this.lock();
		try {
			log.debug("start ”);
			first.signal();
		} finally {
			this.unlock();
		}
	}
	public void print(String str. Condition currentCondition next){
		for (int i = 0; i < loopNumber; i++) {
			this.lock();
			try {
				current.await();
				log.debug(str);
				next.signal();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				this. unlock();
			}
		}
	}
	//循环次数
	private int loopNumber;
	public AwaitSignal(int loopNumber) {
		this.loopNumber = loopNumber;
	}
}
AwaitSignal as = new AwaitSignal(5); 
Condition aWaitSet = as.newCondition(); 
Condition bWaitSet = as.newCondition(); 
Condition cWaitSet = as.newCondition();
new Thread(() -> {
	as.print("a", aWaitSet, bWaitSet); }).start();
new Thread(() -> {
	as.print("b", bWaitSet, cWaitSet); }).start();
new Thread(() -> {
	as.print("c", cWaitSeta,WaitSet); }).start();
as.start(aWaitSet);

该实现没有考虑a, b, c线程都就绪再开始.

3 Park Unpark 版
class SyncPark { private int loopNumber; private Thread[] threads;
	public SyncPark(int loopNumber) { 
		this.loopNumber = loopNumber;
	}
	public void setThreads(Thread... threads) { 
		this.threads = threads;
	}
	public void print(String str) {
		for (int i = 0; i < loopNumber; i++) { 
			LockSupport.park(); 
			System.out.print(str);
			LockSupport.unpark(nextThread());
		}
	}
	private Thread nextThread() {
		Thread current = Thread.currentThread();
		int index = 0;
		for (int i = 0; i < threads.length; i++) {
			if(threads[i] == current) { 
				index = i;
				break;
			}
		}
		if(index < threads.length - 1) (
			return threads[index+1];
		} else {
			return threads[0];
		}
	}
	public void start() {
		for (Thread thread : threads) { 
			thread.start();
		}
		LockSupport.unpark(threads[0]);
	}
}
SyncPark syncPark = new SyncPark(5);
Thread t1 = new Thread(() -> {
 syncPark.print("a");
});
Thread t2 = new Thread(() -> {
 syncPark.print("b");
});
Thread t3 = new Thread(() -> {
 syncPark.print("c\n");
});
syncPark.setThreads(t1, t2, t3);
syncPark.start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值