使java多线程等待,直到输入

Fuad Hanif

我试图编写一个简单的代码来模拟使用Java的DB2数据库上的并发连接。我当前的代码看起来像这样:

class TheThread implements Runnable{
@override
public void run(){
    //make the database connection
    //need to pause here until any button pressed
    //execute query to the database
}
}

该程序将同时创建几百到数千个线程,因此我想确保在执行查询之前所有线程都已连接,这样它才能真正同时被处理。

我怎样才能做到这一点?

叶夫根尼(Evgeniy Dorofeev)

您可以从java.util.concurrent包中使用CyclicBarrier

static CyclicBarrier b = new CyclicBarrier(nConnections);

public void run() {
    // make the database connection
    b.await();  //threads will stop here untill nConnections are opened
    ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章