「java等待子线程」java主线程等待子线程结束再返回
本篇文章给大家谈谈java等待子线程,以及java主线程等待子线程结束再返回对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Java主线程如何等待子线程执行结束
- 2、java 如何实现等待子线程结束
- 3、java 主线程必须等待子线程完成吗
- 4、Java如何等待子线程执行结束
- 5、如何实现java主线程等待子线程执行完毕之后再执行
Java主线程如何等待子线程执行结束
java.util.concurrent.CountDownLatch 这个类可以实现你所要的功能
例如:CountDownLatch latch = new CountDownLatch(5) //声明计数器为5个
Thread t = new Thread() {
public void run() {
try {
//TODO 你的应用
} catch (Exception e) {
//TODO 异常处理
}
finally {
latch.countDown(); //这句是关键
System.out.println("ok"); //5个线程都跑完后输出
}
}
};
t.start();
然后让以上操作循环五次(就是说同时开5个线程),那么这个"ok"就会在等到这5个线程都ok后才会被输出一次。
java 如何实现等待子线程结束
有多种实现方式,下面列出两种。
第一种:实现Callable类,使用有返回值的线程,只有线程执行完成后才会返回结果。
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
public class Main {
// 初始化一个容量为10的线程池
static final ExecutorService pool = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
ListFutureString futures = new ArrayList();
for (int i = 0; i 3; i++) {
MyThread thread = new MyThread("线程" + i);
futures.add(pool.submit(thread));
}
for (FutureString future : futures) {
String name = future.get();
System.out.println(name + "执行完成...");
}
System.out.println("所有线程执行完成!");
}
}
class MyThread implements CallableString {
private String name;
public MyThread(String name) {
this.name = name;
}
@Override
public String call() throws Exception {
// TODO 执行业务
// 随机延迟,模拟线程耗时
Thread.sleep(1000 + new Random().nextInt(2000));
return name;
}
}
第二种:使用CountDownLatch实现线程计数,代码如下:
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main2 {
// 初始化一个容量为10的线程池
static final ExecutorService pool = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws InterruptedException {
int threadCount = 3;
// 初始化CountDownLatch,用于线程计数
CountDownLatch latch = new CountDownLatch(threadCount);
for (int i = 0; i threadCount; i++) {
MyThread thread = new MyThread("线程" + i, latch);
pool.execute(thread);
}
// 阻塞当前线程,CountDownLatch计数减为0时表示所有线程都执行完毕,才会释放主线程的阻塞
latch.await();
System.out.println("所有线程执行完成!");
}
}
class MyThread implements Runnable {
private String name;
private CountDownLatch latch;
public MyThread(String name, CountDownLatch latch) {
this.name = name;
this.latch = latch;
}
@Override
public void run() {
// TODO 执行业务
// 随机延迟,模拟线程耗时
try {
Thread.sleep(1000 + new Random().nextInt(2000));
} catch (InterruptedException e) {
}
// 计数减一
latch.countDown();
System.out.println(name + "执行完毕...");
}
}
java 主线程必须等待子线程完成吗
//不是的,看截图吧!
public class 等待
{
public static void main(String[] args)
{
System.out.println("\n\t\t==========java 主线程必须等待子线程完成吗!==========\n");
init();
}//初始化!
private static void init()
{
new Thread("线程1")
{
public void run()
{
for (int i=0;i10 ;i++ )
{
System.out.println(Thread.currentThread().getName()+"---i="+i);
}
System.out.println(Thread.currentThread().getName()+"结束!!");
}
}.start();
new Thread("线程2")
{
public void run()
{
for (int i=0;i10 ;i++ )
{
System.out.println(Thread.currentThread().getName()+"---i="+i);
}
System.out.println(Thread.currentThread().getName()+"结束!!");
}
}.start();
System.out.println("主线程结束!!");
}
}
Java如何等待子线程执行结束
先调用
shutdown
在调用
isTerminated
例:
/*
* 采用线程池开启多个子线程,主线程等待所有的子线程执行完毕
*/
public static void moreThread() {
try {
int threadNum = 0;
for (int i = 0; i 10; i++) {
threadNum++;
final int currentThreadNum = threadNum;
exe.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("子线程[" + currentThreadNum + "]开启");
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("子线程[" + currentThreadNum + "]结束");
}
}
});
}
System.out.println("已经开启所有的子线程");
exe.shutdown();
System.out.println("shutdown():启动一次顺序关闭,执行以前提交的任务,但不接受新任务。");
while(true){
if(exe.isTerminated()){
System.out.println("所有的子线程都结束了!");
break;
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("主线程结束");
}
}
如何实现java主线程等待子线程执行完毕之后再执行
java.util.concurrent.CountDownLatch 这个类可以实现你所要的功能
例如:CountDownLatch latch = new CountDownLatch(5) //声明计数器为5个
Thread t = new Thread() {
public void run() {
try {
//TODO 你的应用
} catch (Exception e) {
//TODO 异常处理
}
finally {
latch.countDown(); //这句是关键
System.out.println("ok"); //5个线程都跑完后输出
}
}
};
t.start();
然后让以上操作循环五次(就是说同时开5个线程),那么这个"ok"就会在等到这5个线程都ok后才会被输出一次。
关于java等待子线程和java主线程等待子线程结束再返回的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。