ThreadsToDebug.java
1 package ojvmthreads;
2 import java.io.BufferedReader;
3 import java.io.InputStreamReader;
4
5 public class
6 {
7   private final static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
8
9   // Three boolean variables, the associated thread stops
10   // when its value is false.
11   private boolean goThreadOne = true;
12   private boolean goThreadTwo = true;
13   private boolean goThreadThree = true;
14
15   public ThreadsToDebug()
16   {
17     // Three threads.
18     // They're all calling the same method.
19     Thread threadOne = new Thread("ThreadOne")
20     {
21       private long howMuch = 0;
22
23       public void run()
24       {
25         while (goThreadOne)
26         {
27           howMuch += doYourJob(this.getName());
28           System.out.println(this.getName() + " is already at " + howMuch);
29         }
30       }
31     };
32     Thread threadTwo = new Thread("ThreadTwo")
33     {
34       private long howMuch = 0;
35
36       public void run()
37       {
38         while (goThreadTwo)
39         {
40           howMuch += doYourJob(this.getName());
41           System.out.println(this.getName() + " is already at " + howMuch);
42         }
43       }
44     };
45     Thread threadThree = new Thread("ThreadThree")
46     {
47       private long howMuch = 0;
48
49       public void run()
50       {
51         while (goThreadThree)
52         {
53           howMuch += doYourJob(this.getName());
54           System.out.println(this.getName() + " is already at " + howMuch);
55         }
56       }
57     };
58
59     // Start all the threads
60     threadOne.start();
61     threadTwo.start();
62     threadThree.start();
63
64     // Prompt the user to enter 1, 2, or 3.
65     // When "1" is entered, first thread stop,
66     // "2" stops the second, and "3" the third.
67     // The application exits whan all threads are stopped.
68     while (goThreadOne || goThreadTwo || goThreadThree)
69     {
70       String str = userInput("1, 2, 3:");
71       if (str.equals("1"))
72         goThreadOne = false;
73       else if (str.equals("2"))
74         goThreadTwo = false;
75       else if (str.equals("3"))
76         goThreadThree = false;
77     }
78     System.out.println("Done");
79   }
80
81   // What all the threads do.
82   // It's all about taking a random nap...
83   private long doYourJob(String str)
84   {
85     long nap = Math.round(Math.random() * 5);
86     try
87     {
88       Thread.sleep(nap * 1000);
89     }
90     catch (Exception ignore)
91     {}
92     return nap;
93   }
94
95   public static void main(String[] args)
96   {
97     ThreadsToDebug threadsToDebug = new ThreadsToDebug();
98   }
99
100   // User input from stdin.
101   private static String userInput(String prompt)
102   {
103     String retString = "";
104     System.err.print(prompt);
105     try { retString = stdin.readLine(); }
106     catch (Exception e)
107     {
108       System.out.println(e);
109       try { String s = userInput(""); } catch (Exception ex) {}
110     }
111     return retString;
112   }
113 }
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy