情况是,我必须确保当我尝试getConnection时只创建一个RecoveryThread,如果它在PrimaryData Source上的getConnection失败,那么我的代码是:
public Connection getConnection() throws SQLException {
if (isFailedOver()) {
try {
return failoverDataSource.getConnection();
} catch (SQLException e) {
throwBigError();
}
}
Connection connection = null;
try {
connection = dataSource.getConnection();
return connection;
}
catch (SQLException unexpected) {
return requestFailover();
}
}
private Connection requestFailover() throws SQLException {
this.dbFailoverMutex.requestFailover();
DBFailoverRecoveryService recoveryService = new DBFailoverRecoveryService(this.dbFailoverMutex,this.dataSource);
Thread recoveryServiceThread = new Thread(recoveryService, "DBFailover Recovery Service");
recoveryServiceThread.start();
try {
return failoverDataSource.getConnection();
} catch (SQLException e) {
throwBigError();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
如果有两个不同的线程尝试getConnection,这可能最终调用requestFailover()方法两次,当它被调用两次时,这将最终创建两个recoveryService线程,我该怎么做以确保永远不会发生?
在此先感谢您的帮助.
CountDownLatch in java是一个高级同步实用程序,用于防止特定线程在所有线程准备就绪之前开始处理.
但是,Semaphore完全可以做同样的事情.那么,有什么优点CountDownLatch呢?
还有一个问题:如果CountDownLatch确实有一些优点,为什么它只被设计为使用一次?我认为添加set方法来重置计数很容易.
我在我的Android库项目中使用AsyncTask来避免networkonmainthreadexception,现在我想做的是将值返回到我的变量并在执行下一行代码之前等待它.我使用CountDownLatch来做到这一点.
我执行我的请求并调用我的AsyncTask的方式如下所示.所以我使用CountDownLatch等待响应,然后返回我想要返回的值.
public MyObject getMyObject(){
final String url = "http://[myip]:8080/retrieve";
try{
Response response = executeRequest(url);
final MyObject myObject = om.readValue(response.body().string(),
new TypeReference<MyObject>() {
});
return myObject;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Response executeRequest(String url){
try {
//Simplified please just ignore this..
GenericAsyncParams genericAsyncParams = createParams(......);
final CountDownLatch countDownLatch = new CountDownLatch(1);
ResponseListener responseListener = new ResponseListener(countDownLatch);
MyAsyncTask myAsyncTask = new AsyncTask(responseListener);
myAsyncTask.execute(genericAsyncParams);
countDownLatch.await();
//get the response from the listener class.
Response response = responseListener.getResponse(); …Run Code Online (Sandbox Code Playgroud) 我有一个希望打印值的代码示例。正如我在 countDownLatch.countDown(); 之后的 run 方法中所想的那样 被称为 CountDownLatch 应该达到零并且 main 方法应该终止,但它没有发生。我错过了什么吗?
public class ExecutorWithCountDownLatch {
public static void main(String[] args) throws InterruptedException {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
final CountDownLatch countDownLatch = new CountDownLatch(1);
executorService.execute(new ThreadCounter(countDownLatch));
countDownLatch.await(5, TimeUnit.SECONDS);
}
private static class ThreadCounter implements Runnable {
private CountDownLatch countDownLatch;
public ThreadCounter(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 0; i <= 25; i++) {
System.out.println("printing numbers: " + i);
}
countDownLatch.countDown();
}
} …Run Code Online (Sandbox Code Playgroud)