栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多线程交替输出

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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("cn");
});
syncPark.setThreads(t1, t2, t3);
syncPark.start();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/961547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号