最近我深入研究了Threads的黑暗艺术,我得到了如何创建它们以及何时使用它们以及何时不使用它们。但是当我试图学习如何在他们之间进行交流时;我发现管道就是你用来做它的。我有一个Object,它是我的一个Class'我创建的,但Pipes似乎只能发送Byte Arrays或Integers。我不会能够使用类似对象流的东西将我的对象发送到另一个线程,但我的互联网冲浪已经非常糟糕而且我迷失了。所以我猜猜唯一要做的就是转向Stack Overflow并查看是否有任何人可以提供帮助。感谢您的帮助。
答案 0 :(得分:5)
您应该使用BlockingQueue。
的其中一个实现我最常使用ArrayBlockingQueue因为它允许我限制解决方案的内存占用。 LinkedBlockingDeque可以用于无限大小,但可以肯定你不能超载内存。
以下是两个使用ArrayBlockingQueue
进行通信的线程。
public class TwoThreads {
public static void main(String args[]) throws InterruptedException {
System.out.println("TwoThreads:Test");
new TwoThreads().test();
}
// The end of the list.
private static final Integer End = -1;
static class Producer implements Runnable {
final BlockingQueue<Integer> queue;
public Producer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
for (int i = 0; i < 1000; i++) {
queue.add(i);
Thread.sleep(1);
}
// Finish the queue.
queue.add(End);
} catch (InterruptedException ex) {
// Just exit.
}
}
}
static class Consumer implements Runnable {
final BlockingQueue<Integer> queue;
public Consumer(BlockingQueue<Integer> queue) {
this.queue = queue;
}
@Override
public void run() {
boolean ended = false;
while (!ended) {
try {
Integer i = queue.take();
ended = i == End;
System.out.println(i);
} catch (InterruptedException ex) {
ended = true;
}
}
}
}
public void test() throws InterruptedException {
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
Thread pt = new Thread(new Producer(queue));
Thread ct = new Thread(new Consumer(queue));
// Start it all going.
pt.start();
ct.start();
// Wait for it to finish.
pt.join();
ct.join();
}
}