众所周知,在java中有两种方法(或者以上)解决并发的临界资源问题
一种是通过加锁来实现的,即synchronized。
还有一种是ThreadLocal的方法来解决的
但是本人还是对后者有不理解的地方(或者说是什么情况下该用synchronized,什么情况下该用),特此来请教各路大神
同步采用了"以时间换空间的思想",而ThreadLocal采用"以空间换取时间的"思想
public class Chopsticks
{
String name;
private static ThreadLocal enable=new ThreadLocal(){
public Boolean initialValue(){
return true;
}
};
//boolean Enable = true;
public Chopsticks(String name)
{
this.name = name;
}
//synchronized
public boolean pickup(){
if(this.enable.get()==false){
return false;
}
this.enable.set(false);
return true;
// try {
// while(Enable==false) //筷子被用
// {
// this.wait();
// }
// this.Enable =false; //筷子没有被用
// }
// catch (Exception e)
// {
// e.getMessage();
// }
}
public void putdown() {
// this.Enable =true;
// this.notifyAll();
this.enable.set(true);
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class zhexuejia extends Thread
{
String name; //属性
Chopsticks left;
Chopsticks right;
public zhexuejia(String name, Chopsticks l, Chopsticks r) { //初始化
this.name = name;
left = l;
right = r;
}
@Override
public void run() {
left.pickup();
System.out.println(name + " 眼明手快,一把抓起 "+left.name);
right.pickup();
System.out.println(name + " 眼明手快,一把抓起 "+right.name);
System.out.println(name + " 开始进餐");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println(name + " 酒足饭饱,打了个饱嗝,心满意足的放下了 "+left.name+" 和 " +right.name);
left.putdown();
right.putdown();
}
public static void main(String []args)
{
Chopsticks k1 = new Chopsticks("1号");
Chopsticks k2 = new Chopsticks("2号");
Chopsticks k3 = new Chopsticks("3号");
Chopsticks k4 = new Chopsticks("4号");
Chopsticks k5 = new Chopsticks("5号");
zhexuejia z1=new zhexuejia("1",k5,k1);
zhexuejia z2=new zhexuejia("2",k1,k2);
zhexuejia z3=new zhexuejia("3",k2,k3);
zhexuejia z4=new zhexuejia("4",k3,k4);
zhexuejia z5=new zhexuejia("5",k4,k5);
ExecutorService e= Executors.newFixedThreadPool(10); //将诸线程加入线程池,并发执行
e.execute(z5);
e.execute(z2);
e.execute(z3);
e.execute(z4);
e.execute(z1);
}
}
执行下来的结果与哲学家就餐的问题不符,问题还是在ThreadLocal的问题上
问题的根源就在于什么情况用锁机制,什么情况用ThreadLocal的机制
Spring中的源代码大量使用了ThreadLocal来保持同步,不知道这种同步和哲学家就餐有什么不同呢?
疑问