java中的线程间通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2170520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
inter thread communication in java
提问by tipu
How do threads that rely on one another communicate in Java?
相互依赖的线程如何在 Java 中进行通信?
For example, I am building a web crawler with threads that need data that comes from other threads.
例如,我正在构建一个带有需要来自其他线程的数据的线程的网络爬虫。
采纳答案by cletus
That depends on the nature of the communication.
这取决于通信的性质。
- Is it duplex (ie A talks to B and B talks to A)?
- Is it communication of data or communication of completion?
- 它是双工的(即 A 与 B 对话,B 与 A 对话)?
- 是数据通信还是完成通信?
and so on.
等等。
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That's most easily done by using Future
:
线程间通信最简单和最可取的形式就是等待其他线程完成。使用Future
以下方法最容易做到:
ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable() {
@Override
public void run() {
f.get();
// do stuff
}
});
The second task won't execute until the first completes.
在第一个任务完成之前,第二个任务不会执行。
Java 5+ has manyconcurrent utilities for dealing with this kind of thing. This could mean using LinkedBlockingQueue
s, CountDownLatch
or many, many others.
Java 5+ 有许多并发工具来处理这种事情。这可能意味着使用LinkedBlockingQueue
sCountDownLatch
或许多其他的。
For an in-depth examination of concurrency Java Concurrency in Practiceis a must-read.
对于并发性的深入研究,Java Concurrency in Practice是必读的。
回答by David Sowsy
Take a look at java.util.Observer/java.util.Observable. They are exactly what you are looking for.
看看java.util.Observer/java.util.Observable。它们正是您要寻找的。
回答by Chris
You could use 2 ExecutorServices, submit a Callable implementation to service A that knows about follow on service B. When the Callable has completed it's work, it submits a new task to service B that does something additional with the results.
您可以使用 2 个 ExecutorServices,向服务 A 提交一个 Callable 实现,该实现知道服务 B 的后续工作。当 Callable 完成其工作后,它会向服务 B 提交一个新任务,该任务对结果执行其他操作。
You could use a CountDownLatch or some other barrier if you need to perform additional work once all items have been processed in both services.
如果您需要在两个服务中处理完所有项目后执行其他工作,您可以使用 CountDownLatch 或其他一些障碍。
The ExecutorService api is pretty straight forward, for the most part you'll probably be using something like .newFixedThreadPool(int threads) and submitting Runnables/Callables to it.
ExecutorService api 非常简单,在大多数情况下,您可能会使用诸如 .newFixedThreadPool(int threads) 之类的东西并将 Runnables/Callables 提交给它。
回答by Sujay
Below is example for Inter thread communication:
下面是线程间通信的示例:
public class Main {
public static void main(String[] args) {
Chat m = new Chat();
new T1(m);
new T2(m);
}
}
class Chat {
boolean flag = false;
public synchronized void FromSam(String msg) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag = true;
notify();
}
public synchronized void FromJam(String msg) {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(msg);
flag = false;
notify();
}
}
class T1 implements Runnable {
Chat m;
String[] s1 = { "Hello Jam", "How are you ?", "I am also doing fine!" };
public T1(Chat m1) {
this.m = m1;
new Thread(this, "Sam").start();
}
public void run() {
for (int i = 0; i < s1.length; i++) {
m.FromSam(s1[i]);
}
}
}
class T2 implements Runnable {
Chat m;
String[] s2 = { "HI Sam", "I am good,And U ?", "ha haa" };
public T2(Chat m2) {
this.m = m2;
new Thread(this, "Jam").start();
}
public void run() {
for (int i = 0; i < s2.length; i++) {
m.FromJam(s2[i]);
}
}
}