天天看点

使用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