使用“生产者-消费者模式”编写代码实现:线程A随机间隔(10~200ms)按顺序生成1到100的数字(共100个),
放到某个队列中.线程B、C、D即时消费这些数据,线程B消费所有被3整除的数,
线程C消费所有被5整除的数,其它的由线程D进行消费。线程BCD消费这些数据时在控制台中打印出来,
要求按顺序打印这些数据
限时40分钟,可以查API
当时我用了blockQueue去实现,没有实现出来,后来被面试官问的哑口无言,下面是自己查阅资料,并请教了我公司老大,最后给整理出来的代码。后来觉的我老大给我说的很受用,这种题一只要把题目给分析清楚了(主要考察多线程之间的相互通讯,使用lock的多路通知),一般这种题都是会有坑的,千万不要跳进去。另外,java并发包里面的的东西真的很重要。
import java.util.LinkedList;
import java.util.PrimitiveIterator;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
static class MyQueue {
private Queue<Integer> queue = new LinkedList<>();
private PrimitiveIterator.OfLong longs = new Random().longs(10, 200).iterator();
private Lock lock = new ReentrantLock();
private Condition b = lock.newCondition();
private Condition c = lock.newCondition();
private Condition d = lock.newCondition();
public int b_pull() {
lock.lock();
try {
try {
b.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
return queue.remove();
}
public int c_pull() {
lock.lock();
try {
try {
c.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
return queue.remove();
}
public int d_pull() {
lock.lock();
try {
try {
d.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
lock.unlock();
}
return queue.remove();
}
public void start() {
new Thread(() -> {
for (int i = 1; i <= 100; i++) {
queue.add(i);
lock.lock();
try {
if (i % 3 == 0) {
b.signal();
} else if (i % 5 == 0) {
c.signal();
} else {
d.signal();
}
} finally {
lock.unlock();
}
try {
TimeUnit.MILLISECONDS.sleep(longs.nextLong());
} catch (InterruptedException ignore) {
}
}
System.out.println("all numbers are produced.");
}).start();
}
}
public static void main(String[] args) {
MyQueue queue = new MyQueue();
new Thread(() -> {
while (true) {
System.out.println(String.format("B(mod 3) consume : %d", queue.b_pull()));
}
}).start();
new Thread(() -> {
while (true) {
System.out.println(String.format("C(mod 5) consume : %d", queue.c_pull()));
}
}).start();
new Thread(() -> {
while (true) {
System.out.println(String.format("D(other) consume : %d", queue.d_pull()));
}
}).start();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.start();
}
}
网友评论