KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > LockTest


1 package org.columba.core.util;
2
3 import junit.framework.TestCase;
4
5 import org.columba.core.base.Lock;
6
7 public class LockTest extends TestCase {
8
9     public boolean testBool;
10     public int testInt;
11     
12     
13     public void test1() throws InterruptedException JavaDoc {
14         final Lock testLock = new Lock();
15         testBool = false;
16         
17         assertTrue(testLock.tryToGetLock(this));
18         
19         Thread JavaDoc t = new Thread JavaDoc() {
20             public void run() {
21                 testLock.getLock(this);
22                 assertTrue(testBool);
23             }
24         };
25         
26         t.start();
27         
28         Thread.sleep(100);
29         
30         testBool = true;
31         testLock.release(this);
32         
33         t.join(100);
34         assertFalse(t.isAlive());
35         
36         assertFalse(testLock.tryToGetLock(this));
37         testLock.release(t);
38         assertTrue(testLock.tryToGetLock(this));
39     }
40
41     public void test2() throws InterruptedException JavaDoc {
42         final Lock testLock = new Lock();
43         testInt = 0;
44         
45         assertTrue(testLock.tryToGetLock(this));
46         
47         Thread JavaDoc t1 = new Thread JavaDoc() {
48             public void run() {
49                 testLock.getLock(this);
50                 testInt++;
51                 testLock.release(this);
52             }
53         };
54         
55         Thread JavaDoc t2 = new Thread JavaDoc() {
56             public void run() {
57                 testLock.getLock(this);
58                 testInt++;
59                 testLock.release(this);
60             }
61         };
62
63         t1.start();
64         t2.start();
65         
66         Thread.sleep(100);
67         
68         testLock.release(this);
69         
70         t1.join(100);
71         t2.join(100);
72         
73         assertEquals(2,testInt);
74     }
75     
76 }
77
Popular Tags