summaryrefslogtreecommitdiff
path: root/java/JACE/tests/Concurrency/TokenTest.java
blob: 162170f19969e1386bc96f8b29d8c6126fa4f85d (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
94
95
/*************************************************
 *
 * = PACKAGE
 *    tests.Concurrency
 *
 * = FILENAME
 *    TokenTest.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.tests.Concurrency;

import java.io.*;
import JACE.OS.*;
import JACE.Concurrency.ThreadManager;
import JACE.Concurrency.*;
import JACE.ASX.TimeValue;

public class TokenTest implements Runnable
{
  static class MyToken extends Token
  {
    public void sleepHook ()
    {
      ACE.DEBUG (Thread.currentThread ().getName () + 
		 " blocking, sleepHook called");
    }  
  }

  public void run ()
  {
    String name = Thread.currentThread().getName();
    try
      {
	if (this.token_.acquire () != AbstractLock.FAILURE)
	  ACE.DEBUG (name + " got token");
	else
	  ACE.DEBUG (name + " couldn't get token");

	ACE.DEBUG (name + " calling acquire again (test nesting)");
	if (this.token_.acquire() != AbstractLock.FAILURE)
	  ACE.DEBUG (name + " got token again");
	else
	  ACE.DEBUG (name + " couldn't get token");

	Thread.sleep (1000);

	ACE.DEBUG (name + " gives it up for max 2 sec to first waiter");
	this.token_.renew (1, TimeValue.relativeTimeOfDay(2, 0));

	ACE.DEBUG (name + " releases the token once");
	this.token_.release ();
	ACE.DEBUG (name + " calls release again (nesting level was 2)");
	this.token_.release ();
      }
    catch (InterruptedException e)
      {
	this.token_.release ();
      }
    catch (JACE.ASX.TimeoutException e) 
      {
	ACE.DEBUG (name + " timed out");
      }
  }

  /**
   * Command line: optional number of threads to create (defaults to 2)
   */
  public static void main (String args [])
  {
    ThreadManager tm = new ThreadManager ();
    int n = 2;

    ACE.enableDebugging ();

    try
      {
	if (args.length == 1)
	  {
	    n = Integer.parseInt (args[0]);
	  }
      }
    catch (NumberFormatException e)
      {
	ACE.ERROR ("Illegal argument.");
      }

    tm.spawnN (n,
	       new TokenTest (),
	       false);
  }

  private static MyToken token_ = new MyToken ();
}