如何让 Java 线程等待另一个线程的输出? [英] How to make a Java thread wait for another thread's output?

查看:39
本文介绍了如何让 Java 线程等待另一个线程的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带有应用程序逻辑线程和数据库访问线程的 Java 应用程序.它们都在应用程序的整个生命周期中持续存在,并且都需要同时运行(一个与服务器对话,一个与用户对话;当应用程序完全启动时,我需要两者 他们去工作).

I'm making a Java application with an application-logic-thread and a database-access-thread. Both of them persist for the entire lifetime of the application and both need to be running at the same time (one talks to the server, one talks to the user; when the app is fully started, I need both of them to work).

但是,在启动时,我需要确保应用程序线程最初等待直到数据库线程准备好(当前通过轮询自定义方法 dbthread.isReady() 确定).我不介意应用线程是否阻塞,直到数据库线程准备就绪.

However, on startup, I need to make sure that initially the app thread waits until the db thread is ready (currently determined by polling a custom method dbthread.isReady()). I wouldn't mind if app thread blocks until the db thread was ready.

Thread.join() 看起来不是一个解决方案 - db 线程只在应用程序关闭时退出.

Thread.join() doesn't look like a solution - the db thread only exits at app shutdown.

while (!dbthread.isReady()) {} 有点作用,但空循环消耗了大量处理器周期.

while (!dbthread.isReady()) {} kind of works, but the empty loop consumes a lot of processor cycles.

还有其他想法吗?谢谢.

Any other ideas? Thanks.

推荐答案

我真的建议您阅读像 Sun 的 Java 并发,然后再开始进入神奇的多线程世界.

I would really recommend that you go through a tutorial like Sun's Java Concurrency before you commence in the magical world of multithreading.

还有许多好书(谷歌搜索Java并发编程"、Java并发实践".

There are also a number of good books out (google for "Concurrent Programming in Java", "Java Concurrency in Practice".

获得答案:

在你必须等待 dbThread 的代码中,你必须有这样的东西:

In your code that must wait for the dbThread, you must have something like this:

//do some work
synchronized(objectYouNeedToLockOn){
    while (!dbThread.isReady()){
        objectYouNeedToLockOn.wait();
    }
}
//continue with work after dbThread is ready

在您的 dbThread 方法中,您需要执行以下操作:

In your dbThread's method, you would need to do something like this:

//do db work
synchronized(objectYouNeedToLockOn){
    //set ready flag to true (so isReady returns true)
    ready = true;
    objectYouNeedToLockOn.notifyAll();
}
//end thread run method here

我在这些示例中使用的 objectYouNeedToLockOn 最好是您需要从每个线程并发操作的对象,或者您可以为此创建一个单独的 Object(我不建议使方法本身同步):

The objectYouNeedToLockOn I'm using in these examples is preferably the object that you need to manipulate concurrently from each thread, or you could create a separate Object for that purpose (I would not recommend making the methods themselves synchronized):

private final Object lock = new Object();
//now use lock in your synchronized blocks

为了加深您的理解:
还有其他(有时更好)的方法来执行上述操作,例如使用 CountdownLatches 等.从 Java 5 开始,java.util.concurrent 包和子包中有很多漂亮的并发类.你真的需要在网上找资料来了解并发,或者找一本好书.

To further your understanding:
There are other (sometimes better) ways to do the above, e.g. with CountdownLatches, etc. Since Java 5 there are a lot of nifty concurrency classes in the java.util.concurrent package and sub-packages. You really need to find material online to get to know concurrency, or get a good book.

这篇关于如何让 Java 线程等待另一个线程的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