1)、继承 Thread
2)、实现 Runnable 接口
3)、实现 Callable 接口 + FutureTask (可以拿到返回结果,可以处理异常)
4)、线程池
提示
Executors.newFiexedThreadPool(3);
//或者
new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit unit, workQueue, threadFactory, handler);
通过线程池性能稳定,也可以获取执行结果,并捕获异常。但是,在业务复杂情况下,一个异步调用可能会依赖于另一个异步调用的执行结果
public class ThreadTestDemo {
public static void main(String[] args) {
System.out.println("main.....start......");
Thread01 thread01 = new Thread01();
thread01.start(); //启动线程
System.out.println("main.....end......");
}
public static class Thread01 extends Thread{
@Override
public void run() {
System.out.println("当前线程是:"+Thread.currentThread().getId());
int i = 8 / 2;
System.out.println("运算结果是:"+i);
}
}
}
结果
public class ThreadTestDemo {
public static void main(String[] args) {
System.out.println("main.....start......");
Runnable01 runnable01 = new Runnable01();
new Thread(runnable01).start();
System.out.println("main.....end......");
}
/**
* 实现 Runnable 接口
*/
public static class Runnable01 implements Runnable{
@Override
public void run() {
System.out.println("当前线程是:"+Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运算结果是:"+i);
}
}
}
public class ThreadTestDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main.....start......");
FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
new Thread(futureTask).start();
//阻塞等待整个线程执行完成,获取返回结果
Integer integer = futureTask.get();
System.out.println("获取到的线程返回结果是:"+integer);
System.out.println("main.....end......");
}
/**
* 实现 Callable 接口 + FutureTask (可以拿到返回结果,可以处理异常)
*/
public static class Callable01 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("当前线程是:"+Thread.currentThread().getId());
int i = 12 / 2;
System.out.println("运算结果是:"+i);
return i;
}
}
}
测试结果
public class ThreadTestDemo {
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main.....start......");
//提交给线程池去执行
executor.execute(new Runnable01());
System.out.println("main.....end......");
}
/**
* 实现 Runnable 接口
*/
public static class Runnable01 implements Runnable{
@Override
public void run() {
System.out.println("当前线程是:"+Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运算结果是:"+i);
}
}
}
执行结果
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
池中一直保持的线程的数量,即使线程空闲。除非设置了 allowCoreThreadTimeOut
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
池中允许的最大的线程数
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating. 当线程数大于核心线程数的时候,线程在最大多长时间没有接到新任务就会终止释放,
最终线程池维持在 corePoolSize 大小
* @param unit the time unit for the {@code keepAliveTime} argument
时间单位
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method. 阻塞队列,用来存储等待执行的任务,如果当前对线程的需求超过了 corePoolSize
大小,就会放在这里等待空闲线程执行。
* @param threadFactory the factory to use when the executor
* creates a new thread
创建线程的工厂,比如指定线程名等
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
拒绝策略,如果线程满了,线程池就会使用拒绝策略。
newCachedThreadPool
newFixedThreadPool
newScheduledThreadPool
newSingleThreadExecutor
降低资源的消耗
提高响应速度
提高线程的可管理性