Java使用ThreadFactory来创建新的线程

首先来构建线程封装类WorkThread,该类的功能主要是为了能够更好的管理线程而创建的


package com.bird.concursey;


import java.util.concurrent.atomic.AtomicInteger;


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");  
            }  
        }  
}


其次,来定义一个测试目标

package com.bird.concursey;
public class WorkRunnable implements Runnable
{
    @Override
    public void run() {
         System.out.println("complete a task");
    }


}


实现线程工厂的方法

package com.bird.concursey;


import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;


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);//通过计数器,可以更好的管理线程
    }


}

测试线程工厂

package com.bird.concursey;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


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();  
         }  
    }
    
}


阅读更多
文章标签: 线程
个人分类: 多线程
上一篇Spring线程池开发实战
下一篇死锁的三种形式:一般死锁,嵌套管程锁死,重入锁死
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