多线程实现按序打印
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了多线程实现按序打印,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3980字,纯文字阅读大概需要6分钟。
内容图文
LeetCode1114
lock-condition的wait()和signal()
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Foo {
int num;
Lock lock;
//精确的通知和唤醒线程
Condition condition1, condition2, condition3;
public Foo() {
num = 1;
lock = new ReentrantLock();
condition1 = lock.newCondition();
condition2 = lock.newCondition(); condition3 = lock.newCondition();
}
public void first(Runnable printFirst) throws InterruptedException {
lock.lock();
try {
while (num != 1) {
condition1.await();
}
printFirst.run();
num = 2;
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void second(Runnable printSecond) throws InterruptedException {
lock.lock();
try {
while (num != 2) {
condition2.await();
}
printSecond.run();
num = 3;
condition3.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void third(Runnable printThird) throws InterruptedException {
lock.lock();
try {
while (num != 3) {
condition3.await();
}
printThird.run();
num = 1;
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
信号量Semaphore
import java.util.concurrent.Semaphore;
class Foo {
Semaphore semaphore12, semaphore23;
public Foo() {
//初始的允许请求均设为0
semaphore12 = new Semaphore(0);
semaphore23 = new Semaphore(0);
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
//释放?一个12的信号量量
semaphore12.release();
}
public void second(Runnable printSecond) throws InterruptedException {
//获取?一个12的信号量量,没有则阻塞
semaphore12.acquire();
printSecond.run();
//释放?一个23的信号量量
semaphore23.release();
}
public void third(Runnable printThird) throws InterruptedException {
//获取?一个23的信号量量,没有则阻塞
semaphore23.acquire();
printThird.run();
}
}
使用synchronized
public class Foo {
private volatile int flag = 1;
private final Object object = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (object) {
while (flag != 1) {
object.wait();
}
printFirst.run();
flag = 2;
object.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (object) {
while (flag != 2) {
object.wait();
}
printSecond.run();
flag = 3;
object.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (object) {
while (flag != 3) {
object.wait();
}
printThird.run();
}
}
}
使用CountDownLatch
import java.util.concurrent.CountDownLatch;
class Foo {
CountDownLatch countDownLatch12, countDownLatch23;
public Foo() {
countDownLatch12 = new CountDownLatch(1);
countDownLatch23 = new CountDownLatch(1);
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
countDownLatch12.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
//等待计数器器归零再向下执?
countDownLatch12.await();
printSecond.run();
countDownLatch23.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
//等待计数器器归零再向下执?
countDownLatch23.await();
printThird.run();
}
}
使用AtomicInteger
class Foo {
private AtomicInteger firstJobDone = new AtomicInteger(0);
private AtomicInteger secondJobDone = new AtomicInteger(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first".
printFirst.run();
// mark the first job as done, by increasing its count.
firstJobDone.incrementAndGet();
}
public void second(Runnable printSecond) throws InterruptedException {
while (firstJobDone.get() != 1) {
// waiting for the first job to be done.
}
// printSecond.run() outputs "second".
printSecond.run();
// mark the second as done, by increasing its count.
secondJobDone.incrementAndGet();
}
public void third(Runnable printThird) throws InterruptedException {
while (secondJobDone.get() != 1) {
// waiting for the second job to be done.
}
// printThird.run() outputs "third".
printThird.run();
}
}
内容总结
以上是互联网集市为您收集整理的多线程实现按序打印全部内容,希望文章能够帮你解决多线程实现按序打印所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。