1
1
package concurrency ;
2
2
3
+ import java .util .concurrent .ExecutorService ;
4
+ import java .util .concurrent .Executors ;
5
+ import java .util .concurrent .atomic .AtomicInteger ;
6
+
3
7
/**
4
8
*
5
- * @author chengfeili
9
+ * @author chengfeili
6
10
* Jun 24, 2017 8:17:32 PM
7
11
*
12
+ * thread safety is the property of an object that guarantees safe
13
+ * execution by multiple threads at the same time. Now that we have
14
+ * multiple threads capable of accessing the same objects in memory, we
15
+ * have to make sure to organize our access to this data such that we
16
+ * don’t end up with invalid or unexpected results. Since threads run in
17
+ * a shared environment and memory space, how do we prevent two threads
18
+ * from interfering with each other?
8
19
*/
9
20
public class SynchronizingDataAccess {
10
21
22
+ /**
23
+ * Atomic is the property of an operation to be carried out as a single unit
24
+ * of execution without any interference by another thread.
25
+ *
26
+ * Unlike our previous sample output, the numbers 1 through 10 will always
27
+ * be output.
28
+ *
29
+ * 2 3 1 4 5 6 7 8 9 10
30
+ *
31
+ * 1 4 3 2 5 6 7 8 9 10
32
+ *
33
+ * 1 4 3 5 6 2 7 8 10 9
34
+ */
35
+
36
+ private AtomicInteger sheepCount = new AtomicInteger (0 );
37
+
38
+ private void incrementAndReport () {
39
+ System .out .print (sheepCount .incrementAndGet () + " " );
40
+ }
41
+
42
+ /**
43
+ * Synchronizing Methods
44
+ *
45
+ * We can add the synchronized modifier to any instance method to
46
+ * synchronize automatically on the object itself
47
+ * the following two method definitions are equivalent:
48
+ */
49
+
50
+ /**
51
+ private void incrementAndReport1() {
52
+ synchronized (this) {
53
+ System.out.print((++sheepCount) + " ");
54
+ }
55
+ }
56
+
57
+ private synchronized void incrementAndReport2() {
58
+ System.out.print((++sheepCount) + " ");
59
+ }
60
+ */
61
+
62
+ /**
63
+ public static void printDaysWork() {
64
+ synchronized (SheepManager.class) {
65
+ System.out.print("Finished work");
66
+ }
67
+ }
68
+
69
+ public static synchronized void printDaysWork() {
70
+ System.out.print("Finished work");
71
+ }
72
+ */
73
+
74
+ /**
75
+ * While multi-threaded programming is about doing multiple things at the
76
+ * same time, synchronization is about taking multiple threads and making
77
+ * them perform in a more single-threaded manner.
78
+ *
79
+ * Synchronization is about protecting data integrity at the cost of performance.
80
+ */
81
+ public void costOfSynchronization () {
82
+
83
+ }
84
+ }
85
+
86
+ /**
87
+ *
88
+ * A problem occurs when two threads both execute the right side of the
89
+ * expression, reading the “old” value before either thread writes the “new”
90
+ * value of the variable
91
+ *
92
+ * the unexpected result of two tasks executing at the same time is referred to
93
+ * as a race condition.
94
+ *
95
+ * 1 2 2 3 4 5 6 7 8 9
96
+ *
97
+ * 2 4 5 6 7 8 1 9 10 3
98
+ *
99
+ * 2 1 3 4 5 6 7 8 9 10
100
+ *
101
+ *
102
+ */
103
+ class SheepManager {
104
+ private int sheepCount = 0 ;
105
+
106
+ void incrementAndReport () {
107
+ System .out .print ((++sheepCount ) + " " );
108
+ }
109
+
110
+ public static void main (String [] args ) {
111
+ ExecutorService service = null ;
112
+ try {
113
+ service = Executors .newFixedThreadPool (20 );
114
+ SheepManager manager = new SheepManager ();
115
+ for (int i = 0 ; i < 10 ; i ++)
116
+ service .submit (() -> manager .incrementAndReport ());
117
+ } finally {
118
+ if (service != null )
119
+ service .shutdown ();
120
+ }
121
+ }
11
122
}
123
+
124
+ /**
125
+ * Improving Access with Synchronized Blocks
126
+ *
127
+ * The most common technique is to use a monitor, also called a lock, to
128
+ * synchronize access. A monitor is a structure that supports mutual exclusion
129
+ * or the property that at most one thread is executing a particular segment of
130
+ * code at a given time.
131
+ *
132
+ *
133
+ * This example is referred to as a synchronized block. Each thread that arrives
134
+ * will first check if any threads are in the block.
135
+ *
136
+ * 1 2 3 4 5 6 7 8 9 10
137
+ *
138
+ */
139
+ class SheepManager2 {
140
+ private int sheepCount = 0 ;
141
+
142
+ private void incrementAndReport () {
143
+ synchronized (this ) {
144
+ System .out .print ((++sheepCount ) + " " );
145
+ }
146
+ }
147
+
148
+ public static void main (String [] args ) {
149
+ ExecutorService service = null ;
150
+ try {
151
+ service = Executors .newFixedThreadPool (20 );
152
+ SheepManager2 manager = new SheepManager2 ();
153
+ for (int i = 0 ; i < 10 ; i ++)
154
+ service .submit (() -> manager .incrementAndReport ());
155
+ } finally {
156
+ if (service != null )
157
+ service .shutdown ();
158
+ }
159
+ }
160
+ }
0 commit comments