java如何做到同时执行?多个线程同时执行方法

我们在开发程序的时候,需要多个线程执行任务,每个线程都执行不同的准备工作,但我们要同时执行这些程序要怎么做呢?接下来我们就来给大家讲解一下这方面的内容。

一是用CyclicBarrier (适用在多线程相互等待,直到到达一个屏障点。并且CyclicBarrier是可重用的。)实现。

代码实现:

public class CyclicBarrierTest
{
    boolean finalFlg = false;
    /**
    * CyclicBarrier 适用再多线程相互等待,直到到达一个屏障点。并且CyclicBarrier是可重用的。
    */
    CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
    private void runThread()
    {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            executorService.submit(createThread(i));
        }
    }
    private Thread createThread(int i)
    {
        Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    cyclicBarrier.await();
                    System.out.println("Thread:" + Thread.currentThread()
                        .getName() + "准备完毕,time:" + System.currentTimeMillis());
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                catch (BrokenBarrierException e)
                {
                    e.printStackTrace();
                }
            }
        });
        thread.setName("name" + i);
        return thread;
    }
    public static void main(String[] args)
    {
        CyclicBarrierTest test = new CyclicBarrierTest();
        test.runThread();
    }
}

执行结果:

Thread:pool-1-thread-10准备完毕,time:1521037864652

Thread:pool-1-thread-1准备完毕,time:1521037864652

Thread:pool-1-thread-3准备完毕,time:1521037864652

Thread:pool-1-thread-2准备完毕,time:1521037864652

Thread:pool-1-thread-7准备完毕,time:1521037864652

Thread:pool-1-thread-6准备完毕,time:1521037864652

Thread:pool-1-thread-5准备完毕,time:1521037864652

Thread:pool-1-thread-4准备完毕,time:1521037864652

Thread:pool-1-thread-9准备完毕,time:1521037864652

Thread:pool-1-thread-8准备完毕,time:1521037864652

从结果看,线程几乎是同时执行的。

二是用CountDownLatch 实现,CountDownLatch可以理解为一个计数器,直到计数器为0了,等待的线程才执行。

public class testCountDownLatch
{
    CountDownLatch countDownLatch = new CountDownLatch(1);
    private void runThread()
    {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            executorService.submit(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        countDownLatch.await();
                        System.out.println("Thread:" + Thread.currentThread()
                            .getName() + ",time: " + System.currentTimeMillis());
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
        countDownLatch.countDown();
    }
    public static void main(String[] args)
    {
        testCountDownLatch test = new testCountDownLatch();
        test.runThread();
    }
}

从执行结果看,线程也几乎同时进行的:

Thread:pool-1-thread-2,time: 1521038129807

Thread:pool-1-thread-3,time: 1521038129807

Thread:pool-1-thread-1,time: 1521038129807

Thread:pool-1-thread-4,time: 1521038129807

Thread:pool-1-thread-6,time: 1521038129807

Thread:pool-1-thread-5,time: 1521038129807

Thread:pool-1-thread-9,time: 1521038129807

Thread:pool-1-thread-8,time: 1521038129807

Thread:pool-1-thread-7,time: 1521038129807

Thread:pool-1-thread-10,time: 1521038129807

其他自己用wait和notify实现。

java程序同时执行的话,可以利用以上的两个方法去执行,当然具体实施哪种方法,大家可以根据自己需求去选择!最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。

推荐阅读:

java创建对象new方法是什么?java创建对象方法有哪些?

怎么学习java编程?学习java难吗?

java有哪些新技术?java技术哪些一定要学?