-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtendThread.java
More file actions
51 lines (44 loc) · 1009 Bytes
/
Copy pathExtendThread.java
File metadata and controls
51 lines (44 loc) · 1009 Bytes
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
/*
Project 11-1
Extend Thread.
*/
class MyThread extends Thread {
int count;
// Construct a new thread.
MyThread(String name) {
super(name);
count = 0;
start(); // start the thread
}
// Begin execution of new thread.
public void run() {
System.out.println(getName() + " starting.");
try {
do {
Thread.sleep(100);
System.out.println("In " + getName() + ", count is " + count);
count++;
} while(count < 10);
}
catch(InterruptedException exc) {
System.out.println(getName() + " interrupted.");
}
System.out.println(getName() + " terminating.");
}
}
class UseThreadsImproved {
public static void main(String args[]) {
System.out.println("Main thread starting.");
MyThread mt = new MyThread("Child #1");
do {
System.out.print(".");
try {
Thread.sleep(10);
}
catch(InterruptedException exc) {
System.out.println("Main thread interrupted.");
}
} while (mt.count != 10);
System.out.println("Main thread ending.");
}
}