-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$04.java
57 lines (42 loc) · 1.53 KB
/
Problem$04.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
package chapter_thirty_two;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 32.4 (Synchronize threads) Write a program that launches 1,000 threads. Each thread
* adds 1 to a variable sum that initially is 0. You need to pass sum by reference to
* each thread. In order to pass it by reference, define an Integer wrapper object to
* hold sum. Run the program with and without synchronization to see its effect.
*
*
* @author Sharaf Qeshta
* */
public class Problem$04
{
private static Integer integer = new Integer(0);
public static void main(String[] args)
{
ExecutorService executor = Executors.newFixedThreadPool(1_000);
for (int i = 0; i < 1_000; i++)
executor.execute(Problem$04::add1WithSynchronization);
executor.shutdown();
while (executor.isTerminated());
// output always 1000
System.out.println(integer);
integer = new Integer(0);
ExecutorService executor2 = Executors.newFixedThreadPool(1_000);
for (int i = 0; i < 1_000; i++)
executor2.execute(Problem$04::add1WithoutSynchronization);
executor2.shutdown();
while (executor2.isTerminated());
// the output differ
System.out.println(integer);
}
public static synchronized void add1WithSynchronization()
{
integer = new Integer(integer.intValue() + 1);
}
public static void add1WithoutSynchronization()
{
integer = new Integer(integer.intValue() + 1);
}
}