How can thread run after main method closes?

后端 未结 6 1829
借酒劲吻你
借酒劲吻你 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: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

提交回复
热议问题