Java While(true) thread cleanup - java

I'm having trouble cleaning up my threads in a threaded application. I have various runnables that are kicked off using a thread pool. Most of these threads are normal runnables that only execute once on a Scheduled Fixed Rate. Two of them I have are scheduled to run once and have a while(true) loop in them. When I get to cleaning up the threads, it seems I'm having trouble calling ScheduledFuture.close(false) on the threads with the while loop in them. They don't seem to close.
An example of the format of these threads with while loops are:
public void run()
{
while (true)
{
QueryItem qi = null;
String query = null;
try
{
// This is a BlockingQueue!
qi = (QueryItem) FB2DatabaseRecorder.dbProcQueue.take();
query = qi.getQuery();
} catch (InterruptedException e)
{
errorLog.error("Unable to fetch message from message processing queue.");
}
// DO SOME STUFF
}
}
When I try to do the .close() on this thread, it's typically sitting at the blocking queue waiting for an item to come in. Before closing the threads I ensure that the queues are flushed as to not leave any data behind.
Is there a better way to close this type of thread? It seems like it is just not dying with handle.close(false);

A better way to shutdown your worker thread is to use Thread.interrupt().
Your worker thread is waiting on the take call, and the take call throws if the thread is interrupted. You can send an interrupt and manage the shutdown in the catch clause. In code,
package stackOv;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
class MyQueueWorker implements Runnable {
private BlockingQueue<Object> q;
MyQueueWorker(BlockingQueue<Object> q) {
this.q = q;
}
#Override
public void run() {
while (true) {
try {
Object item = q.take();
// work here
System.out.println("obj=" + item);
} catch (InterruptedException e) {
System.out.println("worker thread is interrupted");
break;
}
}
System.out.println("interrupted, exiting worker thread");
}
}
public class InterruptTake {
public static void main(String[] args) throws Exception {
BlockingQueue<Object> q = new LinkedBlockingQueue<>();
Thread worker = new Thread( new MyQueueWorker(q ), "worker" );
worker.start();
q.put("hello");
q.put("world");
q.put("waiting..");
Thread.sleep(1000);
worker.interrupt();
}
}

Related

Avoid the use of Shutdown Flag in multithreaded code

I have the following code:
private static final AtomicBoolean shutdown = new AtomicBoolean(false);
public static void main(final String... args) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shutdown.set(true);
executorService.shutdown();
try {
executorService.awaitTermination(SHUTDOWN_TIMEOUT.getSeconds(), TimeUnit.SECONDS);
} catch (InterruptedException e) {
executorService.shutdownNow();
}
}));
executorService = Executors.newFixedThreadPool(2);
for (int i = 0; i < 2; i++) {
executorService.execute(create());
}
}
private static Runnable create() {
return new Runnable() {
#Override
public void run() {
while (!shutdown.get()) {
try {
Thread.sleep(5000);
System.out.println("Hatella" + Thread.currentThread().getName());
} catch (Throwable t) {
}
}
}
};
}
This code is working perfectly fine but I wanted to write this code in much simpler way so that I don't have to check the shutdown flag status in each while loop. Any idea what can I do to fix this and achieve the same thing.
shutdown() will only make the ExecutorService not accepting more tasks, but it will continue executing all pending tasks to the end. Since you actually want to stop executing tasks, you should use shutdownNow() in the first place, which will send an interruption signal.
public static void main(final String... args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
executorService.shutdownNow();
try {
executorService.awaitTermination(
SHUTDOWN_TIMEOUT.getSeconds(),TimeUnit.SECONDS);
} catch (InterruptedException e) {}
}));
for (int i = 0; i < 2; i++) {
executorService.execute(create());
}
}
private static Runnable create() {
return () -> {
while(!Thread.interrupted()) {
try {
Thread.sleep(5000);
System.out.println("Hatella" + Thread.currentThread().getName());
}
catch(InterruptedException ex) {
break;
}
catch (Throwable t) {
}
}
System.out.println("thread exit " + Thread.currentThread().getName());
};
}
The interruption flag can not only be queried via Thread.interrupted(), it will also make blocking actions like Thread.sleep(…) terminate earlier, reporting the situation via InterruptedException. In both cases, when Thread.interrupted() returned true or when the InterruptedException has been thrown, the interrupt status will be reset, so it’s crucial to either, react on it immediately or remember that you received it. So in the above example, catch(InterruptedException ex) contains a break, to end the loop.
But as shown, interruption does not terminate a thread but allows to react on it, e.g. by cleaning up when necessary, before exiting.
Note that when the only lengthy operations are the blocking ones, you don’t need to poll the interrupted status manually at all, e.g. the following would work too:
private static Runnable create() {
return () -> {
while(true) {
try {
Thread.sleep(5000);
System.out.println("Hatella" + Thread.currentThread().getName());
}
catch(InterruptedException ex) {
System.out.println("got "+ex+", "+Thread.interrupted());
break;
}
catch (Throwable t) {
}
}
System.out.println("thread exit");
};
}
Since this code does not check-and-reset the interrupted state via Thread.interrupted(), the signal will persist until the next invocation of Thread.sleep, which will be soon enough to appear as an immediate response, as the code executed between two sleep calls is short.
A) See Turning an ExecutorService to daemon in Java. Daemon threads will technically answer stated question (no requirement to poll a "shutdown" variable) but are probably a bad idea in any stateful context as the thread will be stopped in the middle of operation with no warning by the JVM (as soon as all non-daemon threads complete).
executorService = Executors.newFixedThreadPool(2, r -> {
Thread t = Executors.defaultThreadFactory().newThread();
t.setDaemon(true);
return t;
});
B) Another option in the real world (where an idle thread is likely blocking/sleeping on something) is to check shutdown only upon the InterruptedException which will occur upon executorService.shutdownNow()

