使用java.util.concurrent.ThreadFactory類建立線程,threadfactory
工廠設計模式是Java中最常用的設計模式之一。它是一種建立型設計模式,能夠用於建立一個或多個類所需要的對象。有了這個工廠,我們就能集中的建立對象。
集中建立方式給我們帶來了一些好處,例如:
1. 能夠很容易的改變類建立的對象或者建立對象的方式;
2. 能夠很容易限制對象的建立,例如:我們只能為a類建立N個對象;
3. 能夠很容易的產生有關對象建立的統計資料。
在Java中,我們通常使用兩種方式來建立線程:繼承Thread類和實現Runnable介面。Java還提供了一個介面,既ThreadFactory介面,用於建立你自己的線程對象工廠。
很多類中,例如:ThreadPoolExecutor,使用建構函式來接收ThreadFactory來作為參數。這個工廠參數將會在程式執行時建立新的線程。使用ThreadFactory,你能夠自訂執行程式如何建立線程,例如為線程定義適當的名稱、優先順序,或者你甚至可以將它設定為守護線程。
ThreadFactory例子
在這個例子中,我們將學習如何通過實現一個ThreadFactory介面來建立一個有個人化名稱的線程對象,同時,我們儲存了線程對象的建立資訊。
Task.java
class Task implements Runnable{ @Override public void run() { try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } }}
CustomThreadFactory.java
<span style="font-size:18px;">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(); }}</span>
為了使用上面的線程工廠,請看下面的執行程式:
<span style="font-size:18px;">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());}</span>
程式執行結果:
<span style="font-size:18px;">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 2015Created thread 10 with name CustomThreadFactory-Thread_2 on Tue Jan 06 13:18:04 IST 2015Created thread 11 with name CustomThreadFactory-Thread_3 on Tue Jan 06 13:18:04 IST 2015Created thread 12 with name CustomThreadFactory-Thread_4 on Tue Jan 06 13:18:04 IST 2015Created thread 13 with name CustomThreadFactory-Thread_5 on Tue Jan 06 13:18:04 IST 2015Created thread 14 with name CustomThreadFactory-Thread_6 on Tue Jan 06 13:18:04 IST 2015Created thread 15 with name CustomThreadFactory-Thread_7 on Tue Jan 06 13:18:04 IST 2015Created thread 16 with name CustomThreadFactory-Thread_8 on Tue Jan 06 13:18:04 IST 2015Created thread 17 with name CustomThreadFactory-Thread_9 on Tue Jan 06 13:18:04 IST 2015Created thread 18 with name CustomThreadFactory-Thread_10 on Tue Jan 06 13:18:04 IST 2015</span>
上面的代碼中,ThreadFactory介面只有一個叫做newThread()的方法,它接收一個Runnable對象作為參數,同時返回一個Thread對象。當你實現ThreadFactory介面時,你必須重寫這個方法。
Happy Learning !!
原文串連:Creating Threads Using java.util.concurrent.ThreadFactory