How do i check if a thread is running in the pool of thread ExecutorService?
Background:
I want to synchronize between the threads in the Thread pool if there is a flag set.
So if the flag is set to true for synchronization, then I have to check if other Threads are running or wait for its completion and then invoke the blocking thread with synchronize, so that other threads would wait for this blocking thread to finish.
If flag is not set then no need to synchronize and could execute the threads in parallel.
Thanks!
You need to use a Semaphore
.
This allows you to have a number of "permits" to do work. If you only want one task running at a time then have a Semaphore
with one permit, otherwise have a Semaphore
with a number of permits greater than the number of Thread
s in the pool.
static class Worker implements Runnable {
final Semaphore semaphore;
public Worker(Semaphore semaphore) {
this.semaphore = semaphore;
}
@Override
public void run() {
try {
semaphore.acquire();
try {
//do stuff
} finally {
semaphore.release();
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
public static void main(String[] args) {
final int numThreads = 10;
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final Semaphore semaphore;
boolean myflag = true;
if (myflag) {
semaphore = new Semaphore(1);
} else {
semaphore = new Semaphore(numThreads);
}
final Worker worker = new Worker(semaphore);
executorService.submit(worker);
}
This example is a little contrived as you can just use a newSingleThreadExecutor()
when you only need one task to run at a time - but I assume you know that and for some reason cannot.
EDIT
Having poked around a little to see if this can be tidied I came across this. This hints at a neater solution:
static interface TaskBlocker {
void acquire();
void release();
}
static class Worker implements Runnable {
final TaskBlocker taskBlocker;
public Worker(TaskBlocker taskBlocker) {
this.taskBlocker = taskBlocker;
}
@Override
public void run() {
taskBlocker.acquire();
try {
//do stuff
} finally {
taskBlocker.release();
}
}
}
public static void main(String[] args) {
final int numThreads = 10;
final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
final TaskBlocker taskBlocker;
boolean myflag = true;
if (myflag) {
taskBlocker = new TaskBlocker() {
final Lock lock = new ReentrantLock();
@Override
public void acquire() {
lock.lock();
}
@Override
public void release() {
lock.unlock();
}
};
} else {
taskBlocker = new TaskBlocker() {
@Override
public void acquire() {
}
@Override
public void release() {
}
};
}
final Worker worker = new Worker(taskBlocker);
executorService.submit(worker);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With