首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

线程池ExecutorService的submit跟execute

2013-12-04 
线程池ExecutorService的submit和execute转:线程池ExecutorService的submit和execute?在Java5之后,并发线

线程池ExecutorService的submit和execute

转:线程池ExecutorService的submit和execute

?

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

?

??????? ExecutorService executorService = Executors.newCachedThreadPool();

?

??????? ExecutorService executorService = Executors.newFixedThreadPool(3);

?

??????? ExecutorService executorService = Executors.newSingleThreadExecutor();

?

??? @Test

?

??? publicvoid testDemo() throws Exception {

?

?????? //单例线程,任意时间(同一时间)池中只能有一个线程

?

?????? ExecutorService es = Executors.newSingleThreadExecutor();

?

?????? es.execute(new Runnable() {

?

?????????? @Override

?

?????????? publicvoid run() {

?

????????????? System.err.println("线程启动并运行"+Thread.currentThread().getName());

?

?????????? }

?

?????? });

?

?????? es.execute(new Runnable() {

?

?????????? @Override

?

?????????? publicvoid run() {

?

????????????? System.err.println("第二个也运行了"+Thread.currentThread().getName());

?

?????????? }

?

?????? });

?

?????? //Thread.sleep(1000 * 60 * 60);

?

线程启动并运行pool-1-thread-1

?

??? @Test

?

??? publicvoid testDemo3() throws Exception {

?

?????? //声明一个线程池

?

?????? ExecutorService ex = Executors.newCachedThreadPool();

?

?????? for (int i = 0; i < 4; i++) {

?

?????????? finalint a = i;

?

?????????? //每一次execute方法,都是向池中放入一个对象

?

?????????? ex.execute(new Runnable() {

?

????????????? publicvoid run() {

?

????????????????? while(true){

?

???????????????????? System.err.println("测试...."+a+">"

?

??????????????????????????? +Thread.currentThread().getName()+","

?

??????????????????????????? +Thread.currentThread().isDaemon());

?

???????????????????? try{

?

???????????????????????? Thread.sleep(2000);

?

???????????????????? }catch(Exception e){

?

???????????????????????? e.printStackTrace();

?

???????????????????? }

?

????????????????? }

?

????????????? }

?

?????????? });

?

?????? }

?

?????? Thread.sleep(1000*60*60);

?

测试....0>pool-1-thread-1,false

?

测试....3>pool-1-thread-4,false

?

测试....2>pool-1-thread-3,false

?

测试....1>pool-1-thread-2,false

?

测试....0>pool-1-thread-1,false

?

测试....3>pool-1-thread-4,false

?

测试....2>pool-1-thread-3,false

?

测试....1>pool-1-thread-2,false

?

测试....1>pool-1-thread-2,false

?

测试....2>pool-1-thread-3,false

?

测试....3>pool-1-thread-4,false

?

测试....0>pool-1-thread-1,false

?

??? publicvoid testCall() throws Exception{

?

?????? //声明一个类,可以被调用,类似于线程,但它可以拥有返回值

?

?????? class MyCall implements Callable<String>{

?

?????????? privateintseq;

?

?????????? public MyCall(int seq){

?

????????????? this.seq=seq;

?

?????????? }

?

?????????? //抛出异常并可以拥有返回值

?

?????????? public String call() throws Exception {

?

????????????? System.err.println("执行"+seq+","+Thread.currentThread().getName());

?

????????????? Thread.sleep(3000);

?

????????????? System.err.println("Weak up "+seq);

?

????????????? return"完成"+seq;//这是返回值

?

?????????? }

?

?????? }

?

?????? ExecutorService es = Executors.newCachedThreadPool();//创建线程池对象

?

?????? List<Future<String>> result =new ArrayList<Future<String>>();//放结果用的集合

?

?????? for(int i=0;i<3;i++){

?

?????????? Future<String> f=es.submit(new MyCall(i));//线程执行完成以后可以通过引用获取返回值

?

?????????? result.add(f);

?

?????? }

?

?????? for(Future<String> f:result){

?

?????????? System.err.println("返回值:"+f.get());//输出返回的值

?

?????? }

?

?????? System.err.println("完成....");

??? }

热点排行