How can thread run after main method closes?

后端 未结 6 1841
借酒劲吻你
借酒劲吻你 2021-02-13 22:41

Here are my two classes:

public class Firstclass {
    public static void main(String args[]) throws InterruptedException {
        System.out.println(\"Main sta         


        
相关标签:
6条回答
  • 2021-02-13 23:11

    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).

    0 讨论(0)
  • 2021-02-13 23:13

    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.

    0 讨论(0)
  • 2021-02-13 23:14

    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.

    0 讨论(0)
  • 2021-02-13 23:16

    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.
    0 讨论(0)
  • 2021-02-13 23:27

    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.

    0 讨论(0)
  • 2021-02-13 23:29

    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

    0 讨论(0)
提交回复
热议问题