ThreadFactory
生活随笔
收集整理的這篇文章主要介紹了
ThreadFactory
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
根據需要創建新線程的對象。使用線程工廠就無需再手工編寫對?new Thread?的調用了,從而允許應用程序使用特殊的線程子類、屬性等等。 JDK中的介紹: class?SimpleThreadFactory?implements?ThreadFactory?{?? ??public?Thread?newThread(Runnable?r)?{?? ????return?new?Thread(r);?? ??}?? }?? /**? ?????*?The?default?thread?factory? ?????*/?? ????static?class?DefaultThreadFactory?implements?ThreadFactory?{?? ????????static?final?AtomicInteger?poolNumber?=?new?AtomicInteger(1);?? ????????final?ThreadGroup?group;?? ????????final?AtomicInteger?threadNumber?=?new?AtomicInteger(1);?? ????????final?String?namePrefix;?? ?? ????????DefaultThreadFactory()?{?? ????????????SecurityManager?s?=?System.getSecurityManager();?? ????????????group?=?(s?!=?null)??s.getThreadGroup()?:?? ?????????????????????????????????Thread.currentThread().getThreadGroup();?? ????????????namePrefix?=?"pool-"?+?? ??????????????????????????poolNumber.getAndIncrement()?+?? ?????????????????????????"-thread-";?? ????????}?? ?? ????????public?Thread?newThread(Runnable?r)?{?? ????????????Thread?t?=?new?Thread(group,?r,?? ??????????????????????????????????namePrefix?+?threadNumber.getAndIncrement(),?? ??????????????????????????????????0);?? ????????????if?(t.isDaemon())?? ????????????????t.setDaemon(false);?? ????????????if?(t.getPriority()?!=?Thread.NORM_PRIORITY)?? ????????????????t.setPriority(Thread.NORM_PRIORITY);?? ????????????return?t;?? ????????}?? ????}??
下面寫一簡單示例。 [java]?view plaincopy print?package?com.test;?? ?? import?java.util.concurrent.ExecutorService;?? import?java.util.concurrent.Executors;?? import?java.util.concurrent.ThreadFactory;?? ?? class?Task?implements?Runnable{?? ????int?taskId;?? ????public?Task(int?taskId)?{?? ????????this.taskId=taskId;?? ????}?? ?????? ????@Override?? ????public?void?run()?{?? ????????System.out.println(Thread.currentThread().getName()+"--taskId:?"+taskId);?? ?????????? ????}?? }?? ?? class?DaemonThreadFactory?implements?ThreadFactory?{?? ????@Override?? ????public?Thread?newThread(Runnable?r)?{?? ????????Thread?t=new?Thread(r);?? ????????t.setDaemon(true);?? ????????return?t;?? ????}?? ?????? }?? public?class?ThreadFactoryTest?{?? ????public?static?void?main(String[]?args)?{?? ????????ExecutorService?exec=Executors.newFixedThreadPool(3,new?DaemonThreadFactory());?? ????????for(int?i=0;i<3;i++)?{?? ????????????exec.submit(new?Task(i));?? ????????}?? ????????exec.shutdown();?? ????}?? }??
輸出如下: Thread-0--taskId: 0
Thread-1--taskId: 1
Thread-2--taskId: 2 分析: DaemonThreadFactory中覆寫的newThread()方法與submit()方法的調用關系,也就是說DaemonThreadFactory是如何起作用的。 調試輸出其調用關系: 也就是說,submit()時會調用DaemonThreadFactory類的newThread()方法來創建線程。
An object that creates new threads on demand. Using thread factories removes hardwiring of calls tonew Thread, enabling applications to use special thread subclasses, priorities, etc.
The simplest implementation of this interface is just:
?
[java]?view plaincopy print??
The?Executors.defaultThreadFactory?method provides a more useful simple implementation, that sets the created thread context to known values before returning it.? [java]?view plaincopy print?下面寫一簡單示例。 [java]?view plaincopy print?
輸出如下: Thread-0--taskId: 0
Thread-1--taskId: 1
Thread-2--taskId: 2 分析: DaemonThreadFactory中覆寫的newThread()方法與submit()方法的調用關系,也就是說DaemonThreadFactory是如何起作用的。 調試輸出其調用關系: 也就是說,submit()時會調用DaemonThreadFactory類的newThread()方法來創建線程。
轉載于:https://www.cnblogs.com/yaowen/p/6069628.html
總結
以上是生活随笔為你收集整理的ThreadFactory的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyQ1_介绍
- 下一篇: 拦截导弹 (加了神奇的位运算)