工厂设计模式是Java中最常用的设计模式之一。它是一种创建型设计模式,能够用于创建一个或多个类所需要的对象。有了这个工厂,我们就能集中的创建对象。

 

集中创建方式给我们带来了一些好处,例如:
1. 能够很容易的改变类创建的对象或者创建对象的方式;
2. 能够很容易限制对象的创建,例如:我们只能为a类创建N个对象;
3. 能够很容易的生成有关对象创建的统计数据。

 

在Java中,我们通常使用两种方式来创建线程:继承Thread类和实现Runnable接口。Java还提供了一个接口,既ThreadFactory接口,用于创建你自己的线程对象工厂。

 

很多类中,例如:ThreadPoolExecutor,使用构造函数来接收ThreadFactory来作为参数。这个工厂参数将会在程序执行时创建新的线程。使用ThreadFactory,你能够自定义执行程序如何创建线程,例如为线程定义适当的名称、优先级,或者你甚至可以将它设定为守护线程。

 

ThreadFactory例子

在这个例子中,我们将学习如何通过实现一个ThreadFactory接口来创建一个有个性化名称的线程对象,同时,我们保存了线程对象的创建信息。

 

Task.java

 

 

1. class Task implements Runnable  
2. {  
3.    @Override  
4.    public void run()  
5.    {  
6.       try  
7.       {  
8.          TimeUnit.SECONDS.sleep(2);  
9.       } catch (InterruptedException e)  
10.       {  
11.          e.printStackTrace();  
12.       }  
13.    }  
14. }

CustomThreadFactory.java

 

 

1. <span style="font-size:18px;">public class CustomThreadFactory implements ThreadFactory  
2. {  
3.    private int          counter;  
4.    private String       name;  
5.    private List<String> stats;  
6.    
7.    public CustomThreadFactory(String name)  
8.    {  
9.       counter = 1;  
10.       this.name = name;  
11.       stats = new ArrayList<String>();  
12.    }  
13.    
14.    @Override  
15.    public Thread newThread(Runnable runnable)  
16.    {  
17.       Thread t = new Thread(runnable, name + "-Thread_" + counter);  
18.       counter++;  
19.       stats.add(String.format("Created thread %d with name %s on %s \n", t.getId(), t.getName(), new Date()));  
20.       return t;  
21.    }  
22.    
23.    public String getStats()  
24.    {  
25.       StringBuffer buffer = new StringBuffer();  
26.       Iterator<String> it = stats.iterator();  
27.       while (it.hasNext())  
28.       {  
29.          buffer.append(it.next());  
30.       }  
31.       return buffer.toString();  
32.    }  
33. }</span>


为了使用上面的线程工厂,请看下面的执行程序:

 

 

1. <span style="font-size:18px;">public static void main(String[] args)  
2. {  
3.   CustomThreadFactory factory = new CustomThreadFactory("CustomThreadFactory");  
4.   Task task = new Task();  
5.   Thread thread;  
6.   System.out.printf("Starting the Threads\n\n");  
7.   for (int i = 1; i <= 10; i++)  
8.   {  
9.      thread = factory.newThread(task);  
10.      thread.start();  
11.   }  
12.   System.out.printf("All Threads are created now\n\n");  
13.   System.out.printf("Give me CustomThreadFactory stats:\n\n" + factory.getStats());  
14. }</span>

程序执行结果:

 

 

1. <span style="font-size:18px;">Output :  
2.    
3. Starting the Threads  
4.    
5. All Threads are created now  
6.    
7. Give me CustomThreadFactory stats:  
8.    
9. Created thread 9 with name CustomThreadFactory-Thread_1 on Tue Jan 06 13:18:04 IST 2015  
10. Created thread 10 with name CustomThreadFactory-Thread_2 on Tue Jan 06 13:18:04 IST 2015  
11. Created thread 11 with name CustomThreadFactory-Thread_3 on Tue Jan 06 13:18:04 IST 2015  
12. Created thread 12 with name CustomThreadFactory-Thread_4 on Tue Jan 06 13:18:04 IST 2015  
13. Created thread 13 with name CustomThreadFactory-Thread_5 on Tue Jan 06 13:18:04 IST 2015  
14. Created thread 14 with name CustomThreadFactory-Thread_6 on Tue Jan 06 13:18:04 IST 2015  
15. Created thread 15 with name CustomThreadFactory-Thread_7 on Tue Jan 06 13:18:04 IST 2015  
16. Created thread 16 with name CustomThreadFactory-Thread_8 on Tue Jan 06 13:18:04 IST 2015  
17. Created thread 17 with name CustomThreadFactory-Thread_9 on Tue Jan 06 13:18:04 IST 2015  
18. Created thread 18 with name CustomThreadFactory-Thread_10 on Tue Jan 06 13:18:04 IST 2015</span>

上面的代码中,ThreadFactory接口只有一个叫做newThread()的方法,它接收一个Runnable对象作为参数,同时返回一个Thread对象。当你实现ThreadFactory接口时,你必须重写这个方法。