天天看點

使用java.util.concurrent.ThreadFactory來建立線程使用java.util.concurrent.ThreadFactory來建立線程

Creating Threads Usingjava.util.concurrent.ThreadFactory,它簡單介紹了如何使用ThreadFactory來建立線程,最重要的是這中做的好處,為什麼有時候要用它建立,下面就讓我們看看這篇文章吧。

工廠設計模式是一種最常用的設計模式在java中。這是一個建立型模式需求,可以用來開發一個對象的一個或多個類。有了這個工廠,我們就可以集中對象的建立。

建立邏輯的集中帶給我們一些好處,如下:

很容易改變的類建立的對象或我們建立這些對象的方式。

很容易用有限的資源限制的建立對象,例如,我們隻能有N個對象。

很容易生成統計資料對建立的對象。

在java中,我們通常使用兩種方法即建立線程。線程實作runnable接口的類和擴充。Java還提供了一個接口,<code> ThreadFactory</code>接口,建立你自己的<code> Thread</code>對象的工廠。

各種類,如<code> ThreadPoolExecutor</code>,使用構造函數接受<code> ThreadFactory</code>作為參數。這個工廠當執行程式建立一個新的線程使用。使用<code>ThreadFactory</code>您可以自定義線程建立的執行者,他們有适當的線程名稱、優先級,甚至他們還可以守護程序。

在這個例子中,我們将學習如何實作<code> ThreadFactory</code>接口來建立線程對象與一個個性化的名字雖然我們儲存的統計<code> Thread</code>建立的對象。

Task.java

<b>[java]</b> view plain copy

class Task implements Runnable

{

   @Override

   public void run()

   {

      try

      {

         TimeUnit.SECONDS.sleep(2);

      } catch (InterruptedException e)

         e.printStackTrace();

      }

   }

}

CustomThreadFactory.java

public class CustomThreadFactory implements ThreadFactory

   private int          counter;

   private String       name;

   private List&lt;String&gt; stats;

   public CustomThreadFactory(String name)

      counter = 1;

      this.name = name;

      stats = new ArrayList&lt;String&gt;();

   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&lt;String&gt; it = stats.iterator();

      while (it.hasNext())

         buffer.append(it.next());

      return buffer.toString();

使用上面的線程工廠,看下面的例子:

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 &lt;= 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

在這裡,<code> ThreadFactory</code>接口隻有一個方法調用<code> newThread()</code>。它接收一個<code> Runnable</code>對象作為參數,并傳回一個<code> Thread</code>對象。當你實作一個<code> ThreadFactory</code>接口,您必須實作該接口并覆寫此方法。

原文位址http://www.bieryun.com/2214.html