【问题标题】:Java synchronizing a method in a classJava同步类中的方法
【发布时间】:2013-10-12 14:06:36
【问题描述】:

基本上,我的层次结构是我有一个 A 类,它启动 B 类的 100 个线程,而 B 类使用一个名为 FileCreator 的类。 FileCreator 类有一个名为 writeToFile() 的同步方法。 这就是我在 B 类中实例化它的方式:

FileCreator newFile = new FileCreator(downloadFolder, content, page.getWebURL());
newFile.writeToFile();

现在我的问题是 writeToFile() 实际上没有同步。基本上这就是我对writeToFile()所做的事情

public synchronized void writeToFile() {
        System.out.println("Thread accessed");
        //Some stuff here
        System.out.println("Thread FINISHED!");
}

但是我在控制台中得到了这个结果:

Thread accessed
Thread accessed
Thread FINISHED!
Thread FINISHED!

所以它并不是真正同步的。因为这些类是由不同的线程访问的,所以我假设这会导致问题。有没有办法真正同步我的方法,以便一次只有一次访问?

【问题讨论】:

    标签: java multithreading synchronization thread-safety synchronized


    【解决方案1】:

    我会使用“虚拟”对象并对其进行同步。 块级别更高效,因为它不会锁定整个方法。

    Object xLock = new Object(); // !!! in you main thread
    
    ....
    
    public void writeToFile() {
        synchronized(xLock ){
          System.out.println("Thread accessed");
          //Some stuff here
          System.out.println("Thread FINISHED!");
        }       
    }
    

    当然你也可以写:

     public void writeToFile() {
        synchronized(this){
          System.out.println("Thread accessed");
          //Some stuff here
          System.out.println("Thread FINISHED!");
        }       
      }
    

    }

    请记住,xLock 应该在主线程中启动。

    作为参考

    =======================

    方法层

    class MethodLevel {
    
      //shared among threads
    SharedResource x, y ;
    
    public void synchronized method1() {
       //multiple threads can't access
    }
    public void synchronized method2() {
      //multiple threads can't access
    }
    
     public void method3() {
      //not synchronized
      //multiple threads can access
     }
    }
    

    块级

    class BlockLevel {
      //shared among threads
      SharedResource x, y ;
    
      //dummy objects for locking
      Object xLock = new Object();
      Object yLock = new Object();
    
        public void method1() {
         synchronized(xLock){
        //access x here. thread safe
        }
    
        //do something here but don't use SharedResource x, y
        // because will not be thread-safe
         synchronized(xLock) {
           synchronized(yLock) {
          //access x,y here. thread safe
          }
         }
    
         //do something here but don't use SharedResource x, y
         //because will not be thread-safe
        }//end of method1
     }
    

    【讨论】:

    • 查看我的编辑,只有synchronized(xLock ){/*..*/}里面的内容
    • 是的,我刚刚明白了。所以我应该创建一个单例 xLock?
    • 是的,只需像Object lock = new Object() 一样创建单个Object 并使用上述方法。
    • 您是否在主类A 中创建了Object xLock = new Object();
    • 好的,我刚刚修好了,抱歉,这是我的单身问题:)
    【解决方案2】:

    它在每个实例的基础上同步。实例方法上的synchronized 表示

    synchronized(this) {
        ...
    }
    

    但由于您有 100 个实例,它们都不会阻塞。您需要在共享对象上进行同步。将lock 对象传递给每个实例,或者只创建一个传递给每个Thread 的实例。 synchronize 在那个实例上。

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多