Java多线程之Thread、Runnable、Callable及线程池
一、多线程
线程是指进程中的一个执行流程,一个进程中可以有多个线程。如java.exe进程中可以运行很多线程。进程是运行中的程序,是内存等资源的集合,线程是属于某个进程的,进程中的多个线程共享进程中的内存。线程之间的并发执行是线程轮流占用资源执行的结果,给人一种“同时”执行的感觉。在Java中多线程的编程有很多方法来实现,这里从Thread类、Runnable与Callable接口以及线程池等几个方式来探讨。
二、Thread类
以下是Thread类的部分源代码:
public class Thread implements Runnable { public Thread() {
//...
} public Thread(Runnable target) {
//...
} public static void sleep(long millis, int nanos)
throws InterruptedException {
//...
} public synchronized void start() {
//...
} public void run() {
//...
}
}
可以看到,Thread类实际上是实现了Runnable接口的,重写了Runnable接口中的run()方法;Thread的对象可以通过Runnable的对象来构造,此时如果调用了run()方法,那么这个run()方法是Runnable对象中的run()方法;调用start()方法会启动线程,并将引发调用run()方法,实现并行运行;sleep()使当前的线程暂停一段时间执行,此时当前线程的还占用着资源,并不会释放资源,等到时间到了就会接着继续执行。
一个Thread的简单示例,以帮助理解:
class TestThread extends Thread{
public void run(){
//...
}
}
public class Test{
public static void main(String []args){
Thread t=new TestThread();
t.start();
}
}
三、Runnable接口与Callable接口
还是从Runnable与Callable的源码开始,
public interface Runnable {
public abstract void run();
} public interface Callable<V> {
V call() throws Exception;
}
Runnable封装一个异步运行的任务,可以把它想象成一个没有参数和返回值的异步方法,而run()方法提供所要执行任务的指令。Callable与Runnable类似,不同的是Callable接口是一个参数化的类型,而且有返回值,返回值的类型是参数的类型,如Callable<Integer>是最终返回一个Integer对象的异步任务;Callable提供一个返回参数类型的call方法,和run()的作用类似。
Runnable与Callable接口的应用示例:
/**
* Runnable接口
*
*/
class TestRunnable implements Runnable{
public void run() {
//...
}
}
public class Test {
public static void main(String[] args){
Thread t=new Thread(new TestRunnable());
t.start();
}
} /**
* Callable接口
*
*/
class TestCallable implements Callable<String>{
public String call() throws Exception {
String s="good";
return s;
}
}
public class Test {
public static void main(String[] args) throws Exception{
/**
* FutureTask实现了Future(用于保存异步计算的结果)和Runnable的接口,可将Callable转换成Future和Runnable
*/
FutureTask<String> task=new FutureTask<String>(new TestCallable());
Thread t=new Thread(task);
t.start();
//取得Callable异步线程的返回值
System.out.println(task.get());
}
}
四、线程池
构造一个新的线程是有一定代价的,会涉及到与操作系统的交互。如果程序中创建了大量的生命周期很短的线程,应该使用线程池(thread pool)。一个线程池中包含一些准备运行的空闲线程。将Runnable对象交给线程池,就会有一个线程调用run()方法,当run()方法退出时,线程不会死亡,而是在线程池中准备为下一个请求提供服务。
使用线程池还会减少并发线程的数量。创建大量的线程会大大降低性能甚至是虚拟机崩溃,所有最好是使线程的数目是“固定”的,以限制并发线程的总数。
线程池的创建是使用执行器(Executors)的静态工厂方法,如下:
1、newCachedThreadPool 构建一个线程池,对于每个任务,如果有空闲线程可用,则立即让它执行,如果没有空闲的线程,则创建一个线程,空闲线程会被保留60秒;
2、newFixedThreadpool 构建一个具有固定大小的线程池。如果提交的任务多于空闲的线程数,那么把到不到服务的任务放置到队列中,其它任务完成后再运行它们;
3、newSingleThreadExecutor 是一个退化了大小为1的线程池,由一个线程提交任务,一个接一个。
这三个方法都返回了实现了ExecutorService接口的ThreadPoolExecutor类的对象。使用submit方法将Runnable或Callable对象提交给ExecutorService,在调用了submit时会得到一个Future对象,可用来查询任务的状态,future的接口如下:
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
cancel方法用来取消运行的,如果还没开始运行,则它被取消且不再开始;如果正在运行中,则若mayInterrupt参数为true,它就被中断;
isDone判断任务是否完成,若任务在运行中,isDone返回fasle,如果完成了,则返回true;
不带参数的get方法调用时被阻塞,直到计算完成;如果在计算完成之前,第二个get的调用超时,就会抛出异常;如果该线程被中断,那么两个get都会抛出异常。
线程池的执行流程如下:
1、调用Executors类中的静态方法创建线程池;
2、使用submit提交Ruunable或Callable对象;
3、保存返回的Future对象;
4、没有任务提交时,调用shutdown来关闭线程池。
线程池的示例如下:
/**
* 线程池
*
*/
class MyCallable implements Callable<String>{
public String call() throws Exception {
String s="good";
return s;
}
}
public class Test {
public static void main(String[] args) throws Exception{
//创建大小固定为3的线程池
ExecutorService pool=Executors.newFixedThreadPool(3);
//提交一个Callable任务到线程池,并把结果保存到future对象中
Future<String> res=pool.submit(new MyCallable());
//得到线程执行返回的结果
System.out.println(res.get());
//关闭线程池
pool.shutdown();
}
}
最新文章
- IE8 jq focus BUG
- Xslt 1.0中使用Array
- 我的Windows软件清单
- struts原理
- python文件I/O(转)
- Oracle 查询性能优化实践
- MAC OS Finder 中快速定位指定路径
- ubuntu Nodejs和npm的安装
- python_Memcached
- Error running app: Instant Run requires &#39;Tools | Android | Enable ADB integration&#39; to be enabled.解决办法
- runtime详解2
- mysql - 初探
- MWEB+七牛 上传图片
- OpenWrt编译
- 代写java程序qq:928900200
- Mysql5.7忘记root密码及修改root密码的方法
- 如何让Oracle释放undo表空间
- LDAP &; Implentation
- 【设计经验】2、ISE中ChipScope使用教程
- HDU 2588 GCD(欧拉函数)
热门文章
- Farm Tour POJ - 2135 (最小费用流)
- 2016-2017 ACM-ICPC CHINA-Final
- 11、python中的函数(基础)
- 读书笔记jvm探秘之二: 对象创建
- JS的跨域理解
- ogre3D学习基础12 --- 让机器人动起来(移动模型动画)
- 如何将Linux rm命令删除的文件放入垃圾箱
- python-day3-内置函数与字符字节之间的转换
- [oldboy-django][2深入python] orm中auto_now =True, antu_now_add=True的应用
- Spring Cloud 目录