From 9dcbc882a9b0ce215d6224907cb141755f38a07f Mon Sep 17 00:00:00 2001
From: btp <842297171@qq.com>
Date: Fri, 9 Mar 2018 15:04:32 +0800
Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
JavaConcurrencyInPractice.iml | 47 +++++++++++++++++++++++++
src/Chapter4/Counter.java | 22 ++++++++++++
src/Chapter4/MonitorVehicleTracker.java | 37 +++++++++++++++++++
src/Chapter4/PersonSet.java | 22 ++++++++++++
src/Chapter4/PrivateLock.java | 23 ++++++++++++
5 files changed, 151 insertions(+)
create mode 100644 JavaConcurrencyInPractice.iml
create mode 100644 src/Chapter4/Counter.java
create mode 100644 src/Chapter4/MonitorVehicleTracker.java
create mode 100644 src/Chapter4/PersonSet.java
create mode 100644 src/Chapter4/PrivateLock.java
diff --git a/JavaConcurrencyInPractice.iml b/JavaConcurrencyInPractice.iml
new file mode 100644
index 0000000..417aeb8
--- /dev/null
+++ b/JavaConcurrencyInPractice.iml
@@ -0,0 +1,47 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Chapter4/Counter.java b/src/Chapter4/Counter.java
new file mode 100644
index 0000000..adf6908
--- /dev/null
+++ b/src/Chapter4/Counter.java
@@ -0,0 +1,22 @@
+package Chapter4;
+
+import net.jcip.annotations.GuardedBy;
+import net.jcip.annotations.ThreadSafe;
+
+@ThreadSafe
+public final class Counter {
+ @GuardedBy("this") private long value = 0;
+
+ public synchronized long getValue() {
+ return value;
+ }
+
+ public synchronized long increment() {
+ if(value == Long.MAX_VALUE) {
+ throw new IllegalStateException("counter overflow");
+ }
+ return ++value;
+ }
+
+
+}
diff --git a/src/Chapter4/MonitorVehicleTracker.java b/src/Chapter4/MonitorVehicleTracker.java
new file mode 100644
index 0000000..4fc804a
--- /dev/null
+++ b/src/Chapter4/MonitorVehicleTracker.java
@@ -0,0 +1,37 @@
+package Chapter4;
+
+import java.util.Collections;
+import java.util.Map;
+
+import net.jcip.annotations.GuardedBy;
+import net.jcip.annotations.NotThreadSafe;
+import net.jcip.annotations.ThreadSafe;
+
+@ThreadSafe
+public class MonitorVehicleTracker {
+ @GuardedBy("this")
+ private final Map locations;
+
+ public MonitorVehicleTracker(Map locations) {
+ this.locations = deepCopy(locations);
+ }
+
+ private static Map deepCopy(Map locations){
+ return Collections.unmodifiableMap(locations);
+ }
+}
+
+@NotThreadSafe
+class MutablePoint{
+ public int x,y;
+
+ public MutablePoint() {
+ x = 0;
+ y = 0;
+ }
+
+ public MutablePoint(MutablePoint p) {
+ this.x = p.x;
+ this.y = p.y;
+ }
+}
\ No newline at end of file
diff --git a/src/Chapter4/PersonSet.java b/src/Chapter4/PersonSet.java
new file mode 100644
index 0000000..a8acacd
--- /dev/null
+++ b/src/Chapter4/PersonSet.java
@@ -0,0 +1,22 @@
+package Chapter4;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import net.jcip.annotations.GuardedBy;
+import net.jcip.annotations.ThreadSafe;
+
+@ThreadSafe
+public class PersonSet {
+ @GuardedBy("this")
+ private final Set myset = new HashSet();
+
+ public synchronized void addPerson(Person p) {
+ myset.add(p);
+ }
+ public synchronized boolean containsPerson(Person p) {
+ return myset.contains(p);
+ }
+
+}
+class Person {}
diff --git a/src/Chapter4/PrivateLock.java b/src/Chapter4/PrivateLock.java
new file mode 100644
index 0000000..c493e19
--- /dev/null
+++ b/src/Chapter4/PrivateLock.java
@@ -0,0 +1,23 @@
+package Chapter4;
+
+import net.jcip.annotations.GuardedBy;
+
+/**
+ * 通过一个私有锁来保护状态
+ * @author dell
+ *
+ */
+public class PrivateLock {
+ private final Object myLock = new Object();
+
+ @GuardedBy("myLock")
+ Widget widget;
+
+ void someMethod() {
+ synchronized(myLock) {
+ //修改或访问widget的状态
+ }
+ }
+}
+
+class Widget{}
\ No newline at end of file