When does the main thread die?

The question was to generate random numbers between 1 and 99 using a stream. However, the problem here is that I don’t know where the “main stream” comes from? Does the main thread end at the end?

This is an example output:

Main thread stopping Random no = 57 Random no = 47 Random no = 96 Random no = 25 Random no = 74 Random no = 15 Random no = 46 Random no = 90 Random no = 52 Random no = 97 Thread that generates random nos is stopping 

Mythread Class:

 public class MyThread extends Thread { MyThread() { // default constructor } MyThread(String threadName) { super(threadName); // Initialize thread. start(); } public void run() { // System.out.println(Thread.currentThread().getName()); Random rand = new Random(); int newValue; for (int i = 0; i < 10; i++) { newValue = rand.nextInt(99);// generates any vale between 1 to 99 System.out.println("Random no = " + newValue); } System.out.println("Thread that generates random nos is stopping"); } } 

Main class:

 public class HW5ex2a { public static void main(String[] args) throws InterruptedException { MyThread t = new MyThread(); t.start(); t.join();// wait for the thread t to die System.out.println("Main thread stopping"); } } 
+7
java multithreading
source share
3 answers

You cannot rely on the main thread and other threads being written to System.out. Your thread t runs fine, the main thread waits for it to finish, and then the main thread exits, as expected. But this is not reflected in the order that you see on System.out.

To answer your question - the main thread is waiting for the thread to finish, then it writes a message to System.out, then it dies. The only confusing thing is that since you are writing System.out from two different threads, you have no guarantees regarding relative ordering. Println from two different threads may seem to be alternating in some way ... it just appears first when exiting the main thread.

As Alexis Leclerc points out, you get unpredictable rotation, the same thing as in this java threads tutorial .

+2
source share

Memory Synchronization in a Multi-Core System

Since both threads run on separate cores (most likely), objects to which the threads have access are cached by each core (in L1 cache) to improve performance.

Whenever the state of an object changes, all caches try to synchronize with the value in RAM, which is not guaranteed immediately.

Some threads can synchronize in front of other threads, even if they break the relationship going on before between them.

The reason for this is to implement the java memory model .


The same thing happens with the System.out object here.

You cannot guarantee the order in which data is transmitted and passed through it.

Therefore, this is unpredictable. Call

 System.out.flush(); 

can improve the result, but it does not guarantee.


For more information, see Multiple Threads Using System.out.println in Java

Hope this helps.

+1
source share

The output of the main stream in the following situation: -

  • Usually when the program ends.
  • If you are using System.exit(1) .
  • JVM failed.

Hope this helps you.

0
source share

All Articles