- 浏览: 179491 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
donggongai:
Blackbaby 写道mazhi4562qq 写道moist ...
openfire集成现有系统的用户表 -
Blackbaby:
mazhi4562qq 写道moistrot 写道哥们,你集成 ...
openfire集成现有系统的用户表 -
mazhi4562qq:
moistrot 写道哥们,你集成成功了吗?我这样集成之后,登 ...
openfire集成现有系统的用户表 -
dxcsingle:
哥们 我如今也遇到这个问题 能讲哈怎么处理么?
openfire集成现有系统的用户表 -
hooktoo:
该例子有很严重的问题,java没释放内存
JNative dll回调函数使用实例
As you can see, a thread pool is a useful way to limit the amount of threads that a Java program will be using. This article presented a complete thread pool that can be used with Java. This thread pool can easily become the starting point for any application that you create, which requires a thread pool.
Listing 1—The ThreadPool Class
package com.heaton.threads; import java.util.*; /** * Java Thread Pool * * This is a thread pool that for Java, it is * simple to use and gets the job done. This program and * all supporting files are distributed under the Limited * GNU Public License (LGPL, http://www.gnu.org). * * This is the main class for the thread pool. You should * create an instance of this class and assign tasks to it. * * For more information visit http://www.jeffheaton.com. * * @author Jeff Heaton (http://www.jeffheaton.com) * @version 1.0 */ public class ThreadPool { /** * The threads in the pool. */ protected Thread threads[] = null; /** * The backlog of assignments, which are waiting * for the thread pool. */ Collection assignments = new ArrayList(3); /** * A Done object that is used to track when the * thread pool is done, that is has no more work * to perform. */ protected Done done = new Done(); /** * The constructor. * * @param size How many threads in the thread pool. */ public ThreadPool(int size) { threads = new WorkerThread[size]; for (int i=0;i<threads.length;i++) { threads[i] = new WorkerThread(this); threads[i].start(); } } /** * Add a task to the thread pool. Any class * which implements the Runnable interface * may be assienged. When this task runs, its * run method will be called. * * @param r An object that implements the Runnable interface */ public synchronized void assign(Runnable r) { done.workerBegin(); assignments.add(r); notify(); } /** * Get a new work assignment. * * @return A new assignment */ public synchronized Runnable getAssignment() { try { while ( !assignments.iterator().hasNext() ) wait(); Runnable r = (Runnable)assignments.iterator().next(); assignments.remove(r); return r; } catch (InterruptedException e) { done.workerEnd(); return null; } } /** * Called to block the current thread until * the thread pool has no more work. */ public void complete() { done.waitBegin(); done.waitDone(); } protected void finalize() { done.reset(); for (int i=0;i<threads.length;i++) { threads[i].interrupt(); done.workerBegin(); threads[i].destroy(); } done.waitDone(); } } /** * The worker threads that make up the thread pool. * * @author Jeff Heaton * @version 1.0 */ class WorkerThread extends Thread { /** * True if this thread is currently processing. */ public boolean busy; /** * The thread pool that this object belongs to. */ public ThreadPool owner; /** * The constructor. * * @param o the thread pool */ WorkerThread(ThreadPool o) { owner = o; } /** * Scan for and execute tasks. */ public void run() { Runnable target = null; do { target = owner.getAssignment(); if (target!=null) { target.run(); owner.done.workerEnd(); } } while (target!=null); } }
Listing 2—The Done Class
package com.heaton.threads; /** * * This is a thread pool for Java, it is * simple to use and gets the job done. This program and * all supporting files are distributed under the Limited * GNU Public License (LGPL, http://www.gnu.org). * * This is a very simple object that * allows the TheadPool to determine when * it is done. This object implements * a simple lock that the ThreadPool class * can wait on to determine completion. * Done is defined as the ThreadPool having * no more work to complete. * * Copyright 2001 by Jeff Heaton * * @author Jeff Heaton (http://www.jeffheaton.com) * @version 1.0 */ public class Done { /** * The number of Worker object * threads that are currently working * on something. */ private int _activeThreads = 0; /** * This boolean keeps track of if * the very first thread has started * or not. This prevents this object * from falsely reporting that the ThreadPool * is done, just because the first thread * has not yet started. */ private boolean _started = false; /** * This method can be called to block * the current thread until the ThreadPool * is done. */ synchronized public void waitDone() { try { while ( _activeThreads>0 ) { wait(); } } catch ( InterruptedException e ) { } } /** * Called to wait for the first thread to * start. Once this method returns the * process has begun. */ synchronized public void waitBegin() { try { while ( !_started ) { wait(); } } catch ( InterruptedException e ) { } } /** * Called by a Worker object * to indicate that it has begun * working on a workload. */ synchronized public void workerBegin() { _activeThreads++; _started = true; notify(); } /** * Called by a Worker object to * indicate that it has completed a * workload. */ synchronized public void workerEnd() { _activeThreads--; notify(); } /** * Called to reset this object to * its initial state. */ synchronized public void reset() { _activeThreads = 0; } }
Listing 3—The Example Worker Thread
import com.heaton.threads.*; /** * This class shows an example worker thread that can * be used with the thread pool. It demonstrates the main * points that should be included in any worker thread. Use * this as a starting point for your own threads. * * @author Jeff Heaton (http://www.jeffheaton.com) * @version 1.0 */ public class TestWorkerThread implements Runnable { static private int count = 0; private int taskNumber; protected Done done; /** * * @param done */ TestWorkerThread() { count++; taskNumber = count; } public void run() { for (int i=0;i<100;i+=2) { System.out.println("Task number: " + taskNumber + ",percent complete = " + i ); try { Thread.sleep((int)(Math.random()*500)); } catch (InterruptedException e) { } } } }
Listing 4—The ThreadPool Class
import com.heaton.threads.*; /** * Main class used to test the thread pool. * * @author Jeff Heaton (http://www.jeffheaton.com) * @version 1.0 */ public class TestThreadPool { /** * Main entry point. * * @param args No arguments are used. */ public static void main(String args[]) { ThreadPool pool = new ThreadPool(10); for (int i=1;i<25;i++) { pool.assign(new TestWorkerThread()); } pool.complete(); System.out.println("All tasks are done."); }
发表评论
-
FreeMarker 模板文件 路径设置
2013-09-23 16:17 15551.freemarker 模板文件路径设置 ... -
关于JVM说明的摘要
2012-03-15 15:00 929If the JVM is launched w ... -
Disabling Certificate Validation in an HTTPS Connection
2011-12-09 13:49 1159final static HostnameVerifi ... -
[Maven]eclipse中的java项目转化成java ee项目
2011-11-22 15:25 1005mvn eclipse:eclipse -Dwtp ... -
Base62
2011-11-19 11:10 1569public class Base62 { pri ... -
How do I use Form Authentication with Tomcat?
2011-04-27 21:27 913web.xml <?xml vers ... -
poi 解析excel
2011-04-06 18:13 1331package com.synnex.web.c ... -
Ant with Maven environment
2011-02-16 14:36 893<?xml version="1.0" ... -
How can I setup my BlazeDS implementation with Log4J?
2011-01-25 16:09 880Out of box BlazeDS does no ... -
windows批处理命令执行java程序
2011-01-17 23:23 1580window auto.bat文件内容: ... -
How to specify firstDayOfWeek for java.util.Calendar using a JVM argument
2011-01-14 22:47 827Question: I'm trying t ... -
收集的工具方法
2011-01-09 23:19 817package org.codehaus.jackson.ut ... -
JBoss-4.2.3GA配置MySQL数据库连接池
2010-07-23 10:30 13051.将mysql的JDBC驱动放到的JBOSS_HOME\se ... -
svn插件导致eclipse崩溃
2010-06-10 10:19 1150从 http://subclipse.tigris.org/u ... -
Web 开发中的调试利器--tcptrace
2010-04-16 10:41 962Web 开发中的调试利器--tcptrace -
Tomcat6 Support JTA with JOTM
2010-04-07 11:47 17971 jotm jars into tomcat6/lib ... -
Fire Workflow工作流脚本
2010-03-10 16:43 924Fire Workflow 中的七张表脚本: Orale: ... -
java实现类似函数式语言的map/filter功能
2010-03-02 23:13 1087http://www.jdon.com/jivejdon/th ... -
comet网站
2010-01-07 15:20 138http://www.lightstreamer.com/ ... -
jsp中pageEncoding、charset=utf8"、(request/response).setCharacterEncoding("utf8")
2009-11-26 09:22 3179原文地址:http://hi.baidu. ...
相关推荐
Creating Web Applets With Java
Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate.pdf mypetstore.zip 教程和例子在同一个包内。 学习的好资料!
Java 5.0 is a huge step forward for the development of concurrent applications in Java, providing new higherͲlevel components and additional lowͲlevel mechanisms that make it easier for novices and ...
Creating a Class Factory with C# and .NET.doc
Creating Wizard Dialogs with Java Swing 使用Swing设计向导 官网示例 http://www.oracle.com/technetwork/articles/javase/wizard-136789.html
Whether you are a Java expert or at a beginner level, you'll benefit from this book, because it will teach you a brand new way of coding and thinking. The book starts with an explanation of what ...
Creating a Tree in Web Dynpro Java
Java includes a comprehensive API with a lot of ready-to-use components to implement powerful concurrency applications in an easy way, but with a high flexibility to adapt these components to your ...
您将探索如何使用Ajax来增强站点并使其具有Web 2.0的感觉,以及其他JavaScript增强如何将Web浏览器和网站变成真正的应用程序。
Learning.C .by.Creating.Games.with.UE4.2015.pdf UE4开发指南英文版,想要学习的同学可以看看。
Mason, Mason.js for creating a perfect grid with jQuery.
VBA Creating a DLL with VisualBasic
Creating a video with OpenCV,笔者自己学习的笔记和相关的源码,视频文件,cmake等
modular_java_creating_flexible_applications_with_osgi_and_spring
So, no matter what fonts a balloter (or reader) has on their computer, they'll be able to see the file as you intended it to be seen. If you don't embed fonts, Adobe AcrobatTM or an equivalent PDF ...
Title: Java 7: A Comprehensive Tutorial Author: Budi Kurniawan Length: 850 pages Edition: 1 Language: English Publisher: BrainySoftware Publication Date: 2014-05-15 ISBN-10: 0980839661 ISBN-13: ...
Creating 3D Content with WPF_DOC
Lua 进阶 Lua 与 C 交互设计 Creating Solid APIs with Lua 高清版
Preface Acknowledgements 1 XML for Data 2 XML Protocols 3 Writing XML with Java 4 Converting Flat Files to XML 5 Reading XML 6 SAX 7 The XMLReader Interface 8 SAX ...