Here are my two classes:
public class Firstclass {
public static void main(String args[]) throws InterruptedException {
System.out.println(\"Main sta
join If you have a thread B that can't do its work until another thread A has completed its work, then you want thread B to "join" thread A.
synchronized When we use threads, we usually need to use some synchronization somewhere to make sure our methods don't interrupt each other at the wrong time and mess up our data. Generally, any time more than one thread is accessing mutable (changeable) data, you synchronize to protect that data, to make sure two threads aren't changing it at the same time (or that one isn't changing it at the same time the other is reading it, which is also confusing).
I want to know that even though the main method is closed How are both threads still running?
Your main()
method was run by a separate thread, which start another two threads (First
ad Second
). All Threads are not depend on each other, so, main thread can print the line below the starting line of other threads.
My second question is can anybody explain me what is the difference between join method and synchronized?
join()
means waiting for a thread to complete. This is a blocker method. synchronized is a keyword to denote that, multiple thread can't access a synchronized block/method synchronously.
I want to know that even though the main method is closed How are both threads still running?
The JVM will exit once the last non-jvm thread terminates. this means that if any of the threads you create is still running, the jvm will not shutdown. daemon threads are threads that do not prevent the JVM from shutting down. normally you'd use them for some background tasks which you dont want keeping your application up if the user requested it to shut down.
A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
You can use the setDaemon()
method to change the Thread daemon properties. By default, every thread that a user create is a normal (non-daemon) thread, unless you explicitly call setDaemon()
method.
explain me what is the difference between join method and synchronized
Synchronization is a locking mechanism that allows two threads to not step on each other i.e. synchronization is used to provide proper access to shared resources by multiple threads with the help of locking mechanism.
On the other hand join()
method call allows one thread to wait for the completion of another thread.
Your main
thread is not closed -
// ...
System.out.println("Main close...");
// <--- Your main method is here while all the other threads complete (sort of).
}
On part 2 of your question - There is no connection between join
and synchronized
. They are almost opposite.
join
- Wait for the thread to complete before resuming.synchronized
- Only one thread can enter here, all others must wait.Your first question,
When main method is called by default one main thread is created. And main thread is a non-dameon thread. When threads created by the main method it inherits it's parant's property. That means they are all non-daemon threads. As you know JVM waits until all non-daemon threads to complete. So it will executes even after the main thread completes.
See here : daemon vs non-daemon Java Stack for each thread creation
and your second question: join method joins the currently running thread at the end of the thread which is calling the join method. That means the current thread will be stopped and it start after the thread referenced by the join method.
Synchronization stops two threads executing the same code at the same time.
There are two types of threads user and daemon. So if you want your program to exit make your thread a daemon.
Example
Thread t = new Thread();
t.setDaemon(true);
The process terminates when there are no more user threads.
To your second Question:
Join is to be used only when you need to ensure that the thread dies & you have nothing to do after that
Synchronization is for inter-thread-communication