/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. ========== TwoThreadsApp.java  =============== 

2.  

3. import java.awt.* ; 

4. import java.applet.* ; 

5. import java.awt.event.*; 

6.  

7. public class TwoThreadsApp 

8.     extends Applet 

9.     implements MouseListener 

10.

11.   OurThreadOne task1 = null

12.   OurThreadTwo task2 = null

13.  

14.   int data ; 

15.  

16.   boolean pause = true

17.   boolean keepgoing = true

18.   boolean task1sTurn = true

19.  

20.   Label lbl ; 

21.   TextField tf ; 

22.    

23.   public void 

24.   mouseClicked(MouseEvent ev) 

25.   { 

26.     if ( task1 == null

27.     { 

28.       data = 0

29.       keepgoing = true

30.       pause = false

31.       task1sTurn = true

32.  

33.       task1 = new OurThreadOne ( this ) ; 

34.       task1.start () ; 

35.  

36.       task2 = new OurThreadTwo ( this ) ; 

37.       task2.start () ; 

38.  

39.       lbl.setText ( "====== Threads started =======" ) ; 

40.       return

41.     } 

42.     if ( ev.isMetaDown() ) 

43.     { 

44.       stop(); 

45.       return

46.     } 

47.     pause = !pause ; 

48.  

49.     if ( pause ) 

50.       lbl.setText ( "====== Threads both paused =======" ) ; 

51.     else 

52.       lbl.setText ( "====== Threads continued =======" ) ; 

53.   } 

54.   synchronized  int 

55.   getData () 

56.   { 

57.     while ( Thread.currentThread () == task1 

58.         && !task1sTurn ) 

59.     { 

60.       try

61.         wait ( ) ; 

62.       } 

63.       catch (Exception e) {} 

64.     } 

65.     while ( Thread.currentThread () == task2 

66.          && task1sTurn ) 

67.     { 

68.       try

69.         wait ( ) ; 

70.       } 

71.       catch (Exception e) {} 

72.     } 

73.     return data ; 

74.   } 

75.   synchronized void 

76.   setData ( int d ) 

77.   { 

78.     while ( Thread.currentThread () == task1 

79.         && !task1sTurn ) 

80.     { 

81.       try

82.         wait ( ) ; 

83.       } 

84.       catch (Exception e) {} 

85.     } 

86.     while ( Thread.currentThread () == task2 

87.          && task1sTurn ) 

88.     { 

89.       try

90.         wait ( ) ; 

91.       } 

92.       catch (Exception e) {} 

93.     } 

94.     data = d ; 

95.     tf.setText ( data + "" ) ; 

96.  

97.     if ( Thread.currentThread () == task1 ) 

98.     { 

99.       task1sTurn = false

100.       tf.setBackground ( Color.yellow ) ; 

101.     } 

102.     else 

103.       if ( Thread.currentThread () == task2 ) 

104.       { 

105.         task1sTurn = true

106.         tf.setBackground ( Color.green ) ; 

107.       } 

108.  

109.     notifyAll (); 

110.   } 

111.   public void 

112.   stop() 

113.   { 

114.     pause = true

115.     keepgoing = false

116.  

117.     if ( task1 != null && task2 != null

118.       while ( task1.isAlive() || task2.isAlive() ) 

119.           try

120.               Thread.sleep (5) ; 

121.           } 

122.           catch (Exception e) {} 

123.     task1 = null

124.     task2 = null

125.     lbl.setText ( "====== Threads both stopped =======" ) ; 

126.   } 

127.  

128.   public void 

129.   init() 

130.   { 

131.     data = 0

132.  

133.     add ( lbl = new Label ( "++++++++++++++++++++++++++++" ) ) ; 

134.     add ( tf =   new TextField ( 10 ) ) ; 

135.     addMouseListener ( this ) ; 

136.     setBackground ( Color.orange ) ; 

137.     validate () ; 

138.   } 

139.   public void mousePressed(MouseEvent e) {} 

140.   public void mouseReleased(MouseEvent e) {} 

141.   public void mouseEntered(MouseEvent e) {} 

142.   public void mouseExited(MouseEvent e) {} 

143.

144.  

145. ===========  OurThreadOne.java    ================ 

146.  

147. public class OurThreadOne 

148.     extends Thread 

149.

150.   TwoThreadsApp creator ; 

151.  

152.   public void 

153.   run () 

154.   { 

155.     while ( creator.keepgoing ) 

156.     { 

157.       while ( !creator.pause ) 

158.       { 

159.         int dt = creator.getData () ; 

160.         creator.setData ( dt + 100000 ) ; 

161.       } 

162.       try

163.         sleep ( 10 ) ; 

164.       } 

165.       catch (Exception e) {} 

166.     } ///end while 

167.   } 

168.   OurThreadOne ( TwoThreadsApp app ) 

169.   { 

170.     super ("Uno") ; 

171.     creator = app ; 

172.   } 

173.

174.  

175. ==========  OurThreadTwo.java   ================ 

176.  

177. public class OurThreadTwo 

178.     extends Thread 

179.

180.   TwoThreadsApp creator ; 

181.  

182.   public void 

183.   run () 

184.   { 

185.     while ( creator.keepgoing ) 

186.     { 

187.       while ( !creator.pause ) 

188.       { 

189.         int dt = creator.getData () ; 

190.         creator.setData ( dt + 1 ) ; 

191.       } 

192.       try

193.         sleep ( 10 ) ; 

194.       } 

195.       catch (Exception e) {} 

196.     } ///end while 

197.   } 

198.   OurThreadTwo ( TwoThreadsApp app ) 

199.   { 

200.     super ("Due") ; 

201.     creator = app ; 

202.   } 

203.  

204.

205.