Junit test hangs up on my async thread - java

I am fork a new thread on my service's #Postconstruct method, and in the new thread, a infinite loop is running.
My test is just invoke the service using spring mvc test:
ResultActions result = this.mockMvc.perform(post("/test").with(httpBasic(user, pwd)).contentType("application/json").content(test))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
And the test just hangs there, waiting for the infinite loop thread to stop. but when the service is started normally, the test is fine. Any idea why? And how to fix it.
here is the code in my service java:
#Postconstruct
private void init() {
invoke();
}
private void invoke() {
Runnable task = () -> {
while(true) { ... }
}
Thread t;
for(int i=0; i<3; i++) {
t = new Thread(task);
t.setName("test-" + i);
t.start();
}
}

Suggestion: step back from using "bare metal" Threads. There are nice abstraction concepts, like ExecutorService and things like Futures and Promises.
The point is: one can use dependency injection to provide such an ExecutorService to the production code; and then you can define your own service ... that does everything on the same thread.
Meaning: avoid unit tests that deal with multiple threads - as that often leads to additional waiting, or "flakiness" as you never now exactly how long your test will be running.

Take a look at this example (with a debugger)
Notice that the main will not be exited until all the threads are done.
You can easily do this with putting a breakpoint on a while (run) and change the value of run slowly. Once you turn off all 3 threads, then your main thread will exit.
class Test {
public static void main(String[] args) {
Runnable task = () -> {
boolean run = true;
while (run) {
System.out.println("running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Thread t = new Thread(task);
t.setName("test-" + i);
t.start();
threads.add(t);
}
}
}
Another way to do it is using join, which will explicitly wait for the threads to finish before the program can complete.
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
While there are any active threads running the program will not finish.

Related

How to completely stop / terminate a task that has already been submitted to an ExecutorService in Java?

Problem : I have an use case where I want to cancel a task that has already been submitted to an executor service. future.cancel() is not helpful to me as the task does not go to wait() / sleep() state during the execution. Also, adding isInterrupted() is not scalable because of the following reasons,
Many other services are called during the execution and using isInterrupted() before each call is ugly.
If suppose one of the service calls in one of the submitted tasks takes more than X milliseconds, I would want to abort the task and free up the tread.
Here is a sample code on how I am using future.cancel() right now. Is there a way where I can completely abort the submitted task / kill the thread executing the task in the main() function without disturbing the other submitted tasks.
public class Main {
ExecutorService executorService = newFixedThreadPool(10);
public static void main(String[] args) {
Future<Integer> test = new Main().sample();
try {
test.get(0, java.util.concurrent.TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
System.out.println("Throwing InterruptedException");
} catch (java.util.concurrent.ExecutionException e) {
System.out.println("Throwing ExecutionException");
} catch (java.util.concurrent.TimeoutException e) {
System.out.println("Throwing TimeoutException");
} finally {
System.out.println(test.cancel(true));
}
System.out.println("main() COMPLETED");
}
private Future<Integer> sample() {
return executorService.submit(() -> {
System.out.println("sample() STARTED");
anotherSample();
System.out.println("sample() COMPLETED");
return 1;
});
}
private void anotherSample() throws Exception {
System.out.println("anotherSample() STARTED");
for (int i = 0; i < 100000; i++) {
// do nothing
}
System.out.println("anotherSample() COMPLETED");
}
}
Output :
Throwing TimeoutException
sample() STARTED
anotherSample() STARTED
true
main() COMPLETED
anotherSample() COMPLETED
sample() COMPLETED

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()

Java execute 3 methods at same time inside custom method and grab values from run methods

I would like to execute 3 methods at same time in Java (obviously I need threads), and that I would like to execute not in separate class and not in my main method, but in my custom method. Can it be done?
I have find this piece of code - Execute Multiple Methods Simaltaneously Using Thread In Java
and reused best marked answer for my example, with parameters that I have:
private void fetchData() {
boolean t1_run = true;
boolean t2_run = true;
boolean t3_run = true;
int SLEEP_TIME = 100;//Configurable.
Thread thread1 = new Thread() {
public void run() {
while (t1_run)
{
try
{
subjects = new BeanItemContainer<KltSubject>(KltSubject.class, clijentService.getSubjecteByType(Integer.valueOf(creditor)));
Thread.sleep(SLEEP_TIME);//So that other thread also get the chance to execute.
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
};
Thread thread2 = new Thread() {
public void run() {
while (t2_run)
{
try
{
programs = new BeanItemContainer<Program>(Program.class, creditService.getAllPrograms());
Thread.sleep(SLEEP_TIME);//So that other thread also get the chance to execute.
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
};
Thread thread3 = new Thread() {
public void run() {
while (t3_run)
{
try
{
credits = new BeanItemContainer<CreditExt>(CreditExt.class, creditService.getAllCredits());
Thread.sleep(SLEEP_TIME);//So that other thread also get the chance to execute.
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
};
thread1.start();
thread2.start();
thread3.start();
}
Now, before I've put my variables in threads (variables named: subjects, programs and credits), I could easily get their values (these variables you can see in above code in this example in their run methods, but are defined outside my fetchData() method and are visible).
After setting code like this, and executing it, I recieve null pointer exception because obviously varaibles are not seen any more after threads are executed. How to get that values after execution in threads?
P.S. can this code be written more elegantly, with less lines of code? If Java 8 (or Java 7) can done it - show me please how?
Use advanced Threading API : ExecutorService invokeAll()
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.
Sample code:
ExecutorService service = Executors.newFixedThreadPool(3);
List<MyCallable> futureList = new ArrayList<MyCallable>();
MyCallable1 myCallable1 = new MyCallable1(); // your first thread
MyCallable2 myCallable2 = new MyCallable2(); // your second thread
MyCallable3 myCallable1 = new MyCallable3(); // your third thread
futureList.add(myCallable1);
futureList.add(myCallable2);
futureList.add(myCallable3);
System.out.println("Start");
try{
List<Future<Long>> futures = service.invokeAll(futureList);
for(Future<Long> future : futures){
try{
System.out.println("future.isDone = " + future.isDone());
System.out.println("future: call ="+future.get());
}
catch(Exception err1){
err1.printStackTrace();
}
}
}catch(Exception err){
err.printStackTrace();
}
service.shutdown();
You can have a look into few more examples in this article
You can use join() to wait for the threads to finish.
eg:
thread1.join();
thread2.join();
thread3.join();
when all the threads are done, the variables shouldn't be null.
Making this more compact depends on what exactly you're trying to do. Here is an example:
private List<Result> fetchData() {
final List<Result> results = new ArrayList<Result>();
List<Thread> threads = new ArrayList<Thread>();
for(int i = 0; i < 3; i++){
Thread t = new Thread() {
public void run() {
Result result = getResult();
synchronized(results) {
results.add(result);
}
}
};
t.start();
threads.add(t);
}
for(Thread t:threads) {
t.join();
}
return results;
}
You can simply use a CompletableFuture:
CompletableFuture<BeanItemContainer<KltSubject>> job1 = CompletableFuture.supplyAsync(() ->
new BeanItemContainer<>(KltSubject.class, clijentService.getSubjecteByType(creditor)));
CompletableFuture<BeanItemContainer<Program>> job2 = CompletableFuture.supplyAsync(() ->
new BeanItemContainer<>(Program.class, creditService.getAllPrograms()));
CompletableFuture<BeanItemContainer<CreditExt>> job3 = CompletableFuture.supplyAsync(() ->
new BeanItemContainer<>(CreditExt.class, creditService.getAllCredits()));
subjects = job1.join();
programs = job2.join();
credits = job3.join();
The method supplyAsync will initiate the asynchronous computation of a value and join will return the computed value, waiting, if necessary. But if all three actions imply querying the same database, it might be possible, that you don’t gain any performance advantage as the database may be the limiting factor.

How to terminate callable process called by ExecutorService

In the following code I want to terminate the Callable process submitted by ExecutorService. Currently the execution of the callable process is not terminating even though the shutdown called before the loop execution.
Any suggestion would be helpful.
package foundation.util.sql.parser;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.*;
public class Test {
public static void main(String[] args) {
try {
final java.util.Map<String, ExecutorService> map = new HashMap<>();
ExecutorService service = Executors.newFixedThreadPool(1);
map.put("1", service);
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
System.out.println("Termination Initiated");
ExecutorService executorService = map.get("1");
System.out.println("ShutDown called");
if(!executorService.isShutdown())
{
executorService.shutdownNow();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
Future<Boolean> submit = service.submit(new Callable<Boolean>() {
#Override
public Boolean call() throws Exception {
int j = 0;
System.out.println(Thread.currentThread().getName());
for (int i=0; i<5000;i++) {
//Some business Process.
j = i;
}
System.out.println("Test____"+ j);
return null;
}
});
thread.start();
submit.get();
} catch (Exception e) {
e.printStackTrace();
}
}
}
When we call showDownNow() it doesn't terminate the running tasks, in fact
it just prevents waiting tasks from starting and attempts to stop currently executing tasks.
As per javadoc
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
In your callable you are not responding/checking for the interrupts. You need check periodically if the interrupt flag is set to true. If so, do the necessary clean up if needed and terminate.
As an example, in your case you can consider checking the interrupt flag as below (or wherever applicable):
for (int i=0; i<5000;i++) {
//Some business Process.
if(Thread.currentThread().isInterrupted()) {
// do any cleanup and return from here.
return false;
}
j = i;
}

Java Multithreading doesn't seem to be correctly working

I have a class which processes something. I'm trying to run a number of instances of this class in parallel.
However, I'm not sure if in TaskManager.startAll(), when I call r.go(), whether this would cause r to start running in its own thread, or within the main thread?
The total execution time that I'm getting seems to be very high, and despite my attempts at optimizing, nothing seems to be having any effect. Also, if I run a profiler on my project in Netbeans, it shows all the threads as sleeping. So I'd like to know if I'm doing something wrong?
This is the structure of the class:
public class TaskRunner implements Runnable {
private boolean isRunning = false;
public void run() {
while(true) {
while (! running) {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
process();
}
}
public void go() {
isRunning = true;
}
public void stop() {
isRunning = false;
}
private void process() {
//Do some number crunching and processing here
}
}
Here's how these are being run / managed:
public class TaskManager {
private ArrayList<TaskRunner> runners = new ArrayList<>();
public TaskManager() {
for (int i = 0; i < 10; i++) {
TaskRunner r = new TaskRunner();
new Thread(r).start();
runners.add(r);
}
}
public void startAll() {
for (TaskRunner r : runners) {
r.go();
}
}
}
Indeed, you are not "doing it right." If you want to create a multi-threaded Java application, the place to start is with the java.util.concurrent package.
It appears from your code that you want to run ten tasks in parallel. I assume that after "number crunching and processing," you'll want to aggregate the results and do something with them in the main thread. For this, the invokeAll() method of ExecutorService works well.
First, implement Callable to do the work you show in your process() method.
final class YourTask implements Callable<YourResults> {
private final YourInput input;
YourTask(YourInput input) {
this.input = input;
}
#Override
public YourResults call()
throws Exception
{
/* Do some number crunching and processing here. */
return new YourResults(...);
}
}
Then create your tasks and run them. This would take the place of your main() method:
Collection<Callable<YourResults>> tasks = new List<>(inputs.size());
for (YourInput i : inputs)
tasks.add(new YourTask(i));
ExecutorService workers = Executors.newFixedThreadPool(10);
/* The next call blocks while the worker threads complete all tasks. */
List<Future<YourResult>> results = workers.invokeAll(tasks);
workers.shutdown();
for (Future<YourResult> f : results) {
YourResult r = f.get();
/* Do whatever it is you do with the results. */
...
}
However, I'm not sure if in TaskManager.startAll(), when I call r.go(), whether this would cause r to start running in its own thread, or within the main thread?
So my first comment is that you should make isRunning be volatile since it is being shared between threads. If the threads are not starting when it goes to true (or seem to be delayed in starting) then I suspect that's your problem. volatile provides memory synchronization between the threads so the thread that calls go() and makes a change to isRunning will be seen immediately by the thread waiting for the change.
Instead of spinning like this, I would use wait/notify:
// this synchronizes on the instance of `TaskRunner`
synchronized (this) {
// always do your wait in a while loop to protect against spurious wakeups
while (!isRunning && !Thread.currentThread().isInterrupted()) {
try {
// wait until the notify is called on this object
this.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
Then in the go() method you should do the following. stop() would be similar.
public void go() {
synchronized (this) {
isRunning = true;
this.notifyAll();
}
}
Notice that you should handle thread interrupts carefully. Test for isInterrupted() in the while running loop and re-interrupt a thread when InterruptedException is thrown is always a good pattern.
The total execution time that I'm getting seems to be very high, and despite my attempts at optimizing, nothing seems to be having any effect. Also, if I run a profiler on my project in Netbeans, it shows all the threads as sleeping.
So although the threads are mostly sleeping, they are still each looping 1000 times a second because of your Thread.sleep(1). If you increased the time sleeping (after making isRunning be volatile) they would loop less but the right mechanism is to use the wait/notify to signal the thread.
Awful solution, terrible. first I highly recommend you start reading some tutorial like [this]
Second, if threads should wait for a signal to go for some job, so why just don't you wait them!!!!!, something like this
import java.util.ArrayList;
public class TaskManager
{
//////////////////////
public volatile static Signal wait=new Signal();
//////////////////////
private ArrayList<TaskRunner> runners = new ArrayList<>();
public TaskManager()
{
for (int i = 0; i < 10; i++)
{
TaskRunner r = new TaskRunner();
new Thread(r).start();
runners.add(r);
}
try {
Thread.sleep(1000);
startAll();
Thread.sleep(1000);
pauseAll();
Thread.sleep(1000);
startAll();
Thread.sleep(1000);
haltAll();System.out.println("DONE!");
}catch(Exception ex){}
}
public void startAll()
{
synchronized(wait){
wait.setRun(true);;
wait.notifyAll();
}
}
public void pauseAll(){
wait.setRun(false);
}
public void haltAll(){
for(TaskRunner tx:runners){tx.halt();}
}
public static void main(String[] args) {
new TaskManager();
}
}
class TaskRunner implements Runnable
{
private Thread thisThread;
private volatile boolean run=true;
public void run()
{
thisThread=Thread.currentThread();
while(run){
if(!TaskManager.wait.isRun()){
synchronized(TaskManager.wait)
{
if(!TaskManager.wait.isRun()){
System.out.println("Wait!...");
try
{
TaskManager.wait.wait();
}
catch (Exception e)
{
e.printStackTrace();
break;
}
}
}}
process();
}
}
private double r=Math.random();
private void process(){System.out.println(r);try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}}
public void halt(){run=false;thisThread.interrupt();}
}
class Signal{
private boolean run=false;
public boolean isRun() {
return run;
}
public void setRun(boolean run) {
this.run = run;
}
}
in above sample, all runners works till the Signal run boolean is true, and simple TaskManager class set tit as false for every time it needs to pause the threads. and about the halt, it just set the shutdown(run) flag to false, and also interrupt the thread because of if thread is in wait state.
I hope I could prove your solution is like dream-on story, and also could explained enough about my solution.
have a good parallel application :)

Categories