java semaphore
2015-10-30 11:50
399 查看
java基于semaphore方式实现对资源的并发访问,也是多线程同步的一种方式,它一般是用于实现对多个资源副本的并发访问控制,而synchronized,lock等同步方式不同则用于针对一个资源的并发访问控制,也就是说资源只允许一个线程同时访问,而semaphore则可以控制某几个线程同时访问资源。
当然semaphore它也有一个变种,即二进制信号量,它的作用实际上和synchronized和lock就差不多了,二进制信号量也是限制资源只允许一个线程同时访问。
具体的内容可以参考下面的例子。
当然semaphore它也有一个变种,即二进制信号量,它的作用实际上和synchronized和lock就差不多了,二进制信号量也是限制资源只允许一个线程同时访问。
具体的内容可以参考下面的例子。
package com.basic.thread; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class BasicSemaphore { public static void main(String args[]) { PrintQueueSem pq = new PrintQueueSem(); Thread thread[] = new Thread[10]; for (int i=0; i<10; i++) { thread[i] = new Thread(new Job(pq), "Thread" + i); } for (int i=0; i<10; i++) { thread[i].start(); } } } class PrintQueue { private final Semaphore semaphore; public PrintQueue() { semaphore = new Semaphore(1); } public void printJob(Object doc) { try { semaphore.acquire(); long duration = (long) (Math.random()*10); System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),duration); Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); } } } class PrintQueueSem { private boolean freePrinters[]; private Lock lockPrinter; private final Semaphore semaphore; public PrintQueueSem() { semaphore = new Semaphore(3); freePrinters = new boolean[3]; for (int i = 0; i < 3; i++) { freePrinters[i] = true; } lockPrinter = new ReentrantLock(); } public void printJob(Object obj) { try { semaphore.acquire(); int assignedPrinter = getPrinter(); long duration = (long) (Math.random()*10); System.out.printf("%s: PrintQueue: Printing a Job in Printer%d during %d seconds\n",Thread.currentThread().getName(), assignedPrinter,duration); Thread.sleep(duration); freePrinters[assignedPrinter] = true; } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); } } private int getPrinter() { int ret = -1; try { lockPrinter.lock(); for (int i=0; i<freePrinters.length; i++) { if (freePrinters[i]) { ret = i; freePrinters[i]=false; break; } } } catch (Exception e) { e.printStackTrace(); } finally { lockPrinter.unlock(); } return ret; } } class Job implements Runnable { /** private PrintQueue printQueue; public Job(PrintQueueSem pq) { this.printQueue = pq; } */ private PrintQueueSem printQueue; public Job(PrintQueueSem pq) { this.printQueue = pq; } @Override public void run() { System.out.printf("%s: Going to print a job\n",Thread. currentThread().getName()); printQueue.printJob(new Object()); System.out.printf("%s: The document has been printed\n",Thread.currentThread().getName()); } }
相关文章推荐
- java native方法使用及JNI实例
- token机制(Struts中知识点)
- .Net 与 Java 的服务接口相互调用
- Java编程思想小笔记2
- java随机生成数字字母验证码
- 从头认识java-6.4 如何在聚合与继承之间选择
- Java编程思想小笔记
- 从头认识java-6.4 如何在聚合与继承之间选择
- Java NIO系列教程(七) FileChannel
- Ubuntu中Eclipse中文乱码
- Resteasy集成Spring
- Java分类统计次数,取max
- java向mysql数据库插入数据显示乱码的问题
- Spring MVC篇一、搭建Spring MVC框架
- JAVA中的访问权限
- eclipse中的Console控制台视图脱离主窗口解决办法
- 使用spring集成hibernate
- 用java实现 数三退出游戏(2)约瑟夫环
- hdu 1001 初学java代码
- Spring与Mybatis的整合方法有哪些