Skip to content
Merged
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
Binary file added lib/Keygen.jar
Binary file not shown.
38 changes: 38 additions & 0 deletions src/Chapter2/SonFatherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Chapter2;

public class SonFatherTest {

public static void main(String[] args) {
new Son().getFatherName();
System.out.println();
}

}

class Father{
protected String fatherName = "Father";
public Father() {
System.out.println("Father������");
}
}

class Son extends Father{
public Son() {
System.out.println("Son������");
}
public String getFatherName() {
System.out.println("the super class:"+super.getClass());
return super.fatherName;
}
}


interface FatherI{
String name = "maiyun";
}

class SonI implements FatherI{
public void getFatherName() {
System.out.println("the super class:"+super.getClass());
}
}
15 changes: 15 additions & 0 deletions src/Chapter3/Holder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Chapter3;

public class Holder {
private int n;

public Holder(int n) {
this.n = n;
}

public void assertSanity() {
if(n != n) {
throw new AssertionError("This statement is false");
}
}
}
16 changes: 16 additions & 0 deletions src/Chapter3/MutableInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Chapter3;

import net.jcip.annotations.NotThreadSafe;

@NotThreadSafe
public class MutableInteger {
private int value;

public int get() {
return value;
}

public void set(int value) {
this.value = value;
}
}
21 changes: 21 additions & 0 deletions src/Chapter3/NoVisibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Chapter3;

public class NoVisibility {
private static boolean ready;
private static int number;

private static class ReaderThread extends Thread{
public void run() {
while(!ready) {
Thread.yield();
}
System.out.println(number);
}
}

public static void main(String...args) {
new ReaderThread().start();
number = 42;
ready = true;
}
}
24 changes: 24 additions & 0 deletions src/Chapter3/OneValueCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package Chapter3;

import java.math.BigInteger;
import java.util.Arrays;

import net.jcip.annotations.Immutable;

@Immutable
public class OneValueCache {
private final BigInteger lastNumber;
private final BigInteger[] lastFactors;

public OneValueCache(BigInteger lastNumber,BigInteger[] lastFactors) {
this.lastNumber = lastNumber;
this.lastFactors = lastFactors;
}
public BigInteger[] getFactors(BigInteger i) {
if(lastNumber == null || !lastNumber.equals(i)) {
return null;
}else {
return Arrays.copyOf(lastFactors, lastFactors.length);
}
}
}
17 changes: 17 additions & 0 deletions src/Chapter3/Publish.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Chapter3;

import java.util.HashSet;
import java.util.Set;

/**
* ����һ������
* ��򵥵ķ���һ������ķ����ǽ���������ñ�����һ�����еľ�̬������
*
*/
public class Publish {
public static Set<Integer> integers;

public void initialize() {
integers = new HashSet<Integer>();
}
}
26 changes: 26 additions & 0 deletions src/Chapter3/SafeListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Chapter3;

/**
* ʹ�ù���������ֹthis�����ڹ���������ݳ�
* @author dell
*
*/
public class SafeListener {
/*
private final EventListener listener;

private SafeListener() {
listener = new EventListener() {
public void onEvent(Event e) {
doSomthing(e);
}
};
}

public static EventListener newInstance(EventSource source){
SafeListener safe = new SafeListener();
source.registerListener(safe.listener);
return safe;
}
*/
}
17 changes: 17 additions & 0 deletions src/Chapter3/SynchronizedInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Chapter3;

import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

@ThreadSafe
public class SynchronizedInteger {
@GuardedBy("this") private int value;

public synchronized int get() {
return value;
}

public synchronized void set(int value) {
this.value = value;
}
}
23 changes: 23 additions & 0 deletions src/Chapter3/ThisEscape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Chapter3;

import java.util.ArrayList;
import java.util.List;

/**
* ��ʽ��ʹthis�����ݳ�
* @author dell
*
*/
public class ThisEscape {
/*
public ThisEscape(EventSource source) {
source.registerListener(
new EventListener(){
public void onEvent(Event e){
doSomething(e);
}
}
);
}
*/
}
20 changes: 20 additions & 0 deletions src/Chapter3/ThreadStooges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package Chapter3;

import java.util.HashSet;
import java.util.Set;

import net.jcip.annotations.Immutable;

@Immutable
public final class ThreadStooges {
private final Set<String> stooges = new HashSet<String>();

public ThreadStooges() {
stooges.add("Moe");
stooges.add("Larry");
stooges.add("Curly");
}
public boolean isStooge(String name) {
return stooges.contains(name);
}
}
15 changes: 15 additions & 0 deletions src/Chapter3/UnsafeStates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Chapter3;

/**
* ʹ�ڲ��Ŀɱ�״̬�ݳ�
* @author dell
*
*/
public class UnsafeStates {
private String[] states = new String[] {
"AK","AL"
};
public String[] getStates() {
return states;
}
}
17 changes: 17 additions & 0 deletions src/Chapter3/VolatileUseCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Chapter3;

/**
* volatile�����ĵ����÷�
* ���ij��״̬���ж��Ƿ��˳�ѭ��
*
*/
public class VolatileUseCase {
volatile boolean asleep;

//...
void isOut() {
while(!asleep) {
//do something
}
}
}
50 changes: 50 additions & 0 deletions src/Chapter3/introdution/章节3-对象的共享
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
���ڴ�ɼ��ԣ�����ϣ��ȷ����һ���߳��޸��˶���״̬�������߳̿��Կ���������״̬�仯��

����Ҫ����ָ��ִ�е�˳�����޷�Ԥ֪��ͬ�߳�֮���ָ����Ժ���˳��ִ�С�

��һ���߳̿��ܻ��ij������������ֵ���������һ��������ʧЧֵ��

����Ͱ�ȫ�ԣ����߳���û��ͬ��������¶�ȡ����ʱ�����ܻ�õ�һ��ʧЧֵ�����������ֵ����֮ǰij���߳����õ�ֵ��������һ�����ֵ��

�����ڷ�volatile���͵�long��double������JVM���н�64λ�Ķ�������д�����ֽ������32λ�IJ���������һ���̶߳�����32λ��һ��������32λ��

���ڷ���ij�������ҿɱ�ı���ʱҪ�������߳���ͬһ������ͬ��������Ϊ��ȷ��ij���߳�д��ñ�����ֵ���������߳���˵�ǿɼ��ġ�

�����������⣬�ڴ�ɼ��ԡ�

��volatile:�ٲ��Ὣ��volatile�����ϵIJ����������ڴ����һ���������volatile�������ᱻ�����ڼĴ������߶��������������ɼ��ĵط������
�ڶ�ȡvolatile����ʱ�ܻ᷵������д���ֵ��

���ڷ���volatile����ʱ����ִ�м������������Ҳ�Ͳ���ִ���߳�������

�����ڴ�ɼ��ԵĽǶ�������д��volatile�����൱���˳�ͬ������飬��ȡvolatile�����൱�ڽ���ͬ������顣

���������Ƽȿ���ȷ���ɼ����ֿ���ȷ��ԭ���ԣ���volatile����ֻ��ȷ���ɼ��ԡ�

��volatile����long��double���͵ı���ʹ������ӵ���˼򵥵ĸ�ֵ�뷵�ز�����ԭ���ԡ�

��������ʹ�������ڵ�ǰ������֮��Ĵ�����ʹ�á��ݳ�����ij����Ӧ�÷����Ķ��󱻷�����

��������һ������ʱ���ڸö���ķ�˽���������õ����ж���ͬ���ᱻ������

������һ�����󴫵ݸ�ij���ⲿ����ʱ�����൱�ڷ������������

��this�����ݳ����Ӷ���Ĺ��캯���з������������������δ������ɵġ�

��ThreadLocal��Ϊÿ��ʹ�ñ������̶߳���һ�ݶ����ĸ������߳���ֹ����Щֵ����Ϊ�������ա�

�����ɱ���󣺶����ڴ�������״̬�Ͳ��ܱ��޸ġ����ɱ����һ�����̰߳�ȫ�ġ�

����ȫ�����ķ�ʽ��
���ھ�̬��ʼ�������г�ʼ��һ���������á�
�ڽ���������ñ��浽volatile���͵������AtomicReference�����С�
�۽���������ñ��浽ij����ȷ��������final�������С�
�ܽ���������ñ��浽һ���������������С�

��������JVM�ڲ�������ͬ�����ƣ����ͨ�����ַ�ʽ��ʼ�����κζ��󶼿��Ա���ȫ�ط�����

������ķ�������ȡ�������Ŀɱ��ԣ�
�ٲ��ɱ�������ͨ�����������������
����ʵ���ɱ�������ͨ����ȫ��ʽ��������
�ۿɱ�������ͨ����ȫ��ʽ���������������̰߳�ȫ�Ļ�����ij��������������