多线程常用的5种-线程模型

在处理业务的时候,有时候需要根据情况使用不同的线程处理模型来处理业务逻辑,这里演示一下常见的线程模型使用技巧。

1、Future模型

前面的章节中提到过Future模型,该模型通常在使用的时候需要结合Callable接口配合使用。Future:未来的、将来的,再结合Callable大概可以明白其功能。

Future是把结果放在将来获取,当前主线程并不急于获取处理结果。允许子线程先进行处理一段时间,处理结束之后就把结果保存下来,当主线程需要使用的时候再向子线程索取。

Callable是类似于Runnable的接口,其中call方法类似于run方法,所不同的是run方法不能抛出受检异常没有返回值,而call方法则可以抛出受检异常并可设置返回值。两者的方法体都是线程执行体。

/**

* When an object implementing interface Runnable is used

* to create a thread, starting the thread causes the object's

* run method to be called in that separately executing

* thread.

*

* The general contract of the method run is that it may

* take any action whatsoever.

*

* @see java.lang.Thread#run()

*/

public abstract void run();

/**

* Computes a result, or throws an exception if unable to do so.

*

* @return computed result

* @throws Exception if unable to compute a result

*/

V call() throws Exception;

注意这里,无法抛出受检异常不等于无法捕获线程中throws的异常。run方法执行体中抛出异常是可以被捕获的,前提是使用Future来处理,后面会有说明。

如果有一种场景需要一个线程处理一段业务,处理结束之后主线程将会使用处理结果进行后续处理。这样,按照普通逻辑,就需要使用到一个全局变量来保存子线程处理之后的结果。子线程处理结束之后,把结果保存在全局变量中供主线程进行调用。一旦涉及到全局能量便存在着多线程读写全局变量错误的风险。而使用Future模式便可以省去全局变量的使用,直接从线程中获取子线程处理结果。下面看一下使用示例;

package thread.blogs.threadmodel;

/**

* Created by PerkinsZhu on 2017/9/1 15:34.

*/

public class AbstractModel {

protected static void sleep(int time) {

try {

Thread.sleep(time);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

protected static void println(Object info) {

System.out.println(info);

}

}

package thread.blogs.threadmodel;

import java.util.concurrent.*;

/**

* Created by PerkinsZhu on 2017/9/1 15:32.

*/

public class FutureModel extends AbstractModel {

public static void main(String[] args) {

testFuture();

}

/**

* 区别: CallAble 可以有返回值 可以抛出受检异常

* Runnable 没有返回值 无法抛出受检异常但可捕获线程中发生的异常。

* 者都可通过对future.get()进行try cathch捕获异常

*/

private static void testFuture() {

MyCallable myCallable = new MyCallable();

MyRunnable myRunnable = new MyRunnable();

ExecutorService executorService = Executors.newFixedThreadPool(5);

Future future = executorService.submit(myCallable);

sleep(2000);

try {

//String data = future.get(2000, TimeUnit.MILLISECONDS);//可以指定超时时间

Object data = future.get();//当执行Runnable的时候,这里返回的为nul。此时如果有run方法体中有异常异常抛出,可以在此捕获到,虽然Run方法没有显示的抛出受检异常。

println(data + "---" + data.getClass().toString());

} catch (InterruptedException e) {

println(e.getMessage());

} catch (ExecutionException e) {

println(e.getMessage());

} catch (Exception e) {

println(e.getMessage());

}

executorService.shutdown();

}

static class MyCallable implements Callable {

@Override

public String call() throws Exception {

sleep(500);

println("I am Callable...");

//int num = 10/0;

//throw new RuntimeException("异常");

return "hello";

}

}

static class MyRunnable implements Runnable {

@Override

public void run() {//不支持返回值,无法对线程捕获异常。

sleep(500);

println("I am Runnable...");

// int num = 10/0;

//throw new RuntimeException("异常");

}

}

}

可以取消注释 分别测试 myCallable 和myRunnable 对异常捕获和结果获取进行测试。

2、fork&join 模型 该模型是jdk中提供的线程模型。该模型包含递归思想和回溯思想,递归用来拆分任务,回溯用合并结果。 可以用来处理一些可以进行拆分的大任务。其主要是把一个大任务逐级拆分为多个子任务,然后分别在子线程中执行,当每个子线程执行结束之后逐级回溯,返回结果进行汇总合并,最终得出想要的结果。这里模拟一个摘苹果的场景:有100棵苹果树,每棵苹果树有10个苹果,现在要把他们摘下来。为了节约时间,规定每个线程最多只能摘10棵苹树以便于节约时间。各个线程摘完之后汇总计算总苹果树。代码实现如下:

package thread.blogs.threadmodel;

import scala.Console;

import java.util.concurrent.*;

import java.util.concurrent.locks.ReentrantLock;

/**

* Created by PerkinsZhu on 2017/9/5 13:05.

*/

public class ForkJoin {

public static void main(String[] args) {

testAcation();

}

private static void testAcation() {

ForkJoinPool pool = new ForkJoinPool();

ForkJoinTask future = pool.submit(new ResultTask(100));//共100棵苹果树

try {

Console.println(future.get());

pool.awaitTermination(1000, TimeUnit.MILLISECONDS);

} catch (Exception e) {

e.printStackTrace();

}

pool.shutdown();

}

}

class ResultTask extends RecursiveTask { //也可继承自RecursiveAction抽象类,区别在于compute方法没有返回值,如果只需要执行动作则可以使用该接口

private int treeNum;

public ResultTask(int num) {

this.treeNum = num;

}

@Override

protected Integer compute() {

if (treeNum < 10) {//每个线程最多只能摘10棵苹果树

return getAppleNum(treeNum);

} else {

//对任务进行拆分,注意这里不仅仅可以一分为二进行拆分,也可以拆为多个子任务

int temp = treeNum / 2;

ResultTask left = new ResultTask(temp);

ResultTask right = new ResultTask(treeNum - temp);

left.fork();

right.fork();

//对子任务处理的结果进行合并

int result = left.join() + right.join();

Console.println("========" + Thread.currentThread().getName() + "=========" + result);

return result;

}

}

public Integer getAppleNum(int treeNum) {

return treeNum * 10;//每棵树上10个苹果

}

}

这里需要看一下执行结果,主要是为了明白在拆分子任务的时候并不是无限制开启线程,而是使用了线程池ForkJoinPool复用线程。注意下面输出的线程名称!

========ForkJoinPool-1-worker-3=========120

========ForkJoinPool-1-worker-7=========120

========ForkJoinPool-1-worker-0=========120

========ForkJoinPool-1-worker-5=========120

========ForkJoinPool-1-worker-1=========130

========ForkJoinPool-1-worker-11=========130

========ForkJoinPool-1-worker-4=========250

========ForkJoinPool-1-worker-7=========130

========ForkJoinPool-1-worker-7=========250

========ForkJoinPool-1-worker-3=========130

========ForkJoinPool-1-worker-5=========250

========ForkJoinPool-1-worker-6=========250

========ForkJoinPool-1-worker-2=========500

========ForkJoinPool-1-worker-3=========500

========ForkJoinPool-1-worker-1=========1000

1000

3、actor消息模型

actor模型属于一种基于消息传递机制并行任务处理思想,它以消息的形式来进行线程间数据传输,避免了全局变量的使用,进而避免了数据同步错误的隐患。actor在接受到消息之后可以自己进行处理,也可以继续传递(分发)给其它actor进行处理。在使用actor模型的时候需要使用第三方Akka提供的框架点击查看。

4、生产者消费者模型

生产者消费者模型都比较熟悉,其核心是使用一个缓存来保存任务。开启一个/多个线程来生产任务,然后再开启一个/多个来从缓存中取出任务进行处理。这样的好处是任务的生成和处理分隔开,生产者不需要处理任务,只负责向生成任务然后保存到缓存。而消费者只需要从缓存中取出任务进行处理。使用的时候可以根据任务的生成情况和处理情况开启不同的线程来处理。比如,生成的任务速度较快,那么就可以灵活的多开启几个消费者线程进行处理,这样就可以避免任务的处理响应缓慢的问题。使用示例如下:

package thread.blogs.threadmodel;

import java.util.LinkedList;

import java.util.Queue;

import java.util.UUID;

/**

* Created by PerkinsZhu on 2017/9/22 8:58.

*/

public class PCModel {

public static void main(String[] args) {

testPCModel();

}

private static Queue queue = new LinkedList();//任务缓存,这里保存简单的字符串模拟任务

private static void testPCModel() {

new Thread(() -> {//生产者线程

while (true) {

String uuid = UUID.randomUUID().toString();

queue.add(uuid);

sleep(100);

}

}).start();

for (int i = 0; i < 10; i++) {//开启10消费者处理任务,保证生产者产生的任务能够被及时处理

new Thread(() -> {

while (true) {

doWork(queue);

}

}).start();

}

}

private static void doWork(Queue queue) {

sleep(1000);

synchronized (queue) {

if (queue.size() > 0) {

sleep(10);

System.out.println(queue.poll() + "----" + queue.size());

}

}

}

private static void sleep(int time) {

try {

Thread.sleep(time);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

5、master-worker模型

master-worker模型类似于任务分发策略,开启一个master线程接收任务,然后在master中根据任务的具体情况进行分发给其它worker子线程,然后由子线程处理任务。如需返回结果,则worker处理结束之后把处理结果返回给master。下面的代码示例是使用akka actor for scala演示。使用的时候也可以使用java Thread来实现该模型。

package thread.blogs.threadmodel

import akka.actor.{Actor, ActorSystem, Props}

/**

* Created by PerkinsZhu on 2017/9/21 18:58.

*/

object ActorTest {

val actorSystem = ActorSystem("Master")

def main(args: Array[String]): Unit = {

val actor = actorSystem.actorOf(Props[Master], "Master")

var taskNum = 0;

while (true) {

taskNum = taskNum + 1;

actor ! Task("做作业! --" + taskNum) //发送消息给actor

Thread.sleep(100)

}

}

}

class Master extends Actor {

val actorSystem = ActorSystem("worker")

var num = 0;

override def receive: Receive = {

case task: Task => {

num = num + 1;

//接收到任务之后分发给其它worker线程。可以使用worker池 复用actor

actorSystem.actorOf(Props[Worker], "worker" + num) ! task

}

case any: Any => println(any)

}

}

class Worker extends Actor {

def doWork(task: Task): Unit = println(task.name)

override def receive: Receive = {

case task: Task => doWork(task) //worker处理接受到的任务

case any: Any => println(any)

}

}

case class Task(name: String)

这里如果需要worker返回处理结果,则只需要在worker中调用sender 发送处理结果即可。