Thread-Per-Message 设计模式

一 点睛

Thread-Per-Message 的意思是为每一个消息的处理开辟一个线程使得消息能够以并发的方式进行处理,从而提供系统整体的吞吐能力。这就好比电话接线员一样,收到每一个电话投诉或者业务处理请求,都会提交对应的工单,然后交由对应的工作人员来处理。

二 实战

1 业务请求

package concurrent.threadpermessage;

/**
* @className: Request
* @description: 业务请求
* @date: 2022/4/27
* @author: cakin
*/
public class Request {
    private final String business;

    public Request(String business) {
        this.business = business;
    }

    @Override
    public String toString() {
        return business;
    }
}

2 业务受理

package concurrent.threadpermessage;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
* @className: TaskHandler
* @description: 处理每一个提交的 Request
* @date: 2022/4/27
* @author: cakin
*/
public class TaskHandler implements Runnable {
    // 需要处理的 Request 请求
    private final Request request;

    public TaskHandler(Request request) {
        this.request = request;
    }

    @Override
    public void run() {
        System.out.println("Begin handle " + request);
        try {
            TimeUnit.SECONDS.sleep(new Random().nextInt(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("End handle " + request);
    }
}

3 测试

package concurrent.threadpermessage;

public class Operator {
    public void call(String business) {
        TaskHandler taskHandler = new TaskHandler(new Request(business));
        new Thread(taskHandler).start();
    }

    public static void main(String[] args) {
        Operator operator = new Operator();
        operator.call("银行业务");
        operator.call("花园业务");
        operator.call("木材业务");
        operator.call("学校业务");
        operator.call("培训业务");
    }
}

三 测试结果

Begin handle 木材业务

Begin handle 学校业务

Begin handle 银行业务

Begin handle 花园业务

Begin handle 培训业务

End handle 花园业务

End handle 银行业务

End handle 培训业务

End handle 木材业务

End handle 学校业务

四 代码升级——构造成线程池

1 代码

package concurrent.threadpermessage;

import concurrent.threadpool.BasicThreadPool;
import concurrent.threadpool.ThreadPool;

public class Operator1 {
    // 使用线程池替代为每一个请求创建线程
    private final ThreadPool threadPool = new BasicThreadPool(2, 6, 4, 100);

    public void call(String business) {
        TaskHandler taskHandler = new TaskHandler(new Request(business));
        threadPool.execute(taskHandler);
    }

    public static void main(String[] args) {
        Operator1 operator = new Operator1();
        operator.call("银行业务");
        operator.call("花园业务");
        operator.call("木材业务");
        operator.call("学校业务");
        operator.call("培训业务");
    }
}

2 测试效果

Begin handle 银行业务

Begin handle 花园业务

End handle 花园业务

Begin handle 木材业务

End handle 木材业务

Begin handle 学校业务

End handle 银行业务

Begin handle 培训业务

End handle 培训业务

End handle 学校业务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值