Here are my two classes:
public class Firstclass {
public static void main(String args[]) throws InterruptedException {
System.out.println(\"Main sta
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.