KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > mc > oldclassic


1 package gov.nasa.jpf.mc;
2 //
3
// Copyright (C) 2005 United States Government as represented by the
4
// Administrator of the National Aeronautics and Space Administration
5
// (NASA). All Rights Reserved.
6
//
7
// This software is distributed under the NASA Open Source Agreement
8
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
9
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
10
// directory tree for the complete NOSA document.
11
//
12
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
13
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
14
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
15
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
16
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
17
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
18
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
19
//
20

21 /**
22  * This example shows a deadlock which occurs as a result of a missed signal.
23  * It also contains an assertion check that shows the race violation that is the
24  * cause for the missed signal (and ultimate deadlock)
25  * @author wvisser
26  */

27 public class oldclassic {
28   public static void main (String JavaDoc[] args) {
29     Event new_event1 = new Event();
30     Event new_event2 = new Event();
31
32     FirstTask task1 = new FirstTask(new_event1, new_event2);
33     SecondTask task2 = new SecondTask(new_event1, new_event2);
34
35     task1.start();
36     task2.start();
37   }
38 }
39
40 /**
41  * DOCUMENT ME!
42  */

43 class Event {
44   int count = 0;
45
46   public synchronized void signal_event () {
47     count = (count + 1) % 3;
48     notifyAll();
49   }
50
51   public synchronized void wait_for_event () {
52     try {
53       wait();
54     } catch (InterruptedException JavaDoc e) {
55     }
56
57     ;
58   }
59 }
60
61 /**
62  * DOCUMENT ME!
63  */

64 class FirstTask extends java.lang.Thread JavaDoc {
65   Event event1;
66   Event event2;
67   int count = 0;
68
69   public FirstTask (Event e1, Event e2) {
70     this.event1 = e1;
71     this.event2 = e2;
72   }
73
74   public void run () {
75     count = event1.count;
76
77     while (true) {
78       if (count == event1.count) {
79         assert (count == event1.count);
80         event1.wait_for_event();
81       }
82       count = event1.count;
83       event2.signal_event();
84     }
85   }
86 }
87
88 /**
89  * DOCUMENT ME!
90  */

91 class SecondTask extends java.lang.Thread JavaDoc {
92   Event event1;
93   Event event2;
94   int count = 0;
95
96   public SecondTask (Event e1, Event e2) {
97     this.event1 = e1;
98     this.event2 = e2;
99   }
100
101   public void run () {
102     count = event2.count;
103
104     while (true) {
105       event1.signal_event();
106       if (count == event2.count) {
107         assert (count == event2.count);
108         event2.wait_for_event();
109       }
110       count = event2.count;
111     }
112   }
113 }
Popular Tags