各位高人哪,现在有一个问题,我们需要在代码A中对一个类变量设置一个值,并且,希望能把它锁住,然后在另外一块代码B中,对这个变量进行取值操作,之后,对他进行解锁操作,不知道这样行不行哦……请各位大虾不吝赐教!!!!!

解决方案 »

  1.   

    怎么感觉这是c里面用共享内存的编程习惯呢……
    那给这个类再加一个boolean变量Aa,作为这个变量A的可修改标志
    当Aa为true时可修改
    想锁上时,把Aa置成false
    解锁时,把Aa置成true
      

  2.   

    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;
    }
    }
    另外,一定要枷锁吗?,不用加锁的话用队列也可以完成你要求的功能, 效率还高些
      

  3.   

    感觉像生产者消费者问题啊,有个小例子挺有意思的,网上看到的,有一个桌子,桌子上面有一个盘子,盘子里只能放一颗鸡蛋,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("测试结束");
        }
    }
      

  4.   

    用新的并发类  java.util.concurrent.SynchronousQueue 类吧SynchronousQueue queue = new SynchronousQueue();代码A:queue.put(value);代码B:queue.take();
      

  5.   

    是这样的,我们的实际问题是,通过系统A中的一个超链接,首先链接到自己的一个struts action中,首先在这个action中,获得Config类的类变量userName,然后把点击超链接的用户名赋值给Config类的类变量userName,之后生成一个url,导向另一个系统B,该系统在某一个时刻通过webservice调用系统A的一个方法,该方法会使用Config类的类变量userName,我就想,能不能做一个控制,只有在webservice中处理完一个userName后,下一个点击A系统超链接的人才能改变Config类的类变量userName,这样会不会造成大量排队的现象啊?
      

  6.   


    从你的描述开看,A,B系统之间属于异步通信,也就是说B并不是立即响应A,A也不是请求响应等待,如果锁Config,是会造成排队的。可以考虑在A中用个map来记住对B的请求,当B回调A时(回调函数的参数可以增加一个map的key),回调处理从map中取出相关信息进行处理
      

  7.   


    为什么不直接把userName同url传递到B系统?
      

  8.   

    这段可是同步的吗? 当我开2个线程的时候分别调用getValue()和setValue()  我想知道 value的值是多少?