forked from douglascraigschmidt/LiveLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestThread.java
More file actions
44 lines (39 loc) · 1.6 KB
/
TestThread.java
File metadata and controls
44 lines (39 loc) · 1.6 KB
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
/**
* @class TestThread
*
* @brief This program demonstrates the difference between a Java user
* thread and a daemon thread. If it's launched with no
* command-line parameters the main thread creates a user
* thread, which can outlive the main thread (i.e., it
* continues to run even after the main thread exits). If it's
* launched with a command-line parameter then it creates a
* daemon thread, which exits when the main thread exits.
*/
public class TestThread {
/**
* Entry point method into the program's main thread, which
* creates/starts the desired type of thread (i.e., either "user"
* or "daemon") and sleeps for 1 second while that thread runs in
* the background. If a "daemon" thread is created it will only
* run as long as the main thread runs. Conversely, if a "user"
* thread is created it will continue to run even after the main
* thread exits.
*/
public static void main(String[] args) {
System.out.println("Entering main()");
// Create a "daemon" thread if any command-line parameter is
// passed to the program.
final Boolean daemonThread = args.length > 0;
// Create the appropriate type of thread (i.e., "user" or
// "daemon").
UserOrDaemonThread thr =
new UserOrDaemonThread(daemonThread);
// Start the thread.
thr.start();
// Sleep for 1 second and then exit.
try {
Thread.sleep(1000);
} catch (InterruptedException x) {}
System.out.println("Leaving main()");
}
}