summaryrefslogtreecommitdiff
path: root/java/tests/Concurrency/RWMutexTest.java
blob: ab28c9a83fde1cea90d218803fc03ffd826583c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*************************************************
 *
 * = PACKAGE
 *    tests.Concurrency
 *
 * = FILENAME
 *    RWMutexTest.java
 *
 *@author Ross Dargahi (rossd@krinfo.com)
 *
 *************************************************/
package tests.Concurrency;

import JACE.OS.*;
import JACE.Concurrency.*;

class TestThread extends Thread
{
  TestThread(String  name,
	     boolean writer,
	     RWMutex  lock)
  {
    super (name);
    mWriter = writer;
    mLock = lock;
  }

  public void run()
  {
    for (int i = 0; i < 10; i++)
      {
	try
	  {
	    if (!mWriter)
	      {
		mLock.acquireRead();
		ACE.DEBUG (getName() + ": Acquired Read Lock");
		
		int sleepTime = i * 100;
		sleep (sleepTime);

		mLock.release ();
		ACE.DEBUG (getName () + ": Released Read Lock");
	      }
	    else
	      {
		mLock.acquireWrite ();
		ACE.DEBUG (getName () + ": Acquired Write Lock");
					
		int sleepTime = i * 100;
		sleep (sleepTime);

		mLock.release ();
		ACE.DEBUG (getName () + ": Released Write Lock");
	      }
	  }
	catch (InterruptedException ex)
	  {
	    ACE.ERROR ("InterruptedException");
	  }
      }
  }

  RWMutex  mLock;
  boolean mWriter;
}

public class RWMutexTest
{
  public static void main(String [] args)
  {
    RWMutex lock = new RWMutex();
    
    TestThread t1 = new TestThread ("1", false, lock);
    TestThread t2 = new TestThread ("2", false, lock);
    TestThread t3 = new TestThread ("3", false, lock);
    TestThread t4 = new TestThread ("4", true, lock);
    TestThread t5 = new TestThread ("5", false, lock);
    TestThread t6 = new TestThread ("6", false, lock);
    TestThread t7 = new TestThread ("7", false, lock);
    TestThread t8 = new TestThread ("8", true, lock);

    t1.start ();
    t2.start ();
    t3.start ();
    t4.start ();
    t5.start ();
    t6.start ();
    t7.start ();
    t8.start ();
  }
}