class HL7ServerAdvanced{
    public String receivemsgstring;
    //获取内部类,SafeStorage是一个接口 
    public SafeStorage getSafeStorage() {
            return new MySafeStorge();
    }
    class MySafeStorge implements SafeStorage{
          //内部类中想要同步的函数
          public void store(Transportable theMessage) throws FileNotFoundException,IOException{
                  receivemsgstring = "abc";
                  FileOutputStream fos = new FileOutputStream("recv.txt", true);
                  fos.write(receivemsgstring.getBytes());
                  fos.flush();
                  if (fos != null) {
                        fos.close();
                        fos = null;
                  }
          }
  public static void main(String[] args) {
     final HL7ServerAdvanced hsd = new HL7ServerAdvanced();
     class PrintSubThread extends Thread {
        public void run() { 
          try {
             while (true) {
                sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值  
                if (hsd.receivemsgstring != null ) {
                         System.out.println(hsd.receivemsgstring);
                         hsd.receivemsgstring = null;
                       }
                    }
               }
          catch (InterruptedException iex) {
             iex.printStackTrace();
          }
      }
    }
    class StoreSubThread extends Thread{
       public void run(){
             //调用内部类中的方法
             hsd.getSafeStorage().store(); 
        }   
     }
    PrintSubThread pst = new PrintSubThread();
    StoreSubThread sst = new StoreSubThread();
    pst.start();
    sst.start();  
  }
}我想将两个线程同步访问类中的变量receivemsgstring,StoreSubThread线程先对receivemsgstring调用内部类的store方法赋值并存储,再由PrintSubThread输出并置为空值。 我不知道如何加synchronized关键字来同步类中的变量receivemsgstring,还涉及了内部类的函数与类外的函数同时要操作类变量的问题,向大家指教,谢谢大家!

解决方案 »

  1.   

    补充一下,我就是想对类中的变量receivemsgstring同步访问,我不知道如何对receivemsgstring上锁,以完成同步功能,谢谢大家!
      

  2.   

    类中的函数可以用synchronized(this){}上锁,但是当函数为类中内部类的函数时,我觉得this应该指的是内部类的对象,所以认为synchronized(this){}可能不好使了。
    我也不知道是否是这种情况...
    如何对类中的变量receivemsgstring同步访问?
    多谢大家指教!谢谢!
      

  3.   


    如果你只是想同步 receivemsgstring, 应该用 AtomicReference<V>, 不需要用 synchronize
      

  4.   

    public String receivemsgstring;
    public Object lock = new Object(); // 这个就是锁。在你的任何需要同步访问receivemsgstring的代码段,用synchronized(lock){
      ...
    }
    就可以了。
      

  5.   

    谢谢两位的回复!我还想问java2000_net一句,在类中store()函数中用lock锁住,在类外用 “对象.lock”锁住,可以同步吗?
    //在类中的函数
    public void store(Transportable theMessage){
           synchronized(lock){
              ...
          }
      }
     //在要访问receivemsgstring的函数在类外
    class HL7Application{
           class HL7ServerAdvanced hsd = new HL7ServerAdvanced();
          
          class PrintSubThread extends Thread {
             public void run() { 
                 while (true) {
                    sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值  
                      synchronized(hsd.lock){
                      if (hsd.receivemsgstring != null ) {
                             System.out.println(hsd.receivemsgstring);
                             hsd.receivemsgstring = null;
                           }
                        }
                      }
                    }
                 }
      }我想问一下lockhsd.lock可否完成同步?是不是按上面的写法就正确了?谢谢!      
     
      

  6.   

    对了,java2000_net有空帮再我看看这个贴,谢谢! http://topic.csdn.net/u/20090706/10/dd255660-2a55-4cc3-92e2-54099003131d.html?31981
      

  7.   

    我试了,lock和hsd.lock是可以的完成同步的。我把PrintSubThread sleep()改成wait(),在store()函数中加入notifyAll(),实现了同步。
    class PrintSubThread extends Thread {
             public void run() { 
                 while (true) {
                    //sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值  
                      synchronized(hsd.lock){
                      hsd.lock.wait();
                      if (hsd.receivemsgstring != null ) {
                             System.out.println(hsd.receivemsgstring);
                             hsd.receivemsgstring = null;
                           }
                        }
                      }
                    }
                 }public void store(Transportable theMessage) {
                   synchronized(lock){
                        //函数中的其他内容.........
                      lock.notifyAll();
                  }
              }这样很好的实现了线程的同步访问receivemsgstring ,谢谢java2000_net, 谢谢大家!