ThreadPoolExecutor使用submit方法提交Runnable任务定义执行结果的返回值类型

ThreadPoolExecutor使用submit方法提交Runnable任务规定执行结果的返回值

实体类代码如下:

@Data
public class User {
    private String username;
    private String password;
}

线程执行代码如下:

public class ThirdRunnable implements Runnable {

    private User user;

    public ThirdRunnable(User user){
        super();
        this.user=user;
    }

    @Override
    public void run() {
        user.setUsername("Gxin");
        user.setPassword("20220802");
    }

}

运行类代码如下:

@Slf4j
public class NotGetRun {
    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        User user = new User();
        ThirdRunnable thirdRunnable = new ThirdRunnable(user);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 6, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
        Future<User> userFuture = threadPoolExecutor.submit(thirdRunnable, user);
        log.info("开始时间:{}",simpleDateFormat.format(new Date()));
        try {
            User info = userFuture.get();
            log.info("future 获取到的账号:{},密码:{}",info.getUsername(),info.getPassword());
            log.info("结束时间:{}",simpleDateFormat.format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

运行结果如下:
17:27:50.832 [main] INFO com.ozx.concurrentprogram.executor.controller.NotGetRun - 开始时间:17:27:50
17:27:50.846 [main] INFO com.ozx.concurrentprogram.executor.controller.NotGetRun - future 获取到的账号:Gxin,密码:20220802
17:27:50.846 [main] INFO com.ozx.concurrentprogram.executor.controller.NotGetRun - 结束时间:17:27:50
从运行结果看出线程池执行Runnable任务,可以获取任务中返回的对象的相关值,而Callable任务实现Callable接口时,就指定返回值的类型,Runnable任务的返回值类型可以通过方法submit(Runnable r,T result)的第2个参数进行设置,result可作为执行结果的返回值类型,而不需要使用get()方法来进行获得。

使用方法cancel(boolean mayInterruptIfRunning)和isCancelled()

方法cancel(boolean mayInterruptIfRunning)的参数mayInterruptIfRunning的作用是如果线程正在运行则是否中断正在运行的线程,在代码中需要使用if(Thread currentThread().isInterrupted())进行配合使用。
方法cancel()的返回值代表发送取消任务的命令是否成功完成。
线程执行代码如下:

public class TheCallable implements Callable<String> {
    private int number;

    public TheCallable(int number){
        super();
        this.number=number;
    }

    @Override
    public String call() throws Exception {
        Thread.sleep(5000);
        return "返回值:"+number;
    }
}

运行类代码如下:

@Slf4j
public class CancelCallableRun {
    public static void main(String[] args) {
        TheCallable theCallable = new TheCallable(20220804);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
        Future<String> future = threadPoolExecutor.submit(theCallable);
        try {
            String result = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        log.info("取消任务结果:{},发送取消任务的命令执行结果:{}",future.cancel(true),future.isCancelled());
    }
}

运行结果如下:
17:59:36.426 [main] INFO com.ozx.concurrentprogram.executor.controller.CancelCallableRun - 取消任务结果:false,发送取消任务的命令执行结果:false

猜你喜欢

转载自blog.csdn.net/LBWNB_Java/article/details/126241406