[java] Java Multithreading concept and join() method

First rule of threading - "Threading is fun"...

I'm not able to understand the flow of execution of the program, And when ob1 is created then the constructor is called where t.start() is written but still run() method is not executed rather main() method continues execution. So why is this happening?

This is exactly what should happen. When you call Thread#start, the thread is created and schedule for execution, it might happen immediately (or close enough to it), it might not. It comes down to the thread scheduler.

This comes down to how the thread execution is scheduled and what else is going on in the system. Typically, each thread will be given a small amount of time to execute before it is put back to "sleep" and another thread is allowed to execute (obviously in multiple processor environments, more than one thread can be running at time, but let's try and keep it simple ;))

Threads may also yield execution, allow other threads in the system to have chance to execute.

You could try

NewThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
    // Yield here
    Thread.yield();
}

And it might make a difference to the way the threads run...equally, you could sleep for a small period of time, but this could cause your thread to be overlooked for execution for a period of cycles (sometimes you want this, sometimes you don't)...

join() method is used to wait until the thread on which it is called does not terminates, but here in output we see alternate outputs of the thread why??

The way you've stated the question is wrong...join will wait for the Thread it is called on to die before returning. For example, if you depending on the result of a Thread, you could use join to know when the Thread has ended before trying to retrieve it's result.

Equally, you could poll the thread, but this will eat CPU cycles that could be better used by the Thread instead...

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to multithreading

How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Waiting until the task finishes What is the difference between Task.Run() and Task.Factory.StartNew() Why is setState in reactjs Async instead of Sync? What exactly is std::atomic? Calling async method on button click WAITING at sun.misc.Unsafe.park(Native Method) How to use background thread in swift? What is the use of static synchronized method in java? Locking pattern for proper use of .NET MemoryCache

Examples related to synchronized

Java Multithreading concept and join() method Difference between volatile and synchronized in Java Java synchronized method lock on object, or method? What does 'synchronized' mean? Is there an advantage to use a Synchronized Method instead of a Synchronized Block? Avoid synchronized(this) in Java?

Examples related to pthread-join

Java Multithreading concept and join() method