java 对一个类变量加锁\解锁 各位高人哪,现在有一个问题,我们需要在代码A中对一个类变量设置一个值,并且,希望能把它锁住,然后在另外一块代码B中,对这个变量进行取值操作,之后,对他进行解锁操作,不知道这样行不行哦……请各位大虾不吝赐教!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么感觉这是c里面用共享内存的编程习惯呢……那给这个类再加一个boolean变量Aa,作为这个变量A的可修改标志当Aa为true时可修改想锁上时,把Aa置成false解锁时,把Aa置成true public class LockTest{ private int value; private boolean isSet; public synchronized void setValue(int v){ while(isSet){ try{ this.wait(); } catch(InterruptedException e){ e.printStackTrace(); } } this.value=v; this.isSet=true; this.notifyAll(); } public synchronized int getValue(){ while(!isSet){ try{ this.wait(); } catch(InterruptedException e){ e.printStackTrace(); } } isSet=false; this.notifyAll(); return value; }}另外,一定要枷锁吗?,不用加锁的话用队列也可以完成你要求的功能, 效率还高些 感觉像生产者消费者问题啊,有个小例子挺有意思的,网上看到的,有一个桌子,桌子上面有一个盘子,盘子里只能放一颗鸡蛋,A专门往盘子里放鸡蛋,如果盘子里有鸡蛋,则一直等到盘子里没鸡蛋,B专门从盘子里拿鸡蛋,如果盘子里没鸡蛋,则等待直到盘子里有鸡蛋。import java.util.ArrayList;import java.util.List;public class Plate { List<Object> eggs = new ArrayList<Object>(); public synchronized Object getEgg() { if (eggs.size() == 0) { try { wait(); } catch (InterruptedException e) { } } Object egg = eggs.get(0); eggs.clear();// 清空盘子 notify();// 唤醒阻塞队列的某线程到就绪队列 System.out.println("拿到鸡蛋"); return egg; } public synchronized void putEgg(Object egg) { if (eggs.size() > 0) { try { wait(); } catch (InterruptedException e) { } } eggs.add(egg);// 往盘子里放鸡蛋 notify();// 唤醒阻塞队列的某线程到就绪队列 System.out.println("放入鸡蛋"); } static class AddThread extends Thread{ private Plate plate; private Object egg=new Object(); public AddThread(Plate plate){ this.plate=plate; } public void run(){ for(int i=0;i<5;i++){ plate.putEgg(egg); } } } static class GetThread extends Thread{ private Plate plate; public GetThread(Plate plate){ this.plate=plate; } public void run(){ for(int i=0;i<5;i++){ plate.getEgg(); } } } public static void main(String args[]){ try { Plate plate=new Plate(); Thread add=new Thread(new AddThread(plate)); Thread get=new Thread(new GetThread(plate)); add.start(); get.start(); add.join(); get.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("测试结束"); }} 用新的并发类 java.util.concurrent.SynchronousQueue 类吧SynchronousQueue queue = new SynchronousQueue();代码A:queue.put(value);代码B:queue.take(); 是这样的,我们的实际问题是,通过系统A中的一个超链接,首先链接到自己的一个struts action中,首先在这个action中,获得Config类的类变量userName,然后把点击超链接的用户名赋值给Config类的类变量userName,之后生成一个url,导向另一个系统B,该系统在某一个时刻通过webservice调用系统A的一个方法,该方法会使用Config类的类变量userName,我就想,能不能做一个控制,只有在webservice中处理完一个userName后,下一个点击A系统超链接的人才能改变Config类的类变量userName,这样会不会造成大量排队的现象啊? 从你的描述开看,A,B系统之间属于异步通信,也就是说B并不是立即响应A,A也不是请求响应等待,如果锁Config,是会造成排队的。可以考虑在A中用个map来记住对B的请求,当B回调A时(回调函数的参数可以增加一个map的key),回调处理从map中取出相关信息进行处理 为什么不直接把userName同url传递到B系统? 这段可是同步的吗? 当我开2个线程的时候分别调用getValue()和setValue() 我想知道 value的值是多少? 这个回溯错哪部了? 初学者关于继承中STATIC 的应用问题 在1至100范围内产生50个随机数,打印到控制台,取出所有的偶数降序排列后打印到控制台。 Javascript 語句下載遠端服務器.txt文件 循环处理文件出错 流对文件的操作的问题 语音的网络传输 怎样把十六进制的数据,十进制的数据,二进制的数据变成人可读的字符串!(高分,不够可以再加) 这是为什么????总得到NULL 过两天可能要用JAVA~开发一个什么工程项目管理的什么软件~具体怎么样我还不知道~ 求教final的用法 局域网(分子网的)主机搜索
那给这个类再加一个boolean变量Aa,作为这个变量A的可修改标志
当Aa为true时可修改
想锁上时,把Aa置成false
解锁时,把Aa置成true
private int value;
private boolean isSet;
public synchronized void setValue(int v){
while(isSet){
try{
this.wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
this.value=v;
this.isSet=true;
this.notifyAll();
}
public synchronized int getValue(){
while(!isSet){
try{
this.wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
isSet=false;
this.notifyAll();
return value;
}
}
另外,一定要枷锁吗?,不用加锁的话用队列也可以完成你要求的功能, 效率还高些
import java.util.ArrayList;
import java.util.List;public class Plate { List<Object> eggs = new ArrayList<Object>(); public synchronized Object getEgg() {
if (eggs.size() == 0) {
try {
wait();
} catch (InterruptedException e) {
}
} Object egg = eggs.get(0);
eggs.clear();// 清空盘子
notify();// 唤醒阻塞队列的某线程到就绪队列
System.out.println("拿到鸡蛋");
return egg;
} public synchronized void putEgg(Object egg) {
if (eggs.size() > 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
eggs.add(egg);// 往盘子里放鸡蛋
notify();// 唤醒阻塞队列的某线程到就绪队列
System.out.println("放入鸡蛋");
}
static class AddThread extends Thread{
private Plate plate;
private Object egg=new Object();
public AddThread(Plate plate){
this.plate=plate;
}
public void run(){
for(int i=0;i<5;i++){
plate.putEgg(egg);
}
}
}
static class GetThread extends Thread{
private Plate plate;
public GetThread(Plate plate){
this.plate=plate;
}
public void run(){
for(int i=0;i<5;i++){
plate.getEgg();
}
}
}
public static void main(String args[]){
try {
Plate plate=new Plate();
Thread add=new Thread(new AddThread(plate));
Thread get=new Thread(new GetThread(plate));
add.start();
get.start();
add.join();
get.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("测试结束");
}
}
从你的描述开看,A,B系统之间属于异步通信,也就是说B并不是立即响应A,A也不是请求响应等待,如果锁Config,是会造成排队的。可以考虑在A中用个map来记住对B的请求,当B回调A时(回调函数的参数可以增加一个map的key),回调处理从map中取出相关信息进行处理
为什么不直接把userName同url传递到B系统?