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

查看:300
本文介绍了如何让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).

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

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()看起来不像解决方案 - 数据库线程仅在应用程序关闭时退出。

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 Concurrency

还有很多好书(google为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 我在这些示例中使用的最好是您需要从每个线程并发操作的对象,或者您可以创建一个单独的对象为此目的(我不建议让方法本身同步):

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天全站免登陆