子线程加入到主线程中(同时结束)

1.CountDownLatch

import java.util.concurrent.*;

public class Xcc {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        Xcc1 xcc1 = new Xcc1();
        long s1 = System.currentTimeMillis();
        int i = xcc1.test1();
        long e1 = System.currentTimeMillis();
        System.out.println("主线程01-:"+(e1-s1)+"---");
    }
}


class Xcc1{
    
    
    int test1() throws InterruptedException {
    
    
      final CountDownLatch  countDownLatch = new CountDownLatch(1);
      ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
      ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        long s = System.currentTimeMillis();
        Thread thread = new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                try {
    
    
                    // 打印正在执行的缓存线程信息
                    System.out.println(Thread.currentThread().getName()
                            + "正在被执行");
                    Thread.sleep(4000);
                    long e = System.currentTimeMillis();
                    System.out.println("子线程01-:"+(e-s));
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
                System.out.println(123);
            }
        });
        //fixedThreadPool.execute(thread);
        Future<?> submit = fixedThreadPool.submit(thread);
        countDownLatch.await();//线程等待子线程
        long e2 = System.currentTimeMillis();
        System.out.println("主线程02-:"+(e2-s));
        //thread.stop();
        fixedThreadPool.shutdown();
        return 1;
    }

}
pool-1-thread-1正在被执行
子线程01-:4045
123
主线程02-:4045
主线程01-:4050---

https://www.cnblogs.com/lixin-link/p/10998058.html

2.BlockingQueue

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class xcc2 {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        testxc02 testxc02 = new testxc02();
        testxc02.test();
    }
}

class testxc02{
    
    
    public String  test() throws InterruptedException {
    
    
        //线程池
        ExecutorService executor =  Executors.newFixedThreadPool(3);
        BlockingQueue queue = new ArrayBlockingQueue(1);//数组型队列,长度为1
        Thread t = new Thread(() -> {
    
    
            try {
    
    
                Thread.sleep(2000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("t Over");
            try {
    
    
                queue.put("OK");//在队列中加入数据
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        });
        long start = System.currentTimeMillis();
        System.out.println("start = " + start);
        //t.start();
        executor.submit(t);
        try {
    
    
            queue.take();//主线程在队列中获取数据,take()方法会阻塞队列,ps还有不会阻塞的方法
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("end = " + end);
        System.out.println("end - start = " + (end - start));
        executor.shutdown();
        return (String) queue.take();
    }
}

3.Future

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class xcc3_future {
    
    
    public static void main(String[] args) {
    
    
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Thread t = new Thread(() -> {
    
    
            try {
    
    
                Thread.sleep(3050);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println("t Over");
        });
        long start = System.currentTimeMillis();
        System.out.println("start = " + start);
        Future future = executorService.submit(t);//子线程启动
        try {
    
    
            future.get();//需要捕获两种异常
        }catch (InterruptedException e){
    
    
            e.printStackTrace();
        }catch (ExecutionException e){
    
    
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("end = " + end);
        System.out.println("end - start = " + (end - start));
        executorService.shutdown();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/119714896