//atomicboolean
package com.huang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicBooleanTest {
private AtomicBoolean bool = new AtomicBoolean(true);
public static void main(String[] args) {
AtomicBooleanTest abt = new AtomicBooleanTest();
abt.test();
}
private void test() {
ExecutorService es = Executors.newFixedThreadPool(2);
int len = 2;
for (int i = 0; i < len; i++)
{
final int v = i;
es.execute(new Runnable() {
@Override
public void run() {
bool.compareAndSet(true, false);
}
});
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.SECONDS);
System.out.println("result:" + bool.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/
//atomicintegerarray
package com.huang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicIntegerArrayTest {
private int len = 10;
private AtomicIntegerArray array = new AtomicIntegerArray(new int[len]);
public static void main(String[] args) {
AtomicIntegerArrayTest ait = new AtomicIntegerArrayTest();
ait.test();
}
private void test() {
ExecutorService es = Executors.newFixedThreadPool(5);
for(int i = 0; i < len; i++)
{
final int index = i;
es.execute(new Runnable() {
@Override
public void run() {
array.set(index, index + 100);
}
});
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.SECONDS);
array.compareAndSet(5, 105, 100005);
int size = array.length();
for(int i = 0;i < size;i++)
{
System.out.println(array.get(i));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//
//atomicinteger
package com.huang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
/**
* Created by Administrator on 2018\10\8 0008.
*/
public class AtomicIntegerTest {
private AtomicInteger value = new AtomicInteger(100000);
public static void main(String[] args) {
AtomicIntegerTest ait = new AtomicIntegerTest();
ait.test();
}
private void test() {
int len = 10000;
ExecutorService exec = Executors.newFixedThreadPool(10);
for (int i = 0; i < len; i++)
{
exec.execute(new Runnable() {
@Override
public void run() {
value.decrementAndGet();
}
});
}
exec.shutdown();
try {
exec.awaitTermination(5, TimeUnit.SECONDS);
value.compareAndSet(90000, 100001);
System.out.println("result:" + value.intValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/
//atomiclongarray
package com.huang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicLongArrayTest {
private int len = 10;
private AtomicLongArray array = new AtomicLongArray(new long[len]);
public static void main(String[] args) {
AtomicLongArrayTest ait = new AtomicLongArrayTest();
ait.test();
}
private void test() {
ExecutorService es = Executors.newFixedThreadPool(5);
for(int i = 0; i < len; i++)
{
final int index = i;
es.execute(new Runnable() {
@Override
public void run() {
array.set(index, index + 100);
}
});
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.SECONDS);
array.compareAndSet(5, 105, 1000000005);
int size = array.length();
for(int i = 0;i < size;i++)
{
System.out.println(array.get(i));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
///
//atomiclong
package com.huang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicLongTest {
private AtomicLong along = new AtomicLong(0);
public static void main(String[] args) {
AtomicLongTest alt = new AtomicLongTest();
alt.test();
}
private void test() {
ExecutorService es = Executors.newFixedThreadPool(5);
int len = 10;
for(int i = 0; i < len; i++)
{
es.execute(new Runnable() {
@Override
public void run() {
along.incrementAndGet();
}
});
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.SECONDS);
along.compareAndSet(10, 100);
System.out.println("value:" + along.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//atomicreferencearray
package com.huang.test.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReferenceArray;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicReferenceArrayTest {
private int len = 5;
private AtomicReferenceArray refArray = new AtomicReferenceArray(new MyObj[len]);
class MyObj {
int value;
public MyObj(int value)
{
this.value = value;
}
}
private void test()
{
ExecutorService es = Executors.newFixedThreadPool(5);
for(int i = 0;i < len;i++)
{
final int index = i;
es.execute(new Runnable() {
@Override
public void run() {
refArray.set(index, new MyObj(index));
}
});
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.SECONDS);
refArray.compareAndSet(3, refArray.get(3), new MyObj(100));
int size = refArray.length();
for(int i = 0;i < size;i++)
{
MyObj obj = refArray.get(i);
System.out.println(obj.value);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AtomicReferenceArrayTest art = new AtomicReferenceArrayTest();
art.test();
}
}
///
//atomicreference
package com.huang.test.concurrent;
import java.util.concurrent.atomic.AtomicReference;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicReferenceTest {
private AtomicReference ref;
public AtomicReferenceTest()
{
}
private void test()
{
MyObj obj1 = new MyObj();
MyObj obj2 = new MyObj();
MyObj obj3 = new MyObj();
ref = new AtomicReference<>(obj1);
MyObj obj = ref.get();
assert obj == obj1;
ref.getAndSet(obj2);
// ref.compareAndSet(obj1, obj2);
obj = ref.get();
assert obj == obj2;
ref.compareAndSet(obj2, obj3);
obj = ref.get();
assert obj == obj3;
}
class MyObj
{
}
public static void main(String[] args) {
AtomicReferenceTest art = new AtomicReferenceTest();
art.test();
}
}
//atomicreferencefieldupdater
package com.huang.test.concurrent;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
/**
* Created by Administrator on 2018\10\10 0010.
*/
public class AtomicRefrenceFieldUpaterTest {
private AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(MyObj.class, MyField.class, "ff");
static class MyObj
{
volatile MyField ff = null;
public MyObj()
{
}
public void setFf(MyField ff) {
this.ff = ff;
}
public MyField getFf() {
return ff;
}
}
static class MyField
{
int v;
public MyField(int v)
{
this.v = v;
}
}
private void test()
{
MyObj obj = new MyObj();
MyField fie = new MyField(1);
obj.setFf(fie);
updater.compareAndSet(obj, fie, new MyField(2));
System.out.println("value:" + obj.getFf().v);
}
public static void main(String[] args) {
AtomicRefrenceFieldUpaterTest test = new AtomicRefrenceFieldUpaterTest();
test.test();
}
}