好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java并发编程信号量Semapher

Semapher 信号量也是Java中的一个同步器,与CountDownLatch和CycleBarrier不同的是,它内部的计数器是递增的,并且在一开始初始化Semaphoer时可以指定一个初始值,但是并不需要知道需要同步的线程个数,而是在需要同步的地方调用acquire方法时指定需要同步的线程个数。

我们通过下面一个例子来看一下Semapher效果:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

 

public class SemaphoreTest {

 

     private static Semaphore semaphore = new Semaphore( 0 );

     public static void main(String[] args) throws InterruptedException{

         ExecutorService executorService = Executors.newFixedThreadPool( 2 );

         executorService.submit( new Runnable() {

             @Override

             public void run() {

                 try {

                     System.out.println(Thread.currentThread() + "over" );

                     semaphore.release();

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

         });

 

         executorService.submit( new Runnable() {

             @Override

             public void run() {

                 try {

                     System.out.println(Thread.currentThread() + "over" );

                     semaphore.release();

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

         });

 

         semaphore.acquire( 2 );

         System.out.println( "all child thread over!" );

         executorService.shutdown();

 

     }

}

如上代码首先创建了一个信号量实例,构造函数的入参为0,说明当前信号量计数器的值为0。然后,main函数向线程池添加两个线程任务,在每个线程内部调用信号量的acquire方法,传参为2说明调用acquire方法的线程会一直阻塞,知道信号量的技术变为2才会返回。如果构造Semaphore时,传递的参数为N,并在M个线程中调用了该信号量的release方法,那么在调用acquire使M个线程同步时传递的参数应该是M+N。

下面举例子来模拟CycliBarrier复用的功能,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

 

public class Semaphoer {

 

     private static Semaphore semaphore = new Semaphore( 0 );

     public static void main(String[] args) throws InterruptedException {

         ExecutorService executorService = Executors.newFixedThreadPool( 2 );

         executorService.submit( new Runnable() {

             @Override

             public void run() {

                 try {

                     System.out.println(Thread.currentThread() + "A task over" );

                     semaphore.release();

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

         });

 

         executorService.submit( new Runnable() {

             @Override

             public void run() {

                 try {

                     System.out.println(Thread.currentThread() + "A task over" );

                     semaphore.release();

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

         });

 

         semaphore.acquire( 2 );

         System.out.println( "task A is over" );

         executorService.submit( new Runnable() {

             @Override

             public void run() {

                 try {

                     System.out.println(Thread.currentThread() + "B task over" );

                     semaphore.release();

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

         });

 

         executorService.submit( new Runnable() {

             @Override

             public void run() {

                 try {

                     System.out.println(Thread.currentThread() + "B task over" );

                     semaphore.release();

                 } catch (Exception e) {

                     e.printStackTrace();

                 }

             }

         });

 

         semaphore.acquire( 2 );

         System.out.println( "task B is over" );

         executorService.shutdown();

 

     }

}

如上代码首先将线程A和线程B加入到线程池。主线程执行代码(1)后被阻塞。线程A和线程B调用 release 方法后信号量的值变为了2,这时候主线程的aquire方法会在获取到2个信号量后返回(返回后当前信号量值为0)。然后主线程添加线程C和线程D到线程池,之后主线程执行代码(2)后被阻塞(因为主线程要获取2个信号量,而当前信号量个数为0)。当线程C和线程D执行完release 方法后,主线程才返回。从本例子可以看出, Semaphore 在某种程度上实现了CyclicBarrier 的复用功能。

到此这篇关于Java并发编程信号量Semapher的文章就介绍到这了,更多相关Java Semapher内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7084140876535955470

查看更多关于Java并发编程信号量Semapher的详细内容...

  阅读:11次