-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairWorker.java
96 lines (87 loc) · 2.72 KB
/
PairWorker.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Week 6 : Programming Assignment 4
QN :Execution of two or more threads occurs in a random order. The keyword 'synchronized'
in Java is used to control the execution of thread in a strict sequence. In the following,
the program is expected to print the output as given below. Do the necessary use of 'synchronized'
keyword, so that, the program prints the Final sum as given below:
-----------------OUTPUT-------------------
Final sum:6000
-------------------------------------------------
...............................................................................*/
/*
A simple class that demonstrates using the 'synchronized'
keyword so that multiple threads may send it messages.
The class stores two ints, a and b; sum() returns
their sum, and inc() increments both numbers.
<p>
The sum() and incr() methods form a "critical section" --
they can compute the wrong thing if run by multiple threads
at the same time. The sum() and inc() methods are declared
"synchronized" -- they respect the lock in the receiver object.
*/
class Pair {
private int a, b;
public Pair() {
a = 0;
b = 0;
}
/* Answer is given below for --+ NPTEL +--
...............................................................................*/
public int sum() {
return(a+b); }
synchronized public void inc() {
a++;
b++;
}
}
//...............................................................................
/*
A simple worker subclass of Thread.
In its run(), sends 1000 inc() messages
to its Pair object.
(1000 may not be big enough to exhibit the bug on uniprocessor --
hardware more like 1000000 may be required).
*/
public class PairWorker extends Thread {
public final int COUNT = 1000;
private Pair pair;
// Ctor takes a pointer to the pair we use
public PairWorker(Pair pair) {
this.pair = pair;
}
// Send many inc() messages to our pair
public void run() {
for (int i=0; i<COUNT; i++) {
pair.inc();
}
}
/*
Test main -- Create a Pair and 3 workers.
Start the 3 workers -- they do their run() --
and wait for the workers to finish.
*/
public static void main(String args[]) {
Pair pair = new Pair();
PairWorker w1 = new PairWorker(pair);
PairWorker w2 = new PairWorker(pair);
PairWorker w3 = new PairWorker(pair);
w1.start();
w2.start();
w3.start();
// the 3 workers are running
// all sending messages to the same object
// we block until the workers complete
try {
w1.join();
w2.join();
w3.join();
}
catch (InterruptedException ignored) {}
System.out.println("Final sum:" + pair.sum()); // should be 6000
/*
If sum()/inc() were not synchronized, the result would
be 6000 in some cases, and other times random values
like 5979 due to the writer/writer conflicts of multiple
threads trying to execute inc() on an object at the same time.
*/
}
}