Home > OS >  Thread Join ScheduledExecutorService not working as expected
Thread Join ScheduledExecutorService not working as expected

Time:02-01

In the below code snippet, I notice the following sequence of execution. Why is the control going back to the flow outside the someMethod() before completing the activity in the someTask block? Shouldn't it complete everything inside thread2 before moving to the next line of code as thread2.join() has been invoked?

System.out.prinltn("inside someMethod...");
System.out.println("after thread2 .....");
System.out.prinltn("inside someTask......");
public static void main(String args[]) {
        Thread thread1 = new Thread(new someclassimplementingRunnable());
        thread1.start();
        try {
            thread1.join();
            Thread thread2 = new Thread(new Runnable() {
                public void run() {
                    someMethod();

                }
            });
            thread2.start();
            thread2.join();
            System.out.println("after thread2 .....");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("outside try-catch");
    }
public static synchronized void someMethod(){
System.out.prinltn("inside someMethod...");
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Runnable someTask = () -> {
System.out.prinltn("inside someTask......");
};
executor.scheduleAtFixedRate(someTask , 0, 2, TimeUnit.SECONDS);
}

CodePudding user response:

it is working as expected. you're just expecting the wrong outcome ;)

Thread2 finishes it's work once the executor starts working, and frankly, its a miracle you get to see "inside someTask" at all, as the executor and it tasks will be garbage collected once "someMethod" finishes working. (i.e. the Executor doesn't stay alive outside of someMethod, and there is no blocking operation at the end of someMethod that waits for the executor to finish).

i would even wager that if you run this code enough times, there will be instances when you won't see the line "inside someTask" because of random OS/process scheduling.

there is no line that makes "someMethod" wait, and so Thread2 finishes its work and it passes .join() and you see the print out "after thread2".

also, remember that the Console is not a synchronic output service. when multiple threads send output to the console, its not guaranteed to appear in the same order. so it is not a good indication of the order of events in a multithreaded environment.

CodePudding user response:

I found the solution and actually its pretty simple, checking the condition while(!executor.isTerminated()) does the job. It ensures the next statements are executed only after the executor has done its job.

  • Related