semaphore.java
来自「Reader Writer Problem to solve synchroni」· Java 代码 · 共 57 行
JAVA
57 行
/* $Id: Semaphore.java,v 1.2 1996/06/28 04:10:16 kmillik1 Exp $ *//* $Log: Semaphore.java,v $# Revision 1.2 1996/06/28 04:10:16 kmillik1# Fixed $Log$ comment.## Revision 1.1 1996/06/28 04:03:02 kmillik1# Initial revision#*/public class Semaphore extends Object { private int s; public Semaphore(int n) { s = n;// System.out.println("Semaphore"); } public Semaphore() { this(0); } synchronized public void up() { int temp; temp=s; s=1;//System.out.println("up value: "+s); if(temp==0){notify(); // System.out.println("notified up value: "+s); }; } synchronized public void down() { try { if (s > 0){ s=0;// System.out.println("down value: "+s);} else{// System.out.print("down value: "+s);// System.out.println(" Waiting.."); wait(); s=0;}; } catch (InterruptedException e) { // add exception handler code here } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?