Best synchronization strategy to block worker threads when a database is down

Multiple workers are processing from a queue and when a database failure occurs it will contact a supervisor that will then lock all worker threads and poll the database at an interval until it is up and it will then release all the threads so they can continue processing. The worker threads can either advance or wait with the processing and the supervisor thread can lock or unlock.
I was thinking of an interface like this. What synchronization primitives would you use? Actors would be a good solution but i don't have the time for a rewrite.
public interface Latch {
/**
* This method will cause a thread(s) to only advance if the latch is in an open state. If the
* latch is closed the thread(s) will wait until the latch is open before they can advance.
*/
void advanceWhenOpen();
/**
* Close the latch forcing all threads that reaches the latch's advance method to wait until
* its open again.
*/
void close();
/**
* Opens the latch allowing blocked threads to advance.
*/
void open();
boolean isOpen();
}
What you want is not really a "latch" - at least the "Java Concurrency in Practice" book says that "Once the latch reaches the terminal state, it cannot change state again, so it remains open forever."
But you can use CountDownLatch objects in the background - whenever your "Latch" needs to be closed, then you can create a new CountDownLatch object with the count of one and await() on in in your advanceWhenOpen(). I think that from a readability point of view this would be the best solution.
I would use a ReadWriteLock as the synchronization primitive for this purpose. The advantage of a read/write lock as opposed to a simple monitor or mutex is that multiple threads can hold the read lock at any given time. This is advantageous when you have lots of readers (e.g. your thread pool in this case) and only one or few writers (e.g. the thread checking for open/close of the database).
With a single monitor or mutex, your threads will serialize on the one lock, making that section of code contentious.
One option is to proxy the queue to make it pausable when the database is unavailable. Workers can check the paused state of the queue while processing and, if necessary, wait for it to unpause. A basic code-demonstration:
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;
public class PausableQueue<T> {
LinkedBlockingQueue<T> q = new LinkedBlockingQueue<T>();
AtomicReference<CountDownLatch> pause = new AtomicReference<CountDownLatch>(new CountDownLatch(0));
public T take() throws InterruptedException {
awaitPause();
return q.take();
}
public void awaitPause() throws InterruptedException {
pause.get().await();
}
public void setPaused(boolean paused) {
if (paused) {
// only update if there are no threads waiting on current countdown-latch
if (!isPaused()) {
pause.set(new CountDownLatch(1));
}
} else {
pause.get().countDown();
}
}
public boolean isPaused() {
return (pause.get().getCount() > 0L);
}
/* *** Test the pausable queue *** */
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
try {
testPause(executor);
} catch (Exception e) {
e.printStackTrace();
}
executor.shutdownNow();
}
private static void testPause(ExecutorService executor) throws Exception {
final PausableQueue<Object> q = new PausableQueue<Object>();
for (int i = 0; i < 3; i++) {
q.q.add(new Object());
}
final CountDownLatch tfinished = new CountDownLatch(1);
Runnable taker = new Runnable() {
#Override
public void run() {
println("Taking an object.");
try {
Object o = q.take();
println("Got an object: " + o);
} catch (Exception e) {
e.printStackTrace();
} finally {
tfinished.countDown();
}
}
};
executor.execute(taker);
tfinished.await();
final CountDownLatch tstarted2 = new CountDownLatch(2);
final CountDownLatch tfinished2 = new CountDownLatch(2);
taker = new Runnable() {
#Override
public void run() {
println("Taking an object.");
tstarted2.countDown();
try {
Object o = q.take();
println("Got an object: " + o);
} catch (Exception e) {
e.printStackTrace();
} finally {
tfinished2.countDown();
}
}
};
q.setPaused(true);
println("Queue paused");
executor.execute(taker);
executor.execute(taker);
tstarted2.await();
// Pause to show workers pause too
Thread.sleep(100L);
println("Queue unpausing");
q.setPaused(false);
tfinished2.await();
// "Got an object" should show a delay of at least 100 ms.
}
private static void println(String s) {
System.out.println(System.currentTimeMillis() + " - " + s);
}
}

