多线程控制场景

1、控制并发数Semaphore

  • 主线程中创建信号量对象,Semaphore sem=new Semaphore(i); 
  • 作为参数传入子线程中
  • 子线程中sem.acquire();获取信号量,获取成功后才能继续执行,限制了并行数目
  • 子线程中sem.release();释放信号量,使其他在等待acquire的线程可以获取到信号量

2、若干子线程都结束后再继续向下执行主线程CountDownLatch

  • 主线程中创建计数对象,CountDownLatch c=new CountDownLatch(i);
  • 作为参数传入子线程中
  • 子线程中c.countDown(); 使计数器+1
  • 主线程中c.await();等待计数器达到指定数目,然后执行主线程下面的部分

3、CyclicBarrier

  • 主线程中创建计数对象,CyclicBarrier cyc = new CyclicBarrier(i);
  • 作为参数传入子线程中
  • 子线程中cyc.await();开始等待,同时使计数器+1
  • 计数器达到指定数目后,所有在cyc.await();处等待的子线程继续向下执行,同时cyc计数清0,开始新的计数
package test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class ThreadTest {
    //通过信号量计数器控制同时并发的线程数
    static class test{
        static void testSemaphore(int i){
            //主线程中限制并发线程数
            Semaphore sem=new Semaphore(i);
            Thread t1=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //子线程获取信号量,默认一个,可以获取多个
                        sem.acquire();
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(1);
                        }
                        //释放信号量,默认一个,可以多个
                        sem.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
            Thread t2=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        sem.acquire();
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(2);
                        }
                        sem.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t2.start();
        }
        static void testCountDownLatch(int i) throws InterruptedException {
            CountDownLatch c=new CountDownLatch(i);
            Thread t1=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(1);
                        }
                        //子线程中使计数器+1
                        c.countDown();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
            Thread t2=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(2);
                        }
                        c.countDown();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t2.start();
            //主线程中等待计数器达到指定数目
            c.await();
            System.out.println("over");
        }
        static void testCyclicBarrier(int i){
            CyclicBarrier cyc = new CyclicBarrier(i);
            Thread t1=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(1);
                        }
                        //等待其他线程到达此处
                        cyc.await();
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(3);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
            Thread t2=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(2);
                        }
                        cyc.await();
                        for(int i=0;i<=2;i++){
                            Thread.sleep(Math.round(Math.random()*10));
                            System.out.println(4);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            t2.start();
        }
    }
    public static void main(String[] args) throws InterruptedException {
//        test.testSemaphore(1);
//        test.testCountDownLatch(2);
        test.testCyclicBarrier(2);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值