本文共 3993 字,大约阅读时间需要 13 分钟。
使用多线程可以提高程序执行的效率,比如我们常见的多线程下载,批量发送文件等功能,常见的实现多线程的方法如下。多线程知识总结见:。
public class MyRunnable implements Runnable { private static AtomicInteger count = new AtomicInteger(0); @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName() + ": " + count.incrementAndGet()); } } public static void main(String[] args) { MyRunnable runnable = new MyRunnable(); Thread t1 = new Thread(runnable); Thread t2 = new Thread(runnable); Thread t3 = new Thread(runnable); t1.start(); t2.start(); t3.start(); }}
public class MyThread extends Thread { private static AtomicInteger count = new AtomicInteger(0); @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName() + ": " + count.incrementAndGet()); } } public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); MyThread t3 = new MyThread(); t1.start(); t2.start(); t3.start(); }}
public class MyInnerThread { private static AtomicInteger count = new AtomicInteger(0); public static void main(String[] args){ Thread t1=new Thread(new Runnable() { @Override public void run() { for(int i=0;i<20;i++){ System.out.println(Thread.currentThread().getName() + ": " + count.incrementAndGet()); } } }); t1.start(); new Thread(new Runnable() { @Override public void run() { for(int i=0;i<20;i++){ System.out.println(Thread.currentThread().getName() + ": " + count.incrementAndGet()); } } }).start(); //lambda 表达式 new Thread(() -> { for(int i=0;i<20;i++){ System.out.println(Thread.currentThread().getName() + ": " + count.incrementAndGet()); } }).start(); }}
我们使用 Thread 或者 Runnable 实现的多线程,在线程执行完毕后,不能返回结果。我们可以使用 Callable + Future 的方式实现多线程,Callable 执行任务,Future 获取结果
public class MyCallable { public static void main(String[] args) { CallableTest callable = new CallableTest(); //1.执行Callable方式,需要FutureTask实现类的支持,用于接收运算结果 FutureTaskresult = new FutureTask (callable); new Thread(result).start(); //2.接收线程运算后的结果 try { Integer sum = result.get(); System.out.println(sum); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }}class CallableTest implements Callable { @Override public Integer call() throws Exception { int sum = 0; for (int i = 0; i < 20; i++) { sum++; } return sum; }}
FutureTask 提供了以下方法:
V get()
: 异步获取结果,如果没有结果,该方法阻塞直到计算完成
V get(Long timeout , TimeUnit unit)
:异步获取结果,但有时间限制,超出限制,抛出异常 boolean isDone()
: 任务十分执行完成,正常结果,抛出异常,中途取消都返回 true boolean isCanceller()
:如果任务提前取消,返回 true boolean cancel(boolean mayInterruptRunning)
: 使用 cancel(true) 尝试以中断线程的方式取消任务,取消成功,返回 true,如果任务还没开始或者已经完成调用的话但会返回 false,boolean mayInterruptRunning 表示十分以中断线程的方式取消任务。 其作用总结为:
1、动态获取结果 2、判断任务十分执行完成 3、取消任务 4、任务十分取消创建一个固定大小的线程池,线程池的创建分为多种,具体我们下一篇分析。
public class MyThreadPool { private static AtomicInteger count = new AtomicInteger(0); public static void main(String[] args){ //创建固定大小的线程池 ExecutorService executorService = Executors.newFixedThreadPool(3); for (int i = 0; i <3 ; i++) { executorService.submit(()->{ for (int j = 0; j < 20; j++) { System.out.println(Thread.currentThread().getName() + ": " + count.incrementAndGet()); } }); } }}
—— 完
我的 Github:
CSDN: 个人网站:
推荐阅读
转载地址:http://iffsi.baihongyu.com/