工厂设计模式是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 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</span>
上面的代码中,ThreadFactory接口只有一个叫做newThread()的方法,它接收一个Runnable对象作为参数,同时返回一个Thread对象。当你实现ThreadFactory接口时,你必须重写这个方法。
其他精彩文章文章
jQuery教程(10)-DOM树操作之内容setter和getter方法
android学习笔记(37)使用 DatePickerDialog、TimePickerDialog
更多关于android开发文章
相关推荐
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1)
java并发工具包 java.util.concurrent中文版-带书签版
java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic,tools。 该图详细的列举了并发包下面的结构,包含所有接口和...
java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
java并发工具包 java.util.concurrent中文版pdf
java.util.ConcurrentModificationException 异常问题详解1
Exception in thread “main“ java.util.InputMismatchException
Java并发编程工具包java.util.concurrent的UML类结构图 PDF
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用
详细介绍了java.util.logging.Logger的用法和结构,对如果扩展Logger起到抛砖引玉的作用!尊重劳动成果,亲下载了要给个评价!
主要介绍了java.util.concurrent.ExecutionException 问题解决方法的相关资料,需要的朋友可以参考下
java.util.concurrent系列文章(2) java.util.concurrent系列文章(2) java.util.concurrent系列文章(2) java.util.concurrent系列文章(2)
如何启动:以win7系统为例,最好jdk8 1.打开cmd,cd到jdk的path,本机是:cd C:\Java\jdk6\bin ...java -cp D:\javaConcurrentAnimated.jar vgrazi.concurrent.samples.launcher.ConcurrentExampleLauncher
java.util.concurrent-多线程框架
一个高性能的Java线程库,该库是 JDK 1.5 中的 java.util.concurrent 包的补充,可用于基于并发消息机制的应用。该类库不提供远程的消息功能,其设计的宗旨是实现一个内存中的消息传递机制. 主要特点有: * All ...
java.util.Date与java.sql.Date互转及字符串转换为日期时间格式.docx
花了一段时间辛苦整理的ppt,与大家分享,请提出您的宝贵意见。