Looking for a solid explanation on how aquire and release work with semaphores to sync threads

I am trying to manipulate this program to print ":---)))))" repeatedly.
I understand that a semaphore is a way of controlling threads, and acquire essentially acquires a permit (reads) and release returns a permit back to the semaphore. (writes)
I've tried manipulating the number of permits when initializing the semaphores, but I am not understanding how to sync them together because I can't figure out how exactly the semaphores operate with how they acquire and release.
I am looking for a helpful explanation that pertains to Java in the context of only using semaphores, acquire and release and how they work together to properly put the threads "in sync"
import java.lang.Thread;
import java.util.concurrent.*;
public class ThreadSync {
private static boolean runFlag = true;
private static Semaphore canPrintC = new Semaphore(1);
private static Semaphore canPrintD = new Semaphore(0);
private static Semaphore canPrintP = new Semaphore(0);
public static void main(String [] args) {
// Create and start each runnable
Runnable task1 = new TaskPrintC();
Runnable task2 = new TaskPrintD();
Runnable task3 = new TaskPrintP();
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
Thread thread3 = new Thread(task3);
thread1.start();
thread2.start();
thread3.start();
// Let them run for 500 ms
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
runFlag = false;
thread3.interrupt();
thread2.interrupt();
thread1.interrupt();
}
public static class TaskPrintC implements Runnable {
public void run() {
while (runFlag) {
try {
canPrintC.acquire();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.printf("%s", ":");
canPrintD.release();
}
}
}
public static class TaskPrintD implements Runnable {
public void run() {
while (runFlag) {
try {
canPrintD.acquire();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.printf("%s", "-");
canPrintP.release();
}
}
}
public static class TaskPrintP implements Runnable {
public void run() {
while (runFlag) {
try {
canPrintP.acquire();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.printf("%s", ")");
canPrintC.release();
}
}
}
}
Threads execute tasks and semaphores can help you to let tasks (or runnable objects) know each other's state (e.g. task A waits for input from task B and task B can signal task A that input is available). The difference between a task and a thread is important.
To stress this point, I have taken your example and made one runnable class that performs the task of printing a character a number of times (configured via variables in the constructor). To mimic the serialized behavior (tasks run after each other), the runnable is also aware of the next runnable that should perform the print task.
To complete the example I also ensured that the thread that is executing the main-method is aware of when the tasks have completed, so that the program stops at the proper time. A CountDownLatch is used in this case (a CountDownLatch is a very simple variation of a Semaphore).
The example below might be a bit hard to understand, but it shows some good practices (re-use code, using a stop-flag instead of interrupt, use an executor to run tasks, cleanup and stop tasks in case of error). It also shows how Semaphores can orchestrate the execution of tasks.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class ChainedSemaphoreTasks {
// amount of times chained tasks are executed.
static int MAX_CHAINED_LOOPS = 3;
// helper to let main-thread know when chained loops have been executed.
final static CountDownLatch MAX_LOOPS_REACHED = new CountDownLatch(1);
public static void main(String[] args) {
String printChars = ":-)";
int[] repeatChars = { 1, 3, 5};
List<ChainedTask> tasks = buildTasks(printChars, repeatChars);
ExecutorService executor = Executors.newCachedThreadPool();
for (ChainedTask task : tasks) {
executor.execute(task);
}
try {
// Trigger first task to start running.
tasks.get(0).triggerPrintTask();
// wait for loop to complete, but not too long.
if (!MAX_LOOPS_REACHED.await(5000L, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Chained tasks loop did not complete within timeout.");
}
long waitStart = System.currentTimeMillis();
executor.shutdown();
if (executor.awaitTermination(1000L, TimeUnit.MILLISECONDS)) {
System.out.println("All tasks stopped within " + (System.currentTimeMillis() - waitStart) + " ms.");
} else {
throw new RuntimeException("Not all chained tasks stopped within timeout.");
}
} catch (Exception e) {
e.printStackTrace();
// cleanup
try {
tasks.get(0).stop();
} catch (Exception e2) {
e2.printStackTrace();
}
executor.shutdownNow();
}
}
static List<ChainedTask> buildTasks(String printChars, int[] repeatChars) {
List<ChainedTask> tasks = new ArrayList<ChainedTask>();
int maxTasks = printChars.length();
if (maxTasks != repeatChars.length) {
throw new IllegalArgumentException("Amount of repeats per pritn character must match amount of characters.");
}
for (int i = 0; i < maxTasks; i++) {
ChainedTask task = new ChainedTask(printChars.charAt(i), repeatChars[i]);
tasks.add(task);
if (i > 0) {
tasks.get(i - 1).setNextTask(task);
}
}
// make last task trigger first task - creates an endless loop.
tasks.get(maxTasks - 1).setNextTask(tasks.get(0));
tasks.get(maxTasks - 1).setLastTask(true);
return tasks;
}
static AtomicInteger chainedLoopsCount = new AtomicInteger();
static class ChainedTask implements Runnable {
// Semaphore to trigger execution
Semaphore performTask = new Semaphore(0);
// If stop is true, task must finish.
// stop must be volatile to ensure updated value is always visible.
volatile boolean stop = false;
// The last task is responsible for stopping execution
boolean lastTask;
// The next task to run after this task.
ChainedTask nextTask;
char printChar;
int repeatAmount;
ChainedTask(char printChar, int repeatAmount) {
this.printChar = printChar;
this.repeatAmount = repeatAmount;
System.out.println("Created " + printChar + " / " + repeatAmount);
}
void triggerPrintTask() {
performTask.release(repeatAmount);
}
void stop() {
// first indicate to stop
stop = true;
// then release a permit to pickup the stop sign.
performTask.release();
// also stop next task, unless this is the last task
if (!isLastTask()) {
getNextTask().stop();
}
}
#Override
public void run() {
try {
while (!stop) {
runTask();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Stopped " + printChar + " / " + repeatAmount);
}
void runTask() throws Exception {
// wait for our turn
performTask.acquire();
// must check 'stop' after getting permit, see the stop-method:
// first stop is set to true and then a permit is released.
if (stop) {
return;
}
// print text for loop-amount
do {
System.out.print(printChar);
} while (performTask.tryAcquire());
if (isLastTask()) {
System.out.println();
// check if we should stop
if (chainedLoopsCount.incrementAndGet() >= MAX_CHAINED_LOOPS) {
// since this is the last task, the next task is the first task.
// stopping the first task will call the stop-method on all tasks, including this one.
getNextTask().stop();
// signal main-thread we are done.
MAX_LOOPS_REACHED.countDown();
}
// Sleep for a long time to test what happens when last task hangs.
// Should trigger the "cleanup" code in the main method.
// Thread.sleep(10000);
}
// trigger next chained task to run
// this has no effect if next chained task was stopped
getNextTask().triggerPrintTask();
}
void setNextTask(ChainedTask nextTask) {
this.nextTask = nextTask;
}
ChainedTask getNextTask() {
return nextTask;
}
void setLastTask(boolean lastTask) {
this.lastTask = lastTask;
}
boolean isLastTask() {
return lastTask;
}
}
}
Semaphore – to solve Producer/Consumer problem
A high level explanation of semaphore.
A semaphore contains a count indicating whether a resource is locked or available. Semaphore is a signaling mechanism (“I am done, you can carry on.”). The resource itself may not be thread safe.
Producer
semObject.Post(); // Send the signal
Increase the semaphore count by 1. If a thread is waiting on the
specified semaphore, it is awakened.[1]
Consumer
semObject.Wait(); // Wait for the signal
When the semaphore count is zero, the thread calling this function
will wait for the semaphore. When the semaphore count is nonzero, the
count will be decremented by 1 and the thread calling this function
will continue.[1]
Reference
[1] Massa, Anthony J., Embedded software development with eCos, Pearson Education, Inc., 2002

How do I respawn threads if they die

I have multiple threads of multiple types (Different classes). I want in case one of them throws an exception and dies to be replaced by another NEW thread. I am aware of the join thread function but how would I go about implementing them for 5 different type of threads such as in case type 1 thread dies is instantly replaced without having to wait for type 2 to die first.
This is some sample pseudo-code.
class1 implements runnable{
void run(){
try{
while(true){
repeat task
}
} catch(Exception e){
log error
}
}
}
class2 implements runnable{
void run(){
try{
while(true){
repeat task
}
} catch(Exception e){
log error
}
}
}
class3 implements runnable{
void run(){
try{
while(true){
repeat task
}
} catch(Exception e){
log error
}
}
}
public main(){
// start all threads .start()
}
I want in case one of them throws an exception and dies to be replaced by another NEW thread.
I don't quite understand why you can't do:
public void run() {
// only quit the loop if the thread is interrupted
while (!Thread.currentThread().isInterrupted()) {
try {
// do some stuff that might throw
repeat task;
} catch (Exception e) {
// recover from the throw here but then continue running
}
}
}
Why do you need to restart a NEW thread? Just because a task threw an exception doesn't mean that it is somehow corrupt and it needs a fresh one to work appropriately. If you are trying to catch all exceptions (including RuntimeException) then catch (Exception e) will do this. If you want to be really careful you can even catch Throwable in case there is a chance that Errors are being generated – this is relatively rare.
If you actually have multiple tasks (or really anytime you are dealing with threads), you should consider using the ExecutorService classes. See the Java tutorial.
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// or you can create an open-ended thread pool
// ExecutorService threadPool = Executors.newCachedThreadPool();
// define your jobs somehow
threadPool.submit(new Class1());
threadPool.submit(new Class2());
...
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
So instead of forking a thread to do multiple tasks, you start a thread pool and it starts threads as necessary to accomplish a bunch of tasks. If a task fails, you could certain submit another task to the pool although that's a slightly strange pattern.
If you want to wait for all of the tasks to finish you'd use:
threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
boolean shouldStop() {
// it's a good idea to think about how/when to stop ;)
return false;
}
void runThreadGivenType(final Runnable taskToRun) {
new Thread() {
#Override
public void run() {
try {
taskToRun.run();
} finally {
if (!shouldStop()) {
runThreadGivenType(taskToRun);
}
}
}
}.start();
}
public void main(String[] args) throws Exception {
runThreadGivenType(new Runnable() { public void run() { System.out.println("I'm almost immortal thread!"); throw new RuntimeException(); } });
TimeUnit.SECONDS.sleep(10);
}
and it's a good idea to think about executors to manage thread pools too. plain, [un/hand]-managed threads are not the best practice ;)

Why does ExecutorService keep executing when threads are blocking?

I am trying to write a part of a multithreaded program where each thread from a fixed thread pool tries to fetch an object from a Queue and if the Queue is empty the thread waits.
The problem I am experiencing is that the memory used by the program keeps increasing.
public class Ex3 {
public static LinkedBlockingQueue<Integer> myLBQ = new LinkedBlockingQueue<Integer>(10);
public static void main(String argc[]) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(3);
myLBQ.add(new Integer(1));
for (;;) {
executor.execute(new MyHandler(myLBQ));
}
}
}
class MyHandler implements Runnable {
LinkedBlockingQueue<Integer> myLBQ;
MyHandler(LinkedBlockingQueue<Integer> myLBQ) {
this.myLBQ = myLBQ;
}
public void run() {
try {
myLBQ.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I don't understand why the executor.execute keeps firing when the threads should be waiting for an item to be added to the Queue. How do I modify my code to reflect this?
This adds tasks to the executor as fast as it can.
for (;;) {
executor.execute(new MyHandler(myLBQ));
}
This will consume about 200 MB per second. It doesn't have anything to do with whether there are tasks to perform or not.
If you don't want to do this I suggest you move the loop to the runnable and add only one. This will cause it to wait for tasks forever.
A better approach is to use the ExecutorService's builtin queue to queue tasks.
ExecutorService executor = Executors.newFixedThreadPool(3);
final int taskId = 1;
executor.submit(new Runnable() {
#Override
public void run() {
doSomething(taskId);
}
});
executor.shutdown();
This does the same thing, but is much simpler IMHO.
it's because you're creating a gazillion instances of MyHandler and inserting them in the internal queue of the executor.
That infinite for loop is quite mean.

Categories