Java_Thread:java线程池

本文详细介绍了Java5之后并发线程的关键变化,特别是如何使用ExecutorService来更高效地启动、管理线程。包括创建任务、执行任务、关闭执行服务对象等核心操作,以及如何利用线程池特性提高程序性能。

======================================================
注:本文源代码点此下载
======================================================

在java5之后,并 发线程这块发生了根本的变化,最重要的莫过于新的启动、调度、管理线程的一大堆api了。在java5以后,通过executor来启动线程比用 thread的start()更好。在新特征中,可以很容易控制线程的启动、执行和关闭过程,还可以很容易使用线程池的特性。

一、创建任务

任务就是一个实现了runnable接口的类。

创建的时候实run方法即可。

二、执行任务

通过java.util.concurrent.executorservice接口对象来执行任务,该接口对象通过工具类java.util.concurrent.executors的静态方法来创建。

executors此包中所定义的 executor、executorservice、scheduledexecutorservice、threadfactory 和 callable 类的工厂和实用方法。

executorservice提供了管理终止的方法,以及可为跟踪一个或多个异步任务执行状况而生成 future 的方法。 可以关闭 executorservice,这将导致其停止接受新任务。关闭后,执行程序将最后终止,这时没有任务在执行,也没有任务在等待执行,并且无法提交新任 务。

executorservice.execute(new testrunnable());

1、创建executorservice

通过工具类java.util.concurrent.executors的静态方法来创建。

executors此包中所定义的 executor、executorservice、scheduledexecutorservice、threadfactory 和 callable 类的工厂和实用方法。

比如,创建一个executorservice的实例,executorservice实际上是一个线程池的管理工具:

executorservice executorservice = executors.newcachedthreadpool();

executorservice executorservice = executors.newfixedthreadpool(3);

executorservice executorservice = executors.newsinglethreadexecutor();

2、将任务添加到线程去执行

当将一个任务添加到线程池中的时候,线程池会为每个任务创建一个线程,该线程会在之后的某个时刻自动执行。

三、关闭执行服务对象

executorservice.shutdown();

四、综合实例

package concurrent;

import java.util.concurrent.executorservice;

import java.util.concurrent.executors;

/**

* created by intellij idea.

*

* @author leizhimin 2008-11-25 14:28:59

*/

public class testcachedthreadpool {

public static void main(string[] args) {

//executorservice executorservice = executors.newcachedthreadpool();

executorservice executorservice = executors.newfixedthreadpool(5); //executorservice executorservice = executors.newsinglethreadexecutor();

for (int i = 0; itask) 方法来执行,并且返回一个future,是表示任务等待完成的 future.

public interface callable返回结果并且可能抛出异常的任务。实现者定义了一个不带任何参数的叫做 call 的方法。

callable 接口类似于 runnable,两者都是为那些其实例可能被另一个线程执行的类设计的。但是 runnable 不会返回结果,并且无法抛出经过检查的异常。

executors 类包含一些从其他普通形式转换成 callable 类的实用方法。

callable中的call()方法类似runnable的run()方法,就是前者有返回值,后者没有。

当将一个callable的对象传递给executorservice的submit方法,则该call方法自动在一个线程上执行,并且会返回执行结果future对象。

同样,将runnable的对象传递给executorservice的submit方法,则该run方法自动在一个线程上执行,并且会返回执行结果future对象,但是在该future对象上调用get方法,将返回null.

遗憾的是,在java api文档中,这块介绍的很糊涂,估计是翻译人员还没搞清楚的缘故吧。或者说是注释不到位。下面看个例子:

import java.util.arraylist;

import java.util.list;

import java.util.concurrent.*;

/**

* callable接口测试

*

* @author leizhimin 2008-11-26 9:20:13

*/

public class callabledemo {

public static void main(string[] args) {

executorservice executorservice = executors.newcachedthreadpool();

list> resultlist = new arraylist>();

//创建10个任务并执行

for (int i = 0; ifuture = executorservice.submit(new taskwithresult(i));

//将任务执行结果存储到list中

resultlist.add(future);

}

//遍历任务的结果

for (future fs : resultlist) {

try {

system.out.println(fs.get());//打印各个线程(任务)执行的结果

} catch (interruptedexception e) {

e.printstacktrace();

} catch (executionexception e) {

e.printstacktrace();

} finally {

//启动一次顺序关闭,执行以前提交的任务,但不接受新任务。如果已经关闭,则调用没有其他作用。

executorservice.shutdown();

}

}

}

}

class taskwithresult implements callable {

private int id;

public taskwithresult(int id) {

this.id = id;

}

/**

* 任务的具体过程,一旦任务传给executorservice的submit方法,则该方法自动在一个线程上执行。

*

* @return

* @throws exception

*/

public string call() throws exception {

system.out.println("call()方法被自动调用,干活!!!" + thread.currentthread().getname());

//一个模拟耗时的操作

for (int i = 999999; i > 0; i--) ;

return "call()方法被自动调用,任务的结果是:" + id + "" + thread.currentthread().getname();

}

}

运行结果:

call()方法被自动调用,干活!!!pool-1-thread-1

call()方法被自动调用,干活!!!pool-1-thread-3

call()方法被自动调用,干活!!!pool-1-thread-4

call()方法被自动调用,干活!!!pool-1-thread-6

call()方法被自动调用,干活!!!pool-1-thread-2

call()方法被自动调用,干活!!!pool-1-thread-5

call()方法被自动调用,任务的结果是:0pool-1-thread-1

call()方法被自动调用,任务的结果是:1pool-1-thread-2

call()方法被自动调用,干活!!!pool-1-thread-2

call()方法被自动调用,干活!!!pool-1-thread-6

call()方法被自动调用,干活!!!pool-1-thread-4

call()方法被自动调用,任务的结果是:2pool-1-thread-3

call()方法被自动调用,干活!!!pool-1-thread-3

call()方法被自动调用,任务的结果是:3pool-1-thread-4

call()方法被自动调用,任务的结果是:4pool-1-thread-5

call()方法被自动调用,任务的结果是:5pool-1-thread-6

call()方法被自动调用,任务的结果是:6pool-1-thread-2

call()方法被自动调用,任务的结果是:7pool-1-thread-6

call()方法被自动调用,任务的结果是:8pool-1-thread-4

call()方法被自动调用,任务的结果是:9pool-1-thread-3

http://www.iteye.com/topic/1115593


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值