java 多线程返回结果_Java多线程-处理线程的返回值

本文介绍了三种处理主线程与子线程同步的方法:主线程等待法、通过Thread类的join()阻塞及Callable接口的FutureTask应用。比较了它们的优缺点,展示了如何在Java中利用这些技术确保线程操作的有序性和数据一致性。
摘要由CSDN通过智能技术生成

8d9545579dab57e637caaefb9dd3ff10.png

一、主线程等待法:优点:实现简单,缺点:代码冗余

package com.test.thread;

public class CycleWait implements Runnable {

private String value;

@Override

public void run() {

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

value = "we have data now";

}

public static void main(String[] args) throws InterruptedException {

CycleWait cycleWait = new CycleWait();

Thread t = new Thread(cycleWait);

t.start();

while (cycleWait.value == null) {

Thread.sleep(100);

}

System.out.println(cycleWait.value);

}

}

运行结果:

we have data now

二、使用Thread类的join()阻塞当前线程,以等待子线程处理完毕。优点:比“主线程等待法”更简单 缺点:粒度不够细

package com.test.thread;

public class CycleWait implements Runnable {

private String value;

@Override

public void run() {

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

value = "we have data now";

}

public static void main(String[] args) throws InterruptedException {

CycleWait cycleWait = new CycleWait();

Thread t = new Thread(cycleWait);

t.start();

// join方法,在start后

t.join();

System.out.println(cycleWait.value);

}

}

三、通过Callable接口实现:通过FutureTask 或者 线程池获取

1、future task

MyCallable.class:

package com.test.thread;

import java.util.concurrent.Callable;

// 实现callable接口

public class MyCallable implements Callable {

@Override

public String call() throws Exception {

String value = "test";

System.out.println("ready to work");

Thread.sleep(5000);

System.out.println("task done");

return value;

}

}

FutureTaskDemo.class:

package com.test.thread;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

//future task

public class FutureTaskDemo {

public static void main(String[] args) throws ExecutionException, InterruptedException {

FutureTask task = new FutureTask(new MyCallable());

new Thread(task).start();

if (!task.isDone()) {

System.out.println("task has not finished, please wait!");

}

System.out.println("task return:" + task.get());

}

}

2、线程池

TreadPoolDemo.class:

package com.test.thread;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class TreadPoolDemo {

public static void main(String[] args) {

// 创建线程池

ExecutorService executorService =

Executors.newCachedThreadPool();

// 向线程池中提交任务

Future future = executorService.submit(new MyCallable());

// 判断任务是否完成

if (!future.isDone()) {

System.out.println("task not finished, please wait~~");

}

try {

System.out.println(future.get());

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

} finally {

// 将线程池关闭

executorService.shutdown();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值