KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > Semaphore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.utils;
12
13 public class Semaphore {
14     protected long notifications;
15     protected Runnable JavaDoc runnable;
16
17     public Semaphore(Runnable JavaDoc runnable) {
18         this.runnable = runnable;
19         notifications = 0;
20     }
21
22     public synchronized void acquire() throws InterruptedException JavaDoc {
23         if (Thread.interrupted())
24             throw new InterruptedException JavaDoc();
25         while (notifications <= 0)
26             wait();
27         notifications--;
28     }
29
30     public boolean equals(Object JavaDoc obj) {
31         return (runnable == ((Semaphore) obj).runnable);
32     }
33
34     public Runnable JavaDoc getRunnable() {
35         return runnable;
36     }
37
38     public int hashCode() {
39         return runnable.hashCode();
40     }
41
42     public synchronized void release() {
43         notifications++;
44         notifyAll();
45     }
46
47     // for debug only
48
public String JavaDoc toString() {
49         return String.valueOf(runnable);
50     }
51 }
Popular Tags