JAVA多线程问题【1】 ThreadFactory接口用法
JAVA多线程问题【1】 ThreadFactory接口用法
南栀安 发表于3个月前
JAVA多线程问题【1】 ThreadFactory接口用法
  • 发表于 3个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

今天看到有一篇博客介绍,百分之八十的多线程问题都是通过ThreadFactory来解决的。

在java中,我们通常使用两种方法即创建线程。线程实现runnable接口的类和扩展。Java还提供了一个接口,ThreadFactory接口,创建你自己的Thread对象的工厂。

根据需要创建新线程的对象。使用线程工厂就无需再手工编写对 new Thread 的调用了,从而允许应用程序使用特殊的线程子类、属性等等。

ThreadFactory的例子

Task.java

class Task implements Runnable  
{  
   @Override  
   public void run()  
   {  
      try  
      {  
         TimeUnit.SECONDS.sleep(2);  
      } catch (InterruptedException e)  
      {  
         e.printStackTrace();  
      }  
   }  
}  

TestMyThread.java

package mbmp.mbmp.display.bigScreen.webapp.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadFactory;


public class TestMyThread implements ThreadFactory{
	
	private int counter;  
    private String name;  
    private List<String> stats;  
    
    public TestMyThread(String name) {  
        counter = 0;  
        this.name = name;  
        stats = new ArrayList<String>();  
    }  
    
    public String getStas() {  
        StringBuffer buffer = new StringBuffer();  
        Iterator<String> it = stats.iterator();  
        while(it.hasNext()) {  
            buffer.append(it.next());  
            buffer.append("\n");  
        }  
        return buffer.toString();  
    }  
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestMyThread factory = new TestMyThread("MyThreadFactory");  
        Task task = new Task();  
        Thread thread = null;  
        for(int i = 0; i < 10; i++) {  
            thread = factory.newThread(task);  
            thread.start();  
        }  
        System.out.printf("Factory stats:\n");  
        System.out.printf("%s\n",factory.getStas());  
	}

	@Override
	public Thread newThread(Runnable run) {
		Thread t = new Thread(run, name + "-Thread-" + counter);  
        counter++;  
        stats.add(String.format("Created thread %d with name %s on%s\n" ,t.getId() ,t.getName() ,new Date()));  
        return t;
	}
}

console输出

Factory stats:
Created thread 9 with name MyThreadFactory-Thread-0 onMon Sep 04 18:12:11 CST 2017

Created thread 10 with name MyThreadFactory-Thread-1 onMon Sep 04 18:12:11 CST 2017

Created thread 11 with name MyThreadFactory-Thread-2 onMon Sep 04 18:12:11 CST 2017

Created thread 12 with name MyThreadFactory-Thread-3 onMon Sep 04 18:12:11 CST 2017

Created thread 13 with name MyThreadFactory-Thread-4 onMon Sep 04 18:12:11 CST 2017

Created thread 14 with name MyThreadFactory-Thread-5 onMon Sep 04 18:12:11 CST 2017

Created thread 15 with name MyThreadFactory-Thread-6 onMon Sep 04 18:12:11 CST 2017

Created thread 16 with name MyThreadFactory-Thread-7 onMon Sep 04 18:12:11 CST 2017

Created thread 17 with name MyThreadFactory-Thread-8 onMon Sep 04 18:12:11 CST 2017

Created thread 18 with name MyThreadFactory-Thread-9 onMon Sep 04 18:12:11 CST 2017


 

共有 人打赏支持
粉丝 2
博文 20
码字总数 5177
×
南栀安
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: