啥?

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package com.aliyun.test.learn;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest {

    public static void main(String[] args) {
        System.out.println("主线程");
    }

    private static void reenTrantLockTest() {
        ReentrantLock lock = new ReentrantLock();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                lock.lock();
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                lock.unlock();
            }, String.valueOf(i) + " ======").start();
        }
    }

    private static void semaphoreTest() {
        Semaphore semaphore = new Semaphore(3);
        new Thread(() -> {
            try {
                semaphore.acquire();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            semaphore.release();
        }, "t1").start();

        new Thread(() -> {
            try {
                semaphore.acquire();
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            semaphore.release();
        }, "t2").start();

        new Thread(() -> {
            try {
                semaphore.acquire();
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            semaphore.release();
        }, "t3").start();

        new Thread(() -> {
            try {
                semaphore.acquire();
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            semaphore.release();
        }, "t4").start();
    }

    private static void cyclicBarrierTest() {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        new Thread(() -> {
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }, "t1").start();

        new Thread(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("等我");
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }, "t2").start();

        new Thread(() -> {
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }, "t3").start();
    }

    private static void threadPool() throws InterruptedException, ExecutionException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 2, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());
        threadPoolExecutor.execute(() -> {
            System.out.println("hahaha");
        });
        threadPoolExecutor.shutdown();

        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<Integer> submit = executorService.submit(() -> {
            int a = 0;
            int b = a + 1;
            return b;
        });
        Integer o = submit.get();
        System.out.println(0);
        executorService.shutdown();
    }


    /**
     * CountDownLatch
     * 两个线程同时运行,第三个线程等他们运行完再运行
     */
    private static void test1() {
        CountDownLatch latch = new CountDownLatch(2);

        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            latch.countDown();
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            latch.countDown();
        }, "t2");
        t2.start();

        Thread t3 = new Thread(() -> {
            try {
                latch.await();
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3");
        t3.start();
    }
}

 

posted on 2023-02-20 19:30  啥?  阅读(10)  评论(0编辑  收藏  举报