[英]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.