加synchronized限制,进行同步管理

解决方案 »

  1.   

    方案
    在类里面声明一个静态属性
    public class test{
      //静态
      private static String strTest;
      
      public test{
          if (strTest == null) strTest = 0;
      } 
      
      public void A1() {
          if ("0".equals(strTest)) {
               B1();
          }
      }  public void B1() {
         //锁
         strTest = 1;     ......
         //开
         strTest = 0;
      }}大概可以吧
      

  2.   

    比如Hashtable的get方法定义如下:
    public synchronized Object get(Object key) {
        Entry tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
            return e.value;
            }
        }
        return null;
    }加了synchronized限制后,同一时刻,get方法就只允许被一个线程调用