使用java.util.concurrent.ThreadFactory来创建线程

使用java.util.concurrent.ThreadFactory来创建线程

Creating Threads Usingjava.util.concurrent.ThreadFactory,它简单介绍了如何使用ThreadFactory来创建线程,最重要的是这中做的好处,为什么有时候要用它创建,下面就让我们看看这篇文章吧。

工厂设计模式是一种最常用的设计模式在java中。这是一个创建型模式需求,可以用来开发一个对象的一个或多个类。有了这个工厂,我们就可以集中对象的创建。

创建逻辑的集中带给我们一些好处,如下:

  1. 很容易改变的类创建的对象或我们创建这些对象的方式。
  2. 很容易用有限的资源限制的创建对象,例如,我们只能有N个对象。
  3. 很容易生成统计数据对创建的对象。

在java中,我们通常使用两种方法即创建线程。线程实现runnable接口的类和扩展。Java还提供了一个接口, ThreadFactory接口,创建你自己的 Thread对象的工厂。

各种类,如 ThreadPoolExecutor,使用构造函数接受 ThreadFactory作为参数。这个工厂当执行程序创建一个新的线程使用。使用ThreadFactory您可以自定义线程创建的执行者,他们有适当的线程名称、优先级,甚至他们还可以守护进程。

ThreadFactory的例子

在这个例子中,我们将学习如何实现 ThreadFactory接口来创建线程对象与一个个性化的名字虽然我们保存的统计 Thread创建的对象。

Task.java

[java] view plain copy

  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

[java] view plain copy

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

使用上面的线程工厂,看下面的例子:

[java] view plain copy

  1. 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. }
  15. Output :
  16. Starting the Threads
  17. All Threads are created now
  18. Give me CustomThreadFactory stats:
  19. Created thread 9 with name CustomThreadFactory-Thread_1 on Tue Jan 06 13:18:04 IST 2015
  20. Created thread 10 with name CustomThreadFactory-Thread_2 on Tue Jan 06 13:18:04 IST 2015
  21. Created thread 11 with name CustomThreadFactory-Thread_3 on Tue Jan 06 13:18:04 IST 2015
  22. Created thread 12 with name CustomThreadFactory-Thread_4 on Tue Jan 06 13:18:04 IST 2015
  23. Created thread 13 with name CustomThreadFactory-Thread_5 on Tue Jan 06 13:18:04 IST 2015
  24. Created thread 14 with name CustomThreadFactory-Thread_6 on Tue Jan 06 13:18:04 IST 2015
  25. Created thread 15 with name CustomThreadFactory-Thread_7 on Tue Jan 06 13:18:04 IST 2015
  26. Created thread 16 with name CustomThreadFactory-Thread_8 on Tue Jan 06 13:18:04 IST 2015
  27. Created thread 17 with name CustomThreadFactory-Thread_9 on Tue Jan 06 13:18:04 IST 2015
  28. Created thread 18 with name CustomThreadFactory-Thread_10 on Tue Jan 06 13:18:04 IST 2015

在这里, ThreadFactory接口只有一个方法调用 newThread()。它接收一个 Runnable对象作为参数,并返回一个 Thread对象。当你实现一个 ThreadFactory接口,您必须实现该接口并覆盖此方法。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值