简体   繁体   English

当我们说同步(实例字段)时,它意味着什么?

[英]what does it mean when we say synchronized(instance field)..?

Attached the code.. what does this mean, synchronized(m)..?? 附上代码..这是什么意思,synchronized(m).. ?? why we should use that..?? 为什么我们应该使用.. ?? What's the difference between synchronized(this) & synchronized(m)..?? synchronized(this)和synchronized(m)之间有什么区别.. ??

class Waiter implements Runnable {

    Message m;

    public Waiter(Message m) {
        this.m = m;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        synchronized (m) {
            try {
                System.out.println("Waiting to get notified at time " +System.currentTimeMillis());
                m.wait();

            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println("Waiter thread notified at time "+System.currentTimeMillis());
            System.out.println("Message processed ");
        }
    }
}

The java keyword synchronized is used to synchronize different threads by one instance, acting as a mutual exclusive semaphore. java关键字synchronized用于通过一个实例同步不同的线程,充当互斥信号量。 Hence, the argument passed to synchronized is the instance which can be owned by one thread exclusively. 因此,传递给synchronized的参数是可以由一个线程专有的实例。 It is up to you, the programmer, on which instance you like to synchronize your threads. 由程序员决定,您希望在哪个实例上同步线程。

But it is a good idea to use the resource, which is under racing conditions, or the owning instance of that resource. 但是,使用资源(在竞争条件下)或该资源的拥有实例是一个好主意。 The later you start a synchronized block and the earlier you leave it, the better your application will scale. 稍后启动同步块,越早离开它,应用程序扩展得越好。

The difference between synchronized(this) and synchronized(m) is that by synchronizing on this , you synchronize on the entire instance. 之间的差synchronized(this)synchronized(m)是通过对同步this ,就在整个实例同步。 So, as you would expect, no body would be able to synchronize on this while you hold the lock. 因此,当你所期望的,没有任何机构将能够在同步this ,而你持有锁。

public synchronized void foo() {
    // Handle shared resource
}

is similar to 类似于

public void foo() { 
    synchronize(this) { 
        // Handle shared resource 
    } 
}

By using objects, such as m , you get a more fine grained control over what you want to synchronize and when. 通过使用诸如m对象,您可以对要同步的内容以及何时进行更精细的控制。 But remember that if someone uses foo(), as shown above, it will not stop access to methods that are not synchronized on this : 但请记住,如果有人使用FOO(),如上图所示,它不会停下来上未同步方法可以访问this

public void anotherLock() {
    synchronized(m) {
        // Should handle another shared resource
        // otherwise you might get unexpected results
    }
}

While a thread is using foo() , another thread can access anotherLock() . 当一个线程使用foo() ,另一个线程可以访问anotherLock()

synchronized is used for thread safety. synchronized用于线程安全。 In your case it is used for implementing observer pattern. 在您的情况下,它用于实现观察者模式。 you want to wait for something to happen on Message object and then only process it so someone will notify on Message object m for which you are waiting (m.wait()). 你想等待Message对象上发生的事情,然后只处理它,这样有人会通知你正在等待的消息对象m(m.wait())。

When you wait on some object you need to take lock on that object for which you always need to put the wait() statement in a synchronized block on wait object. 当你在某个对象上等待时,你需要锁定该对象,你总是需要将wait()语句放在wait对象的synchronized块中。 That is why you are using synchronized(m). 这就是你使用synchronized(m)的原因。

You can not replace it with synchronized(this) as you are calling wait() on object m so synchronized should be on m only. 你不能用synchronized(this)替换它,因为你在对象m上调用wait(),所以同步应该只在m上。

Somewhere in your application you must be calling m.notify() or m.notifyAll() which will resume your wait() on m. 在你的应用程序的某个地方,你必须调用m.notify()或m.notifyAll(),它将在m上恢复你的wait()。

暂无
暂无

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

相关问题 当我们说 ArrayList 不同步时,这意味着什么? - What does it mean when we say an ArrayList is not synchronized? 当我们说Hashtable或Vector是同步的时候意味着什么? - What does it mean when we say Hashtable or Vector is synchronized? 当他们说StringBuffer类已同步时,这到底是什么意思? 是否已声明该类的所有方法同步? - What exactly does it mean when they say that StringBuffer class is synchronized? Are all methods of that class declared synchronized? 当我们说特定数据结构是缓存友好的时候意味着什么? - What does it mean when we say that a particular datastructure is cache friendly? 当我们说java中的Byte宽​​度为8位时,它是什么意思? - What does it mean when we say the width of Byte in java is 8 bit? 当我们在Liferay中说Group时,这是什么意思?Group由什么组成? 及其用途? - What does that mean when we say Group in Liferay and what all does Group consists of? and its uses? 当我们说 tomcat 服务器正在“运行”时,这意味着什么。 幕后到底发生了什么 - What does it mean when we say tomcat server is “running”. What exactly is running behind the scenes 当我们说“整数是不可变的”时,我们到底是什么意思 - what do we mean exactly when we say “ints are immutable” “同步”是什么意思? - What does 'synchronized' mean? 当他们说http是无国籍时,这是什么意思 - what does it mean when they say http is stateless
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM