Regarding daemon thread providing some service to non daemon thread

前端 未结 2 942
我在风中等你
我在风中等你 2020-12-22 05:41

I have one query that is I have developed a code below of multiple threads named thread one and thread two, below is the code ..

class multip implements Runnable {

2条回答
  •  天涯浪人
    2020-12-22 06:04

    To make t1 a daemon for example:

    Thread t1 = new Thread(obj);
    t1.setName("one");
    t1.setDaemon(true);
    t1.start();
    

    Note:

    • setDaemon needs to be called before the thread is started
    • t1.currentThread().setName("one"); most certainly does not do what you want. It sets the name of the main thread to "one", not the name of t1. See my code above for what you probably meant.
    • the information can easily be found in the javadoc of Thread which should be the first place you look at when you search that kind of information.

提交回复
热议问题