简体   繁体   English

通过静态类中的方法同步变量

[英]synchronized a variable through a method from a static class

I've been reading about synchronization in java and I've got a question. 我一直在阅读有关Java同步的信息,但我有一个问题。 So the examples I've seen are either a class creates a synchronized block on a local object. 因此,我所看到的示例是一个类在本地对象上创建一个同步块。 Or a synchronized method. 或同步方法。 Neither of those two is ideal for my situation. 这两个都不适合我的情况。 So I have a static instance of a class that holds all the data from the entire application. 因此,我有一个类的静态实例,该实例包含整个应用程序中的所有数据。 Variables on that class are created as "private", I created getters to retrieve those values. 该类上的变量被创建为“私有”,我创建了获取方法以获取这些值。 Some of those values need to be accessed by only one thread at a time from difference classes across the application. 这些值中的某些值一次仅需要一个线程从应用程序中的不同类访问。 Since they are created as private I'm using the synchronized block as follows... 由于它们是作为私有创建的,因此我按如下方式使用了同步块...

public class Music{
    private ArrayList<Album> albums;
    private static Music musicObject = new Music();

    public ArrayList<Album> getAlbums()
    {
         return albums;
    }
    public Music getInstance()
    {
         return musicObject;
    }
}

public class Album {

    private Date releaseDate;

    private String singer;

    public Date getReleaseDate()
    {
        return releaseDate;
    }

    public String getSinger()
   {
        return releaseDate;
   }  
}
public class AlbumRetrieval
{
   private Music data;

   public AlbumRetrieval()
   {
      data = Music.getInstance();
      synchronized(data.getAlbums())
      {
          //No other thread can access the albums variable in here
      }
   }
}

Can I lock/synchronized an external variable by accessing it through a method call.? 我可以通过方法调用访问外部变量来锁定/同步它吗?

Yes, you can do that. 是的,你可以这么做。 If you couldn't, the code would not compile. 如果不能,则代码将无法编译。

synchronized() takes an object as "argument". synchronized()将一个对象作为“参数”。 It makes sure that two threads can't enter a synchronized block on this object at the same time. 确保两个线程不能同时在此对象上输入同步块。

So, if data.getAlbums() returns a list, the posted code synchronizes on this list, and no other thread will be able to enter a block that is synchronized on this exact same list while the first thread has not exited its synchronized block. 因此,如果data.getAlbums()返回一个列表,则发布的代码将在此列表上同步,并且在第一个线程尚未退出其同步块的情况下,其他线程将无法输入在此完全相同的列表上同步的块。

Note that it's quite a bad strategy to enforce thread-safety. 请注意,强制执行线程安全性是一个非常糟糕的策略。 You'll have, every time you access data, to make sure to synchronize correctly. 每次访问数据时,您都必须确保正确同步。 This is very error-prone, and can easily lead to deadlocks. 这非常容易出错,并且很容易导致死锁。 You'd better encapsulate all the accesses to the albums into a well-defined class, which takes care of proper synchronization, rather than return a non-thread-safe list and rely on each and every caller to apply the appropriate synchronization. 您最好将对专辑的所有访问封装到一个定义良好的类中,该类负责适当的同步,而不是返回非线程安全的列表并依赖每个调用者来应用适当的同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM