简体   繁体   English

如何同步一组线程?

[英]how to synchronize a group of threads?

I would like to make a synchronized method, such that all objects from that type of thread class can only access this function one at a time. 我想做一个同步的方法,这样来自该类型的线程类的所有对象一次只能访问一个函数。

When looking at this web page , it says that: 在查看此网页时 ,它表示:

An object for which access is to be coordinated is accessed through the use of synchronized methods. 通过使用同步方法来访问要为其协调访问的对象。 These methods are declared with the synchronized keyword. 这些方法用synced关键字声明。 Only one synchronized method can be invoked for an object at a given point in time. 在给定的时间点只能为一个对象调用一个同步方法。 This keeps synchronized methods in multiple threads from conflicting with each other. 这样可以防止多个线程中的同步方法相互冲突。

This not what I am looking for, as I said, because I want to be able to make calls on the class mutually exclusive. 正如我所说,这不是我想要的,因为我希望能够在类上互斥。

To make a method synchronize on the class (instead of a specific instance of the class), write: 要使方法在类上同步(而不是类的特定实例),请编写:

public static synchronized ReturnType methodName() {
  ...
}

or 要么

public static ReturnType methodName() {
  synchronized(ThisClass.class) {
    ...
  }
}

Use a static lock: 使用静态锁:

private final static Object lock = new Object();

public void foo() {
    synchronized(lock) {
        ...
    }
}

Thought I'd provide some more information for posterity. 以为我会为后代提供更多信息。

I want to be able to make calls on the class mutually exclusive. 我希望能够在类上互斥。

So it depends on whether you are talking about a lock on an instance of the class or all instances of the class. 因此,这取决于您是在谈论该类实例的锁定还是该类的所有实例的锁定。 When you are synchronizing on an object, other threads will block if they lock on the same object instance. 在对象上进行同步时,如果其他线程锁定在同一对象实例上,则它们将阻塞。

When an instance method is synchronized , it is if you are locking on this . 当一个实例方法是synchronized ,这是如果你是在锁定this The following are the same: 以下是相同的:

  public void synchronized foo() {
     ...
  }

Same as: 如同:

  public void foo() {
     synchronized (this) {
        ...
     }
  }

Typically, as @Tudor mentions, you should consider using a lock object instead of making methods synchronized . 通常情况下,作为@Tudor提到,你应该考虑使用锁对象,而不是制造方法的synchronized This allows you to lock around the specific lines you want to protect. 这使您可以锁定要保护的特定行。

Any instance methods that are synchronized will block other calls to the same instance of the class. 任何已synchronized实例方法都将阻止对该类相同实例的其他调用。 If you instead want to block all instances of a class, then you do what @JimN recommended and synchronize static methods. 相反,如果要阻止类的所有实例,则可以执行@JimN建议的操作并同步静态方法。 As he mentions, it's the same as synchronizing on the class object: 正如他提到的,这与在类对象上同步相同:

public static synchronized ReturnType methodName() {

If for some reason you need to lock across all objects then I'd wrap your lock in a singleton and write some lock/unlock methods using a ReentrantLock . 如果出于某种原因您需要锁定所有对象,那么我会将您的锁定包装在一个单例中,并使用ReentrantLock编写一些锁定/解锁方法。

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

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