[关闭]
@boothsun 2017-07-10T04:57:18.000000Z 字数 4364 阅读 1248

线程同步工具 Semaphore类使用

Java多线程


控制并发访问资源

当一个线程想要访问某个共享资源,首先,它必须获得semaphore。如果semaphore的内部计数器的值大于0,那么semaphore减少计数器的值并允许访问共享的资源。计数器的值大于0表示,有可以自由使用的资源,所以线程可访问并使用它们。

另一种情况,如果semaphore的计数器的值等于0,那么semaphore让线程进入休眠状态一直到计数器大于0。计数器的值等于0表示全部的共享资源都正被线程们使用,所以线程想要访问就必须等到某个资源成为自由的。

当线程使用完共享资源时,他就必须释放出semaphore为了让其他线程可以访问共享资源。这个操作会增加semaphore的内部计数器的值。

使用Semaphore模拟互斥锁

  1. import java.util.concurrent.Semaphore;
  2. //1. 使用semaphore保护的互斥打印队列
  3. class PrintQueue {
  4. private final Semaphore semaphore;
  5. public PrintQueue() {
  6. // 只设定一个许可,这样同一个时刻 只能一个线程执行 printJob方法,从而实现互斥锁
  7. semaphore = new Semaphore(1);
  8. }
  9. //2.实现Implement the printJob()方法,此方法可以模拟打印文档,并接收document对象作为参数。
  10. public void printJob(Object document) {
  11. try {
  12. semaphore.acquire();
  13. //3.然后,实现能随机等待一段时间的模拟打印文档的行。
  14. long duration = (long) (Math.random() * 10);
  15. System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n", Thread.currentThread().getName(), duration);
  16. Thread.sleep(duration);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. } finally {
  20. //7.最后,释放semaphore通过调用semaphore的release()方法。
  21. semaphore.release();
  22. System.out.printf("%s: PrintQueue: Printing a Job release the permits \n", Thread.currentThread().getName());
  23. }
  24. }
  25. }
  26. // 模拟打印进程
  27. class PrintThread extends Thread {
  28. PrintQueue printQueue ;
  29. public PrintThread (PrintQueue printQueue) {
  30. this.printQueue = printQueue ;
  31. }
  32. @Override
  33. public void run() {
  34. printQueue.printJob(new Object());
  35. }
  36. }
  37. public class PrintQueueTest {
  38. public static void main(String[] args) {
  39. PrintQueue printQueue = new PrintQueue() ;
  40. PrintThread a = new PrintThread(printQueue) ;
  41. a.setName("A");
  42. PrintThread b = new PrintThread(printQueue);
  43. b.setName("B");
  44. PrintThread c = new PrintThread(printQueue) ;
  45. c.setName("C");
  46. a.start();
  47. b.start();
  48. c.start();
  49. }
  50. }

控制并发访问多个资源

在上面的例子中,我们使用了semaphore来保护访问一个共享资源的,或者说一个代码片段每次只能被一个线程执行。但是semaphore也可以用来保护多个资源的副本,也就是说当你有一个代码片段每次可以被指定数量的多个线程执行时,可以考虑使用Semaphore。

下面的例子会有一个print queue 但可以在3个不同的打印机上打印文件。

  1. import java.util.concurrent.Semaphore;
  2. import java.util.concurrent.TimeUnit;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;
  5. class PrintQueue2 {
  6. // 模拟打印机 数组
  7. private boolean freePrinters[];
  8. private Lock lockPrinters;
  9. private final Semaphore semaphore;
  10. public PrintQueue2() {
  11. semaphore = new Semaphore(2);
  12. //模拟 初始化三个可用的打印机
  13. freePrinters = new boolean[]{true,true,true};
  14. lockPrinters = new ReentrantLock();
  15. }
  16. public void printJob(Object document) {
  17. int assignedPrinter = -1 ;
  18. try {
  19. semaphore.acquire();
  20. // 获取可用的打印机 序号
  21. assignedPrinter = getPrinter();
  22. //7.然后, 随机等待一段时间来实现模拟打印文档的行。
  23. long duration = (long) (Math.random() * 10);
  24. System.out.printf("%s: PrintQueue: Printing a Job in Printer%d during %d seconds\n", Thread.currentThread().getName(), assignedPrinter, duration);
  25. TimeUnit.SECONDS.sleep(duration);
  26. } catch (InterruptedException e) {
  27. e.printStackTrace();
  28. } finally {
  29. if(assignedPrinter != -1) { // 模拟释放打印机
  30. freePrinters[assignedPrinter] = true;
  31. }
  32. semaphore.release();
  33. System.out.printf("%s: PrintQueue: 打印结束释放打印机\n", Thread.currentThread().getName());
  34. }
  35. }
  36. // 遍历找到当前可用的 打印机的索引下标
  37. private int getPrinter() {
  38. int ret = -1;
  39. try {
  40. lockPrinters.lock();
  41. for (int i = 0; i < freePrinters.length; i++) {
  42. if (freePrinters[i]) {
  43. ret = i;
  44. freePrinters[i] = false;
  45. break;
  46. }
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. } finally {
  51. lockPrinters.unlock();
  52. }
  53. return ret;
  54. }
  55. }
  56. // 模拟打印进程
  57. class PrintThread extends Thread {
  58. PrintQueue2 printQueue2 ;
  59. public PrintThread (PrintQueue2 printQueue) {
  60. this.printQueue2 = printQueue ;
  61. }
  62. @Override
  63. public void run() {
  64. printQueue2.printJob(new Object());
  65. }
  66. }
  67. class PrintQueueTest {
  68. public static void main(String[] args) {
  69. PrintQueue2 printQueue = new PrintQueue2() ;
  70. PrintThread a = new PrintThread(printQueue) ;
  71. a.setName("A");
  72. PrintThread b = new PrintThread(printQueue);
  73. b.setName("B");
  74. PrintThread c = new PrintThread(printQueue) ;
  75. c.setName("C");
  76. a.start();
  77. b.start();
  78. c.start();
  79. }
  80. }

使用Semaphore控制并发线程数

Semaphore可以用来做流量控制,特别公用资源有限的应用场景,比如数据库连接。假设有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但是如果读到内存后,还需要进行存储到数据库中,而数据库的连接数只有10几个,这时我们必须控制只有十个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,我们就可以使用Semaphore来做流控。

  1. public class SemaphoreTest2 {
  2. private static final int THREAD_COUNT = 30;
  3. private static ExecutorService threadPool = Executors
  4. .newFixedThreadPool(THREAD_COUNT);
  5. private static Semaphore semaphore = new Semaphore(10);
  6. public static void main(String[] args) {
  7. for (int i = 0; i < THREAD_COUNT; i++) {
  8. threadPool.execute(new Runnable() {
  9. @Override
  10. public void run() {
  11. try {
  12. semaphore.acquire();
  13. System.out.println("save data");
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. } finally {
  17. semaphore.release();
  18. }
  19. }
  20. });
  21. }
  22. threadPool.shutdown();
  23. }
  24. }

在代码中,虽然有30个线程正在执行,但是只允许10个并发的执行。Semaphore的构造方法Semaphore(int permits)接收一个整型的数字,表示可用的许可证数量。Semaphore(10)表示允许10个线程获取许可证,也就是最大并发数是10。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注