Java 基于多线程Callable接口和Future接口 代码实例演示(二)

 以下使用Callable接口和Future接口 结合项目业务设计接口开发

包结构

 

ste1: 定义线程任务返回状态枚举 ThreadStatus

package com.cn.java.base.thread;


public enum ThreadStatus {
    SUCCESS,

    FAILURE;
}

step2: 定义线程业务抽象接口  AbstractThread

package com.cn.java.base.thread;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public abstract class AbstractThread {

    public void handle() throws Exception {

        log.info("start " + this.getClass().getSimpleName() + " thread...");
        this.doHandler();
        log.info(this.getClass().getSimpleName() + " thread end.");

    }

    protected abstract void doHandler() throws Exception;

}

step3: 定义具体业务实现任务  TestThread

package com.cn.java.base.thread.test;

import com.cn.java.base.thread.AbstractThread;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class TestThread extends AbstractThread {

    /**
     * 执行具体业务需求
     */
    @Override
    protected void doHandler() throws Exception {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            log.info(" 任务名称:{}, 执行时间 : {} 秒", Thread.currentThread().getName(), (i + 1));
        }
    }
}

step4:定义线程委托类实现callable接口  ThreadDelegator

package com.cn.java.base.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.concurrent.Callable;


@Slf4j
@Component
public class ThreadDelegator implements Callable<ThreadStatus> {

    private AbstractThread service;

    public void initService(AbstractThread service) {
        this.service = service;
    }

    @Override
    public ThreadStatus call(){
        ThreadStatus status = ThreadStatus.SUCCESS;
        try{
            service.handle();
        }catch (Exception e){
            status = ThreadStatus.FAILURE;
        }
        return status;
    }
}

step5:定义线程调度管理器  ThreadManager

package com.cn.java.base.thread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Component;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@Component
public class ThreadManager {

    ThreadPoolExecutor manager = new ThreadPoolExecutor(10,
            10,
            30,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue(30));

    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    public Future<ThreadStatus> submit(AbstractThread thread){
        capableBeanFactory.autowireBean(thread);
        ThreadDelegator delegator = capableBeanFactory.getBean(ThreadDelegator.class);
        delegator.initService(thread);
        return manager.submit(delegator);
    }

}

step6:调用演示

package com.cn.java.base.controller;

import com.cn.java.base.common.utils.R;
import com.cn.java.base.service.AsyncService;
import com.cn.java.base.thread.ThreadManager;
import com.cn.java.base.thread.ThreadStatus;
import com.cn.java.base.thread.test.TestThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Future;

@Slf4j
@RestController
@RequestMapping("test")
public class TestController {


    @Autowired
    private ThreadManager manager;

    @GetMapping("testThread")
    public R testThread(){
        TestThread thread1 = new TestThread();
        TestThread thread2 = new TestThread();
        /**
         * 线程任务返回结果
         * 使用场景:
         * 1,可用作异步任务处理
         * 2,可用作线程任务返回的结果处理后续业务
         *      Future提供了三种功能:
         *          1:判断任务是否完成;
         *          2:能够中断任务;
         *          3:能够获取线程任务执行结果。
         *   Callable接口和Future接口 介绍:
         *      https://blog.csdn.net/nameIsHG/article/details/106750740
         */
        Future<ThreadStatus> resutl1 =  manager.submit(thread1);
        Future<ThreadStatus> resutl2 =  manager.submit(thread2);
        return R.ok().put("data","success");
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值