Java并发的例子

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Main {
    private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
    final static  CyclicBarrier barrier = new CyclicBarrier(4);
    public static void main(String[] args) {
         int data[] = new int[4];
        for(int i=0;i<=3; i++){
            final int j=i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    data[j]=j;
//                    System.out.println(j+"----" + Thread.currentThread().getName());
//                   System.out.println(data[j]);
                    try{
                        barrier.await();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            });
        }

//        try {
//            Thread.sleep(30000); //1000 毫秒,也就是1秒.
//        } catch(InterruptedException ex) {
//            Thread.currentThread().interrupt();
//        }
        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        for (int k=0;k<=3;k++){
            System.out.println(data[k]);
        }
    }
}

-----------------------------------------------------------------------------------------------------------------------------------------------

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Main {
    private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
    final static  CyclicBarrier barrier = new CyclicBarrier(4);
    public static void main(String[] args) {
         //int data[] = new int[4];
        String[] s= new String[4];
        for(int i=0;i<=3; i++){
            final int j=i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    s[j]="aaaaaaaaa"+j;
//                    System.out.println(j+"----" + Thread.currentThread().getName());
//                   System.out.println(data[j]);
                    try{
                        barrier.await();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            });
        }

//        try {
//            Thread.sleep(30000); //1000 毫秒,也就是1秒.
//        } catch(InterruptedException ex) {
//            Thread.currentThread().interrupt();
//        }
        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        for (int k=0;k<=3;k++){
            System.out.println(s[k]);
        }
    }
}

import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService exector = Executors.newFixedThreadPool(12);
        int threadNumber = 10;
        String[] s = new String[10];
        final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
        for (int i = 0; i < threadNumber; i++) {
            final int threadID = i;
            exector.execute(
                    () -> {

                        try {
                            Thread.sleep(3000);
                            s[threadID] = "aaaaaaaaa" + threadID;
                            System.out.println(String.format("threadID:[%s] finished!!", threadID));
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            countDownLatch.countDown();  //这个不管是否异常都需要数量减,否则会被堵塞无法结束
                        }


                    }
            );
        }
        countDownLatch.await();//保证之前的所有的线程都执行完成,才会走下面的
        System.out.println(countDownLatch.getCount());
        System.out.println("main thread finished!!");

        for (int k = 0; k < s.length; k++) {
            System.out.println(s[k]);
        }


    }   


}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值