-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBothInMethod.java
More file actions
53 lines (40 loc) · 1.15 KB
/
Copy pathBothInMethod.java
File metadata and controls
53 lines (40 loc) · 1.15 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
45
46
47
48
49
50
51
52
53
package threadbook.ch07;
public class BothInMethod extends Object {
private String objID;
public BothInMethod(String objID) {
this.objID = objID;
}
public void doStuff(int val) {
print("entering doStuff()");
int num = val * 2 + objID.length();
print("in doStuff() - local variable num=" + num);
// slow things down to make observations
try { Thread.sleep(2000); } catch ( InterruptedException x ) { }
print("leaving doStuff()");
}
public void print(String msg) {
threadPrint("objID=" + objID + " - " + msg);
}
public static void threadPrint(String msg) {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + ": " + msg);
}
public static void main(String[] args) {
final BothInMethod bim = new BothInMethod("obj1");
Runnable runA = new Runnable() {
public void run() {
bim.doStuff(3);
}
};
Thread threadA = new Thread(runA, "threadA");
threadA.start();
try { Thread.sleep(200); } catch ( InterruptedException x ) { }
Runnable runB = new Runnable() {
public void run() {
bim.doStuff(7);
}
};
Thread threadB = new Thread(runB, "threadB");
threadB.start();
}
}