首页 > 编程 > Java > 正文

JAVA多线程实现的三种方式

2019-11-11 06:32:14
字体:
来源:转载
供稿:网友

  春节还未结束,业务请求还处于高峰期,于是暴露了请求过多、响应时间过长的问题。针对这种问题的优化,揪其原因,在于处理业务请求是单线程处理,请求过多时,响应慢。将业务处理代码改为多线程处理,即解决了问题。   今天浅谈下java多线程的几种实现方式。   JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。

1. 继承Thread类实现多线程

  继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实例,它代表一个线程的实例,并且,启动线程的唯一方法就是通过Thread类的start()实例方法。start()方法是一个native方法,它将启动一个新线程,并执行run()方法。这种方式实现多线程很简单,通过自己的类直接extend Thread,并复写run()方法,就可以启动新线程并执行自己定义的run()方法。例如:

public class MyThread extends Thread {  public void run() {   System.out.PRintln("MyThread.run()");  }}

在合适的地方启动线程如下:

MyThread myThread1 = new MyThread();MyThread myThread2 = new MyThread();myThread1.start();myThread2.start();

2. 实现Runnable接口方式实现多线程

如果自己的类已经extends另一个类,就无法直接extends Thread,此时,必须实现一个Runnable接口,如下:

public class MyThread extends OtherClass implements Runnable {  public void run() {   System.out.println("MyThread.run()");  }}

为了启动MyThread,需要首先实例化一个Thread,并传入自己的MyThread实例:

MyThread myThread = new MyThread();Thread thread = new Thread(myThread);thread.start();

事实上,当传入一个Runnable target参数给Thread后,Thread的run()方法就会调用target.run(),参考JDK源代码:

public void run() {  if (target != null) {   target.run();  }}

3. 使用ExecutorService、Callable、Future实现有返回结果的多线程

  ExecutorService、Callable、Future这个对象实际上都是属于Executor框架中的功能类。返回结果的线程是在JDK1.5中引入的新特征,确实很实用,有了这种特征我就不需要再为了得到返回值而大费周折了,而且即便实现了也可能漏洞百出。 可返回值的任务必须实现Callable接口,类似的,无返回值的任务必须Runnable接口。执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了,再结合线程池接口ExecutorService就可以实现传说中有返回结果的多线程了。下面提供了一个完整的有返回结果的多线程测试例子,在JDK1.5下验证过没问题可以直接使用。代码如下:

//有返回值的多线程处理 public static void main(String[] args) throws InterruptedException, ExecutionException { // 创建一个线程池,大小为3 ExecutorService executorService = Executors.newFixedThreadPool(3); // 创建多个有返回值的任务 List<Future> list = new ArrayList<Future>(); for (int i = 0; i < 3; i++) { Callable c = new MyCallable(i + " "); // 执行任务并获取Future对象 Future f = executorService.submit(c); System.out.println(">>>" + f.get()); list.add(f); } }}class MyCallable implements Callable<Object> { private String taskNum; MyCallable(String taskNum) { this.taskNum = taskNum; } public Object call() throws Exception { System.out.println(">>>" + taskNum + "任务启动"); Date dateTmp1 = new Date(); Thread.sleep(1000); Date dateTmp2 = new Date(); long time = dateTmp2.getTime() - dateTmp1.getTime(); System.out.println(">>>" + taskNum + "任务终止"); return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】"; } }

运行结果 这里写图片描述

// 无返回值的多线程处理 public static void main(String[] args) throws InterruptedException, ExecutionException { // 创建一个线程池,大小为3 ExecutorService executorService = Executors.newFixedThreadPool(3); // 创建无返回值的任务 for (int i = 0; i < 3; i++) { // 执行任务 executorService.execute(new MyRunnable(i+"")); } //关闭线程池 executorService.shutdown(); }}class MyRunnable implements Runnable{ private String taskNum; MyRunnable(String taskNum) { this.taskNum = taskNum; } @Override public void run() { System.out.println(">>>" + taskNum + "任务启动"); Date dateTmp1 = new Date(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Date dateTmp2 = new Date(); long time = dateTmp2.getTime() - dateTmp1.getTime(); System.out.println(">>>" + taskNum + "任务终止"); System.out.println(taskNum + "任务运行时间【" + time + "毫秒】"); }}

运行结果 这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表