Regarding daemon thread providing some service to non daemon thread

筅森魡賤 提交于 2019-12-18 09:39:47

问题


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 {

public void run() {
    for (int i = 0; i < 20; i++) {
        try {


        Thread.sleep(500);
                System.out.println(Thread.currentThread().getName());
                System.out.println("i");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

public class MultiThread3 {
    public static void main(String... a) {
        multip obj = new multip();
        Thread t1 = new Thread(obj);
        t1.currentThread().setName("one");
        t1.start();
        Thread t2 = new Thread(obj);
        t2.currentThread().setName("two");
        t2.start();

    }

}

Now I want thread two be set as daemon thread and it should provide some service to thread one it could be any kind of service but at last What I was trying to achieve is that a daemon thread providing some service to non daemon thread. Please advise how to achieve this.The question is about of how the daemon thread will provide the service to non daemon thread..?


回答1:


The question is about of how the daemon thread will provide the service to non daemon thread

I would use an executor service. If you want to return a value from the daemon thread, you can use a Callable<ReturnType> instead of a Runnable.

// creating a thread pool.
ExecutorService service = Executors.newSingleThreadExecutor(new ThreadFactory() {
    @Override
    public Thread newThread(Runnable r) {
        // creating a thread.
        Thread two = new Thread(r, "two");
        // making it a daemon thread.
        two.setDaemon(true);
        return two;
    }
});

for(int i=0;i<10;i++)
    // creating a task and submitting it.
    service.submit(new Runnable() {
        @Override
        public void run() {
            System.out.println("["+Thread.currentThread().getName()+"] - Hello World.");
            Thread.yield();
        }
    });
service.shutdown();

prints

[two] - Hello World.
[two] - Hello World.
[two] - Hello World.

First it creates a thread pool with a work queue. The thread pool has a factor which creates threads, in this case with a given name which is a daemon.

Secondly there is a loop which add 10 tasks to the queue for the executors thread(s) to execute.

Finally it stops the service when it has finished with it (this is rarely needed)




回答2:


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.


来源:https://stackoverflow.com/questions/11665729/regarding-daemon-thread-providing-some-service-to-non-daemon-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!