-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestThreads.java
55 lines (48 loc) · 1.22 KB
/
TestThreads.java
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
54
55
public class TestThreads {
public static void main(String[] args) {
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
Thread one = new Thread(t1);
Thread two = new Thread(t2);
one.start();
two.start();
}
}
class Accum {
private static Accum a = new Accum();
private int counter = 0;
private Accum() {};
public static Accum getAccum(){
return a;
}
public void updateCounner(int add){
counter+=add;
}
public int getCount(){
return counter;
}
}
class ThreadOne implements Runnable {
Accum a = Accum.getAccum();
public void run(){
for (int x = 0; x < 98; x++){
a.updateCounner(1000);
try {
Thread.sleep(50);
} catch (InterruptedException ex){ }
}
System.out.println("one " + a.getCount());
}
}
class ThreadTwo implements Runnable {
Accum a = Accum.getAccum();
public void run(){
for (int x = 0; x < 99; x++){
a.updateCounner(1);
try {
Thread.sleep(50);
} catch (InterruptedException ex){ }
}
System.out.println("two " + a.getCount());
}
}