KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > modules > PyLock


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.modules;
3
4 import org.python.core.*;
5
6 public class PyLock extends PyObject {
7     private boolean locked=false;
8     //private Object lock = new Object();
9

10     public boolean acquire() {
11         return acquire(true);
12     }
13
14     public synchronized boolean acquire(boolean waitflag) {
15         if (waitflag) {
16             while (locked) {
17                 try {
18                     wait();
19                 } catch (InterruptedException JavaDoc e) {
20                     System.err.println("Interrupted thread");
21                 }
22             }
23             locked = true;
24             return true;
25         } else {
26             if (locked) {
27                 return false;
28             } else {
29                 locked = true;
30                 return true;
31             }
32         }
33     }
34
35     public synchronized void release() {
36         if (locked) {
37             locked = false; notifyAll();
38         } else {
39             throw Py.ValueError("lock not acquired");
40         }
41     }
42
43     public boolean locked() {
44         return locked;
45     }
46 }
47
Popular Tags