Reputation: 70
I have to make operations on a cache using many threads and thus have created 3 classes that extends from Thread (I realize now they can be Runnables also, or should be). Careful to note, these I'm running more than 1 instance of one of these threads but not at the same time. As in:
public class Operation1 extends Thread
public class Operation2 extends Thread
public class Operation3 extends Thread
Thread[] operation1Threads = new Thread[5];
So after I finish with this one, I create new threads for the second operation (at random).
The following is the way my run method is created.
I make use of cyclic barriers to wait for the threads to work at the same time.
public volatile boolean running = true;
public void run() {
while (running) {
cyclicbarrier1.await();
//some operation here
cyclicbarrier2.await();
}
}
The main thread is focusing on how long to run these for and at the end of that time, I try to stop the threads before working on the second operation. But it seems that my threads don't actually stop.
EDIT: I should note that I've tried interrupting the thread directly. And tried to reset the barrier both of which seemed to be bad practices from answers found on other threads.
for (int i=0; i < numthreads; i++) {
ops[i].interrupt();
}
I use an ExecutorService to manage these operations sequentially.
ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(new Operation1Runnable());
es.submit(new Operation2Runnable());
But the executorservice doesn't go to the next one.
Upvotes: 0
Views: 298