-
核心提示:简单的并发应用 JDK 1.5 API 中增加了新的包.
java.util.concurrent 在并发编程中很常用的实用工具类。 java.util.concurrent.locks
为锁定和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。 此包下的类图 常用的类 : Executors 提供建立线程池 -
简单的并发应用
JDK 1.5 API 中增加了新的包.
java.util.concurrent 在并发编程中很常用的实用工具类。
java.util.concurrent.locks 为锁定和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。此包下的类图
常用的类 :
Executors 提供建立线程池或线程工厂的方法.
ExecutorService 提供了管理终止的方法,以及可为跟踪一个或多个异步任务执行状况而生成 Future 的方法。
Executor 可以简单理解为一个线程的执行者.
Future 表示异步计算的结果,对执行的线程的状态检查,可以获得执行结果.
FutureTask 此类提供了对 Future 的基本实现,可使用 FutureTask 包装Callable 或Runnable对象。所以可将 FutureTask 提交给Executor 执行。
Callable 线程要执行而去实现的接口
Semaphore 一个计数信号量,通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。
更加详细类说明和方法的应用,请可以查阅API
以下是简单的例子 :
(限制并发线程数量)
- public
static
void
main(String[] args) {
-
// 线程池
- ExecutorService exec = Executors.newCachedThreadPool();
-
// 只能5个线程同时访问
-
final
Semaphore semp =
new
Semaphore(
5
); -
// 模拟20个客户端访问
-
for
(
int
index =
0
; index <
20
; index++) { -
final
int
NO = index; - Runnable run =
new
Runnable() { -
public
void
run() { -
try
{ -
// 获取许可
- semp.acquire();
- System.out.println(
"thread:"
+ NO); - Thread.sleep((
long
) (Math.random() *
1000
)); -
// 访问完后,释放
- semp.release();
- }
catch
(InterruptedException e) { - }
- }
- };
- exec.execute(run);
- }
-
// 退出线程池
- exec.shutdown();
- }
简单的线程池管理类:
- import
java.util.concurrent.Callable;
- import
java.util.concurrent.ExecutionException; - import
java.util.concurrent.ExecutorService; - import
java.util.concurrent.Executors; - import
java.util.concurrent.Future; - import
java.util.concurrent.FutureTask; - import
java.util.concurrent.Semaphore; - /**
- * 线程池的管理类
- * 通过静态方法获得此类的实例
- */
- public
class
ThreadPoolManager<T> { -
private
ExecutorService service =
null
; -
private
final
Semaphore semp ; -
private
int
maxNum =
3
; -
private
static
ThreadPoolManager me =
null
; -
public
static
ThreadPoolManager getInstance(){ -
if
(me ==
null
){ - me =
new
ThreadPoolManager(); - }
-
return
me; - }
-
public
static
ThreadPoolManager getInstance(
int
maxNum){ -
if
(me ==
null
){ - me =
new
ThreadPoolManager(maxNum); - }
-
return
me; - }
-
private
ThreadPoolManager(){ - service = Executors.newCachedThreadPool();
- semp =
new
Semaphore(maxNum); - }
-
private
ThreadPoolManager(
int
maxNum){ -
this
.maxNum = maxNum; - service = Executors.newCachedThreadPool();
- semp =
new
Semaphore(maxNum); - }
-
/**
- * 执行线程任务,返回结果.
- * 如果线程池数量全部占用,则此线程阻塞.
- * 线程运行的传入参数类的实现方法.
- * @param task 实现java.util.concurrent.Callable接口的类
- * @return 返回线程运行的结果
- * @throws InterruptedException
- * @throws ExecutionException
- */
-
public
T runTask(Callable<T> task)
throws
InterruptedException, ExecutionException{ - semp.acquire();
//使用信号标记,如果超过个数会造成线程阻塞
- Future<T> f = service.submit(task);
//加载线程任务,并且执行
- semp.release();
//信号释放
-
return
f.get();
//返回线程处理的结果,如果此线程尚未处理完成,则线程阻塞直到返回结果
- }
-
/**
- * @return 返回线程可用数量
- */
-
public
synchronized
int
getavailableNum(){ -
return
this
.semp.availablePermits(); - }
-
/**
- * 关闭线程池
- */
-
public
final
void
close(){ - service.shutdown();
- }
- }
- public