/**
*
*/
package com.ldar.user;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import net.sf.ehcache.search.aggregator.Count;
/**
* @author
* @date 2016年3月3日 上午9:03:51
* @desc 多线程顺序执行
*/
public class MultiThreadSequentialExecTest {
@Test
public void test(){
final LockT t= new LockT();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
t.m1();
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
t.m2();
}
});
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
t.m3();
}
});
t3.start();
}
static class T {
volatile int target =1;//只能保证内存可见性,不能保证原子性
synchronized void m1(){
for(int i=0;i<3;i++){
while(target ==2 || target == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A"+i);
target = 2;
notifyAll();
}
}
synchronized void m2(){
for(int i=0;i<3;i++){
while(target ==1 || target == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B"+i);
target = 3;
notifyAll();
}
}
synchronized void m3(){
for(int i=0;i<3;i++){
while(target ==1 || target == 2){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("C"+i);
target = 1;
notifyAll();
}
}
}
static class LockT {
private final AtomicInteger atomic = new AtomicInteger(1);
//每个线程有独立的副本,防止变量共享
private final ThreadLocal<Integer> local = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 1;
}
};
synchronized void m1(){
for(int i=0;i<3;i++){
while(atomic.get() == 2 || atomic.get() == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A"+local.get());
local.set(local.get()+1);
atomic.incrementAndGet();
//该方法只能在同步方法或同步块内部调用。如果当前线程不是对象所得持有者
notifyAll();
}
}
synchronized void m2(){
for(int i=0;i<3;i++){
while(atomic.get() == 1 || atomic.get() == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B"+local.get());
local.set(local.get()+2);
atomic.incrementAndGet();
notifyAll();
}
}
synchronized void m3(){
for(int i=0;i<3;i++){
while(atomic.get() == 1 || atomic.get() == 2){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("C"+local.get());
local.set(local.get()+3);
atomic.set(1);
notifyAll();
}
}
}
}
*
*/
package com.ldar.user;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import net.sf.ehcache.search.aggregator.Count;
/**
* @author
* @date 2016年3月3日 上午9:03:51
* @desc 多线程顺序执行
*/
public class MultiThreadSequentialExecTest {
@Test
public void test(){
final LockT t= new LockT();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
t.m1();
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
t.m2();
}
});
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
t.m3();
}
});
t3.start();
}
static class T {
volatile int target =1;//只能保证内存可见性,不能保证原子性
synchronized void m1(){
for(int i=0;i<3;i++){
while(target ==2 || target == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A"+i);
target = 2;
notifyAll();
}
}
synchronized void m2(){
for(int i=0;i<3;i++){
while(target ==1 || target == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B"+i);
target = 3;
notifyAll();
}
}
synchronized void m3(){
for(int i=0;i<3;i++){
while(target ==1 || target == 2){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("C"+i);
target = 1;
notifyAll();
}
}
}
static class LockT {
private final AtomicInteger atomic = new AtomicInteger(1);
//每个线程有独立的副本,防止变量共享
private final ThreadLocal<Integer> local = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 1;
}
};
synchronized void m1(){
for(int i=0;i<3;i++){
while(atomic.get() == 2 || atomic.get() == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A"+local.get());
local.set(local.get()+1);
atomic.incrementAndGet();
//该方法只能在同步方法或同步块内部调用。如果当前线程不是对象所得持有者
notifyAll();
}
}
synchronized void m2(){
for(int i=0;i<3;i++){
while(atomic.get() == 1 || atomic.get() == 3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B"+local.get());
local.set(local.get()+2);
atomic.incrementAndGet();
notifyAll();
}
}
synchronized void m3(){
for(int i=0;i<3;i++){
while(atomic.get() == 1 || atomic.get() == 2){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("C"+local.get());
local.set(local.get()+3);
atomic.set(1);
notifyAll();
}
}
}
}