java并发包有什么类?一般作用是什么?

TheDisguiser 2020-06-17 19:55:07 java常见问答 6080

我们知道,在java的JDK1.5版本后,它为我们引入了一个并发包,用以解决各种开发中的并发问题,小伙伴们知道在这个并发包中都有哪些类吗?下面我们就来详细了解看看吧。

一、CountDownLatch类

作用:允许一个或多个线程等待其他线程完成操作

例:

public class TestCountDown
{
    private static CountDownLatch c = new CountDownLatch(2); //等待线程的执行数量为2
    public static void main(String[] args) throws InterruptedException
    {
        new Thread(
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        System.out.println(1);
                        c.countDown();
                    }
                }
            )
            .start();
        new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    System.out.println(2);
                    c.countDown();
                }
            })
            .start();
        c.await(); //阻塞当前线程,即main线程等待其他线程完成任务以后才能执行
        System.out.println(3);
    }
}

二、Semaphore类

作用:控制同时访问特定资源的线程数量

例:

public class TestSemaphore
{
    public static void main(String[] args)
    {
        // 线程池
        ExecutorService exec = Executors.newCachedThreadPool();
        //一次只能5个线程同时访问
        final Semaphore semp = new Semaphore(5);
        // 模拟20个客户端访问
        for (int index = 0; index < 20; index++)
        {
            final int NO = index;
            Runnable run = new Runnable()
            {
                public void run()
                {
                    try
                    {
                        // 获取许可
                        semp.acquire();
                        System.out.println("Accessing: " + NO);
                        Thread.sleep((long)(Math.random() * 10000));
                        // 访问完后,释放
                        semp.release();
                        System.out.println("-----------------" + semp.availablePermits());
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
            exec.execute(run);
        }
        // 退出线程池
        exec.shutdown();
    }
}

三、Exchanger类

作用:用于线程间数据的交换

public class TestExchanger
{
    public static void main(String[] args)
    {
        Exchanger < String > exchanger = new Exchanger < > ();
        ExecutorService es = Executors.newFixedThreadPool(2); //拥有两个线程的线程池
        es.execute(new Runnable()
        {
            @Override
            public void run()
            {
                String A = "银行流水A";
                try
                {
                    exchanger.exchange(A);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        });
        es.execute(new Runnable()
        {
            @Override
            public void run()
            {
                String B = "银行流水B";
                try
                {
                    String A = exchanger.exchange(B);
                    System.out.println("A和B数据是否一致: " + A.equals(B) + ",A: " + A + ",B: " + B);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
}

以上就是关于java并发包中常用类的全部信息了,如果小伙伴们还需要了解更多相关java架构师知识,可以关注我们的网站来了解详情。

推荐阅读:

java并发包下的类一般都有哪些?java并发包常用类