本文介绍了关于为非守护程序线程提供一些服务的守护程序线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个查询,我已经开发了一个名为thread one和thread two的多个线程的代码,下面是代码.. 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自动生成的catch块 e.printStackTrace(); } } } } 公共类MultiThread3 { public static void main(String ... a){ multip obj = new multip(); 线程t1 =新线程(obj); t1.currentThread()。setName(one); t1.start(); 线程t2 =新线程(obj); t2.currentThread()。setName(two); t2.start(); } } 现在我想要线程二被设置为守护线程,它应该为线程提供一些服务,它可以是任何类型的服务但最后我想要实现的是一个守护线程为非守护线程提供一些服务。请告知如何实现这一点。问题是关于守护程序线程如何为非守护程序线程提供服务..?解决方案 问题是关于守护程序线程如何向非守护程序线程提供服务 I会使用遗嘱执行人服务。如果要从守护程序线程返回值,可以使用 Callable< ReturnType> 而不是 Runnable 。 //创建一个线程池。 ExecutorService service = Executors.newSingleThreadExecutor(new ThreadFactory(){ @Override public Thread newThread(Runnable r){ //创建一个线程。线程二= new Thread(r,two); //使其成为守护线程。 two.setDaemon(true); 返回2; } }); for(int i = 0; i< 10; i ++) //创建任务并提交它。 service.submit(new Runnable(){ @Override public void run(){ System.out.println([+ Thread.currentThread()。getName ()+] - Hello World。); Thread.yield(); } }); service.shutdown(); 打印 [two] - Hello World。 [two] - Hello World。 [two] - Hello World。 首先,它创建一个带有工作队列的线程池。线程池有一个创建线程的因子,在这种情况下使用给定的名称作为守护进程。 其次有一个循环,它将10个任务添加到队列中要执行的执行程序线程。 最后它在完成服务后停止服务(这很少需要) 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..? 解决方案 The question is about of how the daemon thread will provide the service to non daemon threadI 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) 这篇关于关于为非守护程序线程提供一些服务的守护程序线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 05:52