使用java.util.concurrent.ThreadFactory來創建線程
來自: http://blog.csdn.net//chenleixing/article/details/42583701
在How to do in java網站看到很多不錯的好文章,《Creating Threads Usingjava.util.concurrent.ThreadFactory》就是其中一篇,它簡單介紹了如何使用ThreadFactory來創建線程,最重要的是這中做的好處,為什么有時候要用它創建,下面就讓我們看看這篇文章吧。
工廠設計模式是一種最常用的設計模式在java中。這是一個創建型模式需求,可以用來開發一個對象的一個或多個類。有了這個工廠,我們就可以集中對象的創建。
創建邏輯的集中帶給我們一些好處,如下:
- 很容易改變的類創建的對象或我們創建這些對象的方式。
- 很容易用有限的資源限制的創建對象,例如,我們只能有N個對象。
- 很容易生成統計數據對創建的對象。
在java中,我們通常使用兩種方法即創建線程。線程實現runnable接口的類和擴展。Java還提供了一個接口, ThreadFactory
接口,創建你自己的 Thread
對象的工廠。
各種類,如 ThreadPoolExecutor
,使用構造函數接受 ThreadFactory
作為參數。這個工廠當執行程序創建一個新的線程使用。使用ThreadFactory
您可以自定義線程創建的執行者,他們有適當的線程名稱、優先級,甚至他們還可以守護進程。
ThreadFactory的例子
在這個例子中,我們將學習如何實現 ThreadFactory
接口來創建線程對象與一個個性化的名字雖然我們保存的統計 Thread
創建的對象。
Task.java
class Task implements Runnable { @Override public void run() { try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } }
CustomThreadFactory.java</span>
public class CustomThreadFactory implements ThreadFactory { private int counter; private String name; private List<String> stats;public CustomThreadFactory(String name) { counter = 1; this.name = name; stats = new ArrayList<String>(); }
@Override public Thread newThread(Runnable runnable) { Thread t = new Thread(runnable, 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; }
public String getStats() { StringBuffer buffer = new StringBuffer(); Iterator<String> it = stats.iterator(); while (it.hasNext()) { buffer.append(it.next()); } return buffer.toString(); } }</pre> 使用上面的線程工廠,看下面的例子:
public static void main(String[] args) { CustomThreadFactory factory = new CustomThreadFactory("CustomThreadFactory"); Task task = new Task(); Thread thread; System.out.printf("Starting the Threads\n\n"); for (int i = 1; i <= 10; i++) { thread = factory.newThread(task); thread.start(); } System.out.printf("All Threads are created now\n\n"); System.out.printf("Give me CustomThreadFactory stats:\n\n" + factory.getStats()); }Output :
Starting the Threads
All Threads are created now
Give me CustomThreadFactory stats:
Created thread 9 with name CustomThreadFactory-Thread_1 on Tue Jan 06 13:18:04 IST 2015 Created thread 10 with name CustomThreadFactory-Thread_2 on Tue Jan 06 13:18:04 IST 2015 Created thread 11 with name CustomThreadFactory-Thread_3 on Tue Jan 06 13:18:04 IST 2015 Created thread 12 with name CustomThreadFactory-Thread_4 on Tue Jan 06 13:18:04 IST 2015 Created thread 13 with name CustomThreadFactory-Thread_5 on Tue Jan 06 13:18:04 IST 2015 Created thread 14 with name CustomThreadFactory-Thread_6 on Tue Jan 06 13:18:04 IST 2015 Created thread 15 with name CustomThreadFactory-Thread_7 on Tue Jan 06 13:18:04 IST 2015 Created thread 16 with name CustomThreadFactory-Thread_8 on Tue Jan 06 13:18:04 IST 2015 Created thread 17 with name CustomThreadFactory-Thread_9 on Tue Jan 06 13:18:04 IST 2015 Created thread 18 with name CustomThreadFactory-Thread_10 on Tue Jan 06 13:18:04 IST 2015</pre>
在這里,ThreadFactory
接口只有一個方法調用newThread()
。它接收一個Runnable
對象作為參數,并返回一個Thread
對象。當你實現一個ThreadFactory
接口,您必須實現該接口并覆蓋此方法。
感興趣的或者感覺翻譯的太硬不太好的博友,可以點擊此處查看英文原文。