LeetCode 多线程练习2(1116. 打印零与奇偶数 / H2O 生成)
Posted Zephyr丶J
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 多线程练习2(1116. 打印零与奇偶数 / H2O 生成)相关的知识,希望对你有一定的参考价值。
多线程练习2
1116. 打印零与奇偶数
题目描述
假设有这么一个类:
class ZeroEvenOdd {
public ZeroEvenOdd(int n) { ... } // 构造函数
public void zero(printNumber) { ... } // 仅打印出 0
public void even(printNumber) { ... } // 仅打印出 偶数
public void odd(printNumber) { ... } // 仅打印出 奇数
}
相同的一个 ZeroEvenOdd 类实例将会传递给三个不同的线程:
- 线程 A 将调用 zero(),它只输出 0 。
- 线程 B 将调用 even(),它只输出偶数。
- 线程 C 将调用 odd(),它只输出奇数。
每个线程都有一个 printNumber 方法来输出一个整数。请修改给出的代码以输出整数序列 010203040506… ,其中序列的长度必须为 2n。
示例 1:
输入:n = 2
输出:“0102”
说明:三条线程异步执行,其中一个调用 zero(),另一个线程调用 even(),最后一个线程调用odd()。正确的输出为 “0102”。
示例 2:
输入:n = 5
输出:“0102030405”
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-zero-even-odd
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
信号量
信号量简单好用,真香
超过85
class ZeroEvenOdd {
private int n;
private Semaphore ling = new Semaphore(1);
private Semaphore ji = new Semaphore(0);
private Semaphore ou = new Semaphore(0);
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i++){
ling.acquire();
printNumber.accept(0);
if(i % 2 == 1)
ji.release();
else
ou.release();
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for(int i = 2; i <= n; i += 2){
ou.acquire();
printNumber.accept(i);
ling.release();
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i += 2){
ji.acquire();
printNumber.accept(i);
ling.release();
}
}
}
volatile+ Thread.yield()
85
class ZeroEvenOdd {
private int n;
private volatile boolean ling = true;
private volatile boolean ou = false;
private volatile boolean ji = false;
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n;){
if(!ling)
Thread.yield();
else{
printNumber.accept(0);
ling = false;
if(i % 2 == 1)
ji = true;
else
ou = true;
i++;
}
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for(int i = 2; i <= n;){
if(!ou)
Thread.yield();
else{
printNumber.accept(i);
ou = false;
ling = true;
i += 2;
}
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n;){
if(!ji)
Thread.yield();
else{
printNumber.accept(i);
ji = false;
ling = true;
i += 2;
}
}
}
}
原子类控制 + Thread.yield()
class ZeroEvenOdd {
private int n;
private AtomicInteger ji = new AtomicInteger(0);
private AtomicInteger ou = new AtomicInteger(0);
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i++){
while(ji.get() != 0 || ou.get() != 0)
Thread.yield();
printNumber.accept(0);
if(i % 2 == 1)
ji.incrementAndGet();
else
ou.incrementAndGet();
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for(int i = 2; i <= n; i += 2){
while(ou.get() != 1)
Thread.yield();
printNumber.accept(i);
ou.set(0);
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i += 2){
while(ji.get() != 1)
Thread.yield();
printNumber.accept(i);
ji.set(0);
}
}
}
ReentrantLock + Condition
85
class ZeroEvenOdd {
private int n;
private volatile int num = 0;
private Lock lock = new ReentrantLock();
private Condition ling = lock.newCondition();
private Condition ou = lock.newCondition();
private Condition ji = lock.newCondition();
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i++){
lock.lock();
try{
while(num != 0){
ling.await();
}
printNumber.accept(0);
if(i % 2 == 1){
ji.signal();
num = 1;
}
else{
ou.signal();
num = 2;
}
} catch(Exception e){
e.printStackTrace();
} finally{
lock.unlock();
}
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for(int i = 2; i <= n; i += 2){
lock.lock();
try{
while(num != 2){
ou.await();
}
printNumber.accept(i);
ling.signal();
num = 0;
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i += 2){
lock.lock();
try{
while(num != 1){
ji.await();
}
printNumber.accept(i);
ling.signal();
num = 0;
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
}
CountDownLatch
100
class ZeroEvenOdd {
private int n;
CountDownLatch ling = new CountDownLatch(0);
CountDownLatch ji = new CountDownLatch(1);
CountDownLatch ou = new CountDownLatch(1);
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i++){
//变为0执行
ling.await();
printNumber.accept(0);
ling = new CountDownLatch(1);
if(i % 2 == 1)
ji.countDown();
else
ou.countDown();
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for(int i = 2; i <= n; i += 2){
ou.await();
printNumber.accept(i);
ou = new CountDownLatch(1);
ling.countDown();
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i += 2){
ji.await();
printNumber.accept(i);
ji = new CountDownLatch(1);
ling.countDown();
}
}
}
SynchronousQueue
52
class ZeroEvenOdd {
private int n;
BlockingQueue<Integer> synzero = new SynchronousQueue<>();
BlockingQueue<Integer> synodd = new SynchronousQueue<>();
BlockingQueue<Integer> syneven = new SynchronousQueue<>();
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i++){
printNumber.accept(0);
if(i % 2 == 1){
//给奇数队列放东西
synodd.put(1);
}else
syneven.put(1);
//从0队列取东西,如果没有,阻塞
synzero.take();
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for(int i = 2; i <= n; i += 2){
syneven.take();
printNumber.accept(i);
synzero.put(1);
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i += 2){
synodd.take();
printNumber.accept(i);
synzero.put(1);
}
}
}
BlockingQueue
100
class ZeroEvenOdd {
private int n;
private BlockingQueue<Integer> ling = new LinkedBlockingQueue<>();
private BlockingQueue<Integer> ji = new LinkedBlockingQueue<>();
private BlockingQueue<Integer> ou = new LinkedBlockingQueue<>();
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for(int i = 1; i <= n; i++){
printNumber.accept(0);
if(i % 2 == 1)
ji.put(1);
else
ouLeetCode 多线程 1116. 打印零与奇偶数
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口