package main.javaConcurrencyInPractice.ifeve;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
//import sun.misc.Unsafe;
import java.lang.reflect.Method;
import java.util.*;
/**
* java.util.concurrent.atomic包中提供了三种原子更新类型,基本原子类、对象引用原子类、数组原子类、更新字段原子类,这些类中提供了基本的原子操作。
* // setup to use Unsafe.compareAndSwapInt for updates
* private static final Unsafe unsafe = Unsafe.getUnsafe();
* unsafe主要提供了三种原子操作:compareAndSwapObject,compareAndSwapInt和compareAndSwapLong,在atomicInteger类55行中有compareAndSwapInt方法的使用
* working memory is abstract of cache and register.工作内存是cache和寄存器的抽象。
* @author pc
*
*/
public class AtomicClassTest {
public static void main(String[] args){
//testAtomicSafe();
//testAtomicArraySafe();
testAtomicReference();
}
public static void testAtomicReference(){
// 原子更新引用reference主要有三个类:atomicReference、AtomicReferenceFieldUpdater
// 对于某个类的原子更新操作。原子更新引用类型、原子更新字段类型、
final Person person = new Person();
// 原子更新字段类型,使用的时候必须使用静态方法newUpdater创建一个更新器,更新某个字段值。
final AtomicIntegerFieldUpdater<Person> atomicReference = AtomicIntegerFieldUpdater.newUpdater(Person.class , "age");
List<Thread> list = new ArrayList<Thread>();
for(int j = 0; j < 1000; j++){
Thread t = new Thread(new Runnable(){
public void run(){
atomicReference.getAndDecrement(person); // 线程安全的更新引用。
}
});
list.add(t);
}
for(Thread thread : list){
thread.start();
}
try{
Thread.sleep(1000);
} catch (Exception e){
}
System.out.println(person.age);
}
public static void testAtomicArraySafe(){
// atomicIntegerArray 需要传递数组进构造函数,atomicIntegerArray通过复制新的数组,对于数组的操作保证原子性。
int[] array = {1 , 2 , 3};
AtomicIntegerArray atomicArray = new AtomicIntegerArray(array);
atomicArray.compareAndSet(0, 1, 2);
atomicArray.getAndSet(0, 8);
System.out.println(array[0]);
System.out.println(atomicArray.get(0));
}
public static void testAtomicSafe(){
final List<Thread> list = new ArrayList<Thread>();
final SafeCount count = new SafeCount();
for(int j = 0; j < 1000; j++){
Thread t = new Thread(new Runnable(){
public void run(){
count.increment();
}
});
list.add(t);
}
for(Thread thread : list){
thread.start();
}
try {
Thread.sleep(1000);// 保证执行完毕!
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count.i);
}
}
class Count{
public int i = 0;
public int increment(){
i++;
return i;
}
public int decrement(){
i--;
return i;
}
public synchronized int increment(boolean flag){// 两种方式保证线程安全。 1, 对象锁的方式。 2 , 通过原子类提供的线程安全的基本方法
i++;
return i;
}
public synchronized int decrement(boolean flag){
i--;
return i;
}
}
class SafeCount{
AtomicInteger i = new AtomicInteger(0);
public SafeCount(){
for(Method method : i.getClass().getDeclaredMethods()){
System.out.println(method);
}
}
public int increment(){
return i.getAndIncrement();
}
public int decrement(){
return i.getAndDecrement();
}
}
class Person{
volatile int age ;
String name;
String gender;
public void incrementAge(){
age++;
}
public void setAge(int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setGender(String gender){
this.gender = gender;
}
}
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
//import sun.misc.Unsafe;
import java.lang.reflect.Method;
import java.util.*;
/**
* java.util.concurrent.atomic包中提供了三种原子更新类型,基本原子类、对象引用原子类、数组原子类、更新字段原子类,这些类中提供了基本的原子操作。
* // setup to use Unsafe.compareAndSwapInt for updates
* private static final Unsafe unsafe = Unsafe.getUnsafe();
* unsafe主要提供了三种原子操作:compareAndSwapObject,compareAndSwapInt和compareAndSwapLong,在atomicInteger类55行中有compareAndSwapInt方法的使用
* working memory is abstract of cache and register.工作内存是cache和寄存器的抽象。
* @author pc
*
*/
public class AtomicClassTest {
public static void main(String[] args){
//testAtomicSafe();
//testAtomicArraySafe();
testAtomicReference();
}
public static void testAtomicReference(){
// 原子更新引用reference主要有三个类:atomicReference、AtomicReferenceFieldUpdater
// 对于某个类的原子更新操作。原子更新引用类型、原子更新字段类型、
final Person person = new Person();
// 原子更新字段类型,使用的时候必须使用静态方法newUpdater创建一个更新器,更新某个字段值。
final AtomicIntegerFieldUpdater<Person> atomicReference = AtomicIntegerFieldUpdater.newUpdater(Person.class , "age");
List<Thread> list = new ArrayList<Thread>();
for(int j = 0; j < 1000; j++){
Thread t = new Thread(new Runnable(){
public void run(){
atomicReference.getAndDecrement(person); // 线程安全的更新引用。
}
});
list.add(t);
}
for(Thread thread : list){
thread.start();
}
try{
Thread.sleep(1000);
} catch (Exception e){
}
System.out.println(person.age);
}
public static void testAtomicArraySafe(){
// atomicIntegerArray 需要传递数组进构造函数,atomicIntegerArray通过复制新的数组,对于数组的操作保证原子性。
int[] array = {1 , 2 , 3};
AtomicIntegerArray atomicArray = new AtomicIntegerArray(array);
atomicArray.compareAndSet(0, 1, 2);
atomicArray.getAndSet(0, 8);
System.out.println(array[0]);
System.out.println(atomicArray.get(0));
}
public static void testAtomicSafe(){
final List<Thread> list = new ArrayList<Thread>();
final SafeCount count = new SafeCount();
for(int j = 0; j < 1000; j++){
Thread t = new Thread(new Runnable(){
public void run(){
count.increment();
}
});
list.add(t);
}
for(Thread thread : list){
thread.start();
}
try {
Thread.sleep(1000);// 保证执行完毕!
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count.i);
}
}
class Count{
public int i = 0;
public int increment(){
i++;
return i;
}
public int decrement(){
i--;
return i;
}
public synchronized int increment(boolean flag){// 两种方式保证线程安全。 1, 对象锁的方式。 2 , 通过原子类提供的线程安全的基本方法
i++;
return i;
}
public synchronized int decrement(boolean flag){
i--;
return i;
}
}
class SafeCount{
AtomicInteger i = new AtomicInteger(0);
public SafeCount(){
for(Method method : i.getClass().getDeclaredMethods()){
System.out.println(method);
}
}
public int increment(){
return i.getAndIncrement();
}
public int decrement(){
return i.getAndDecrement();
}
}
class Person{
volatile int age ;
String name;
String gender;
public void incrementAge(){
age++;
}
public void setAge(int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setGender(String gender){
this.gender = gender;
}
}