Following is my class which uses a CountDownLatch and ExecutorService. I all startTest method from my activity. When I run this code CountdownLatch doesn't come out of wait.
public class Test implements Tester.StartListener, Tester.StopListener, Runnable{
private CountDownLatch latch;
ExecutorService executorService;
Context context;
ListElement listElement;
int countDown;
final String user = "palapi_android@tourmalinelabs.com";
public Test(Context context) {
this.context = context;
this.listElement = listElement;
}
public void startTest(int nTimes) throws InterruptedException {
this.countDown = nTimes;
executorService = Executors.newSingleThreadExecutor();
latch = new CountDownLatch(countDown);
executorService.execute(this);
latch.await();
}
@Override
public void run() {
setText("Running");
Tester.Start(context, user, this);
}
@Override
public void OnStarted() {
Tester.Stop(context, this);
}
@Override
public void OnFail( int reason ) {
Log.d("Failure", "failed because " + reason);
}
@Override
public void OnStopped() {
setText(String.valueOf(countDown));
countDown --;
latch.countDown();
if( countDown >= 0) {
runAfterDelayInRange(new Runnable() {
@Override
public void run() {
Tester.Start(context, user, Test.this);
}
});
} else {
setText("Done");
}
}
protected void setText(final String text) {
getHandler().post(new Runnable() {
@Override
public void run() {
if (listElement != null) {
listElement.setTestResult(text);
}
LauncherActivity activity = (LauncherActivity) context;
activity.notifyDataSetChanged();
}
});
}
protected void runAfterDelayInRange(Runnable action) {
getHandler().post(action);
}
private Handler getHandler(){
return new Handler(context.getMainLooper());
}
}
Can someone please tell me what am I doing wrong? Thank you in advance