java 缓存线程池_Java中有返回值的线程,(缓存)线程池的初步使用

一 简介

在JDK1.5以前的线程是没有返回值的(Thread,Runnable),Callable这个接口是之后才出现的新特性,用法跟Runnable类似,只是不同的是可以有返回值。因此为了测试Callable这个类以及线程池相关内容,我将上一篇文章中的代码进行了小幅度的修改然后写了一下

二 关于线程池的简单使用步骤

1 定义线程类,(1)extends Thread (2)implements Runnable (3)implements Callable<>

2 建立ExecutorService线程池,比如这样写:ExecutorService pool = Executors.newCachedThreadPool(); 这是建立一个缓存池。一般常用的还有:(1)newFixedThreadPool() (2)ScheduledThreadPool() (3)SingleThreadExecutor()

3调用线程池进行操作

//执行任务并获取Future对象

Future> future = pool.submit(myThread);

//从Future对象上获取任务的返回值

future.get();

三 完整测试代码package thread;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

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;

public class Test2 {

public static void main(String[] args) {

long millis1 = System.currentTimeMillis();

String fileName = "C:\\Users\\Administrator\\Desktop\\测试2.txt";

int threadNum = 5;  //测试用的线程数目

List list = new ArrayList();

//创建一个缓存线程池

ExecutorService pool = Executors.newCachedThreadPool();

for (int i = 0; i 

Callable> myThread = new MyThread2("线程" + i, i, threadNum);

Future> future = pool.submit(myThread);

try {

list.addAll(future.get());

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

}

}

pool.shutdown();  //不关闭的话,要等到超时程序才会结束

Iterator iterator = list.iterator();

try {

BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName)));

while(iterator.hasNext()){

writer.write(iterator.next());

writer.newLine();

writer.flush();

}

writer.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

long millis2 = System.currentTimeMillis();

System.out.println(millis2 - millis1);  //大约170-177ms

}

}

/**

* 自定义任务(线程)

* */

class MyThread2 implements Callable>{

private String name; // 线程的名字

private int i; // 第几个线程

private int threadNum; // 总共创建了几个线程

public MyThread2(String name, int i, int threadNum) {

this.name = name;

this.i = i;

this.threadNum = threadNum;

}

public List call() throws Exception {

MyPrint2 myPrint2 = new MyPrint2();

return myPrint2.print(name, i, threadNum);

}

}

/**

* 具体的业务操作

* */

class MyPrint2 {

public List print(String name, int x, int threadNum) {

List list = new ArrayList();

for (int i = x; i <= 10000; i = i + threadNum) {

//System.out.println(name + ": " + i + "----------------------------------我是一条华丽的小尾巴");

list.add(name + ": " + i + "----------------------------------我是一条华丽的小尾巴");

}

return list;

}

}

注:这种写法由于步骤较多,因此在这里反而比单线程还稍慢O(∩_∩)O~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值