Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions JavaConcurrencyInPractice.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<libelement value="jar://$MODULE_DIR$/lib/jcip-annotations.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/javax.servlet-3.0.jar!/" />
<libelement value="jar://$MODULE_DIR$/lib/Keygen.jar!/" />
<src_description expected_position="1">
<src_folder value="file://$MODULE_DIR$/src" expected_position="1" />
</src_description>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/bin" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="jcip-annotations.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jcip-annotations.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="javax.servlet-3.0.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/javax.servlet-3.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="Keygen.jar">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/Keygen.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
22 changes: 22 additions & 0 deletions src/Chapter4/Counter.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
37 changes: 37 additions & 0 deletions src/Chapter4/MonitorVehicleTracker.java
Original file line number Diff line number Diff line change
@@ -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<String,MutablePoint> locations;

public MonitorVehicleTracker(Map<String,MutablePoint> locations) {
this.locations = deepCopy(locations);
}

private static Map<String,MutablePoint> deepCopy(Map<String,MutablePoint> 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;
}
}
22 changes: 22 additions & 0 deletions src/Chapter4/PersonSet.java
Original file line number Diff line number Diff line change
@@ -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<Person> myset = new HashSet<Person>();

public synchronized void addPerson(Person p) {
myset.add(p);
}
public synchronized boolean containsPerson(Person p) {
return myset.contains(p);
}

}
class Person {}
23 changes: 23 additions & 0 deletions src/Chapter4/PrivateLock.java
Original file line number Diff line number Diff line change
@@ -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{}