标签:
ThreadFactory:就像一个代理类。在创建一个线程的时候,通过实现ThreadFactory接口的类对象,就会监听到它,从而执行ThreadFactory的newThread(Runnable r)方法。把该线程传人方法中,你就可以在方法中对线程进行一些操作(如,给线程起名字,把线程封装到那个组,修改线程的优先级等等。)最后会把包装过的线程,放入jvm中去执行。
首先来构建线程封装类WorkThread,该类的功能主要是为了能够更好的管理线程而创建的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class WorkThread extends Thread { private Runnable target; //线程执行目标 private AtomicInteger counter; public WorkThread(Runnable target, AtomicInteger counter) { this .target = target; this .counter = counter; } @Override public void run() { try { target.run(); } finally { int c = counter.getAndDecrement(); System.out.println( "terminate no " + c + " Threads" ); } } } |
其次,来定义一个测试目标
1
2
3
4
5
6
7
8
|
public class WorkRunnable implements Runnable { @Override public void run() { System.out.println( "complete a task" ); } } |
实现线程工厂的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class WorkThreadFactory implements ThreadFactory{ private AtomicInteger atomicInteger = new AtomicInteger( 0 ); @Override public Thread newThread(Runnable r) { int c = atomicInteger.incrementAndGet(); System.out.println( "create no " + c + " Threads" ); return new WorkThread(r, atomicInteger); //通过计数器,可以更好的管理线程 } } |
测试线程工厂
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class TestThreadFactory { public static void main(String[] args) { //创建线程(并发)池,自动伸缩(自动条件线程池大小) ExecutorService es = Executors.newCachedThreadPool( new WorkThreadFactory()); //同时并发5个工作线程 es.execute( new WorkRunnable()); es.execute( new WorkRunnable()); es.execute( new WorkRunnable()); es.execute( new WorkRunnable()); es.execute( new WorkRunnable()); //指示当所有线程执行完毕后关闭线程池和工作线程,如果不调用此方法,jvm不会自动关闭 es.shutdown(); try { //等待线程执行完毕,不能超过2*60秒,配合shutDown es.awaitTermination( 2 * 60 , TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } } } |
线程工厂是一个接口,它用于根据需要创建新线程的对象。使用线程工厂就无需再手工编写对new Thread的调用了,从而允许应用程序使用特殊的线程子类、属性等等。
(相当于创建好了线程,分好了组,要用线程就直接在组里,执行要执行的线程)
static class GameThreadFactory implements ThreadFactory { // static final AtomicInteger poolNumber = new AtomicInteger(1); final ThreadGroup group; final AtomicInteger threadNumber = new AtomicInteger(1); final String namePrefix; public GameThreadFactory(ExecutorType poolType) { SecurityManager s = System.getSecurityManager(); //获得系统安全接口 group = (s != null)? s.getThreadGroup() : //在接口中获得线程的分组 Thread.currentThread().getThreadGroup(); //获得当前线程所在组 namePrefix = "pool-" + poolType.toString()+ "-thread-"; } @Override 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; } }
ThreadPoolExecutor t = new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),new GameThreadFactory(etype)); //把它配置到线程池中管理.
使用:
t.execute(new Runnable() { @Override public void run() { try{ hePlayers_UID_BINDING_AND_RESET_PWD_STATUS.put(pvo.getUid(),map); }
}
});
标签:
原文地址:http://www.cnblogs.com/sanhuan/p/4910758.html