代码块上的同步
我理解代码块的同步意味着特定代码一次只能由一个线程访问,即使许多线程正在等待访问该代码。
当我们在 run 方法中编写线程类时,我们通过给定对象来启动同步块。 例如
class MyThread extends Thread{
String sa;
public MyThread(String s){
sa=s;
}
public void run(){
synchronized(sa){
if(sa.equals("notdone"){
//do some thing on object
}
}
}
}
,这里我们给同步块一个对象,这需要什么。我们如何为该特定代码块提供同步
I understood synchronization of a block of code means that particular code will be accessed by only one thread at time even many thread are waiting to access that.
when we are writing thread class in run method we starting synchronized block by giving object.
for example
class MyThread extends Thread{
String sa;
public MyThread(String s){
sa=s;
}
public void run(){
synchronized(sa){
if(sa.equals("notdone"){
//do some thing on object
}
}
}
}
here we gave sa object to synchronized block what is the need of that.any how we are going to provide synchronization for that particular block of code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议
I would suggest
同步块的参数对象是块锁定的对象。
因此,所有具有相同对象的同步块排除彼此(以及该同一对象的所有同步方法)的同时执行。
因此,如果您有这个示例
,现在我们有两个同步块(A 和 B,位于类
ExampleA
和ExampleB
中),并且有两个锁对象(o1 和 o2)。如果我们现在看一下同时执行,我们可以看到:
因此,同步仅取决于参数对象,而不取决于同步块的选择。
在您的示例中,您使用的是:
这看起来像您在比较和工作时试图避免有人将您的实例变量
sa
更改为另一个字符串 - 但它并不能避免这种情况。同步不适用于变量,它适用于对象 - 并且有问题的对象通常应该是包含变量的某个对象(您的当前
MyThread
对象)例,可通过this
访问),或仅用于同步且不会更改的特殊对象。正如 Peter Lawrey 所说,String 对象通常是同步锁的糟糕选择,因为所有相等的 String 文字都是同一个对象(即会排除彼此的同步块),而相等的非文字字符串(例如在运行时创建)则不是同步锁的最佳选择。相同的对象,因此不会排除其他此类对象或文字的同步块,这通常会导致微妙的错误。
The parameter object of the synchronized block is the object on which the block locks.
Thus all synchronized blocks with the same object are excluding each other's (and all synchronized methods' of this same object) simultaneous execution.
So if you have this example
Now we have two synchronized blocks (A and B, in classes
ExampleA
andExampleB
), and we have two lock objects (o1 and o2).If we now look at the simultaneous execution, we can see that:
Thus, the synchronization depends only on the parameter object, not on the choice of synchronization block.
In your example, you are using this:
This looks like you try to avoid that someone changes your instance variable
sa
to another string while you are comparing it and working - but it does not avoid this.Synchronization does not work on a variable, it works on an object - and the object in question should usually be either some object which contains the variable (the current
MyThread
object in your case, reachable bythis
), or a special object used just for synchronization, and which is not changed.As Peter Lawrey said, String objects usually are bad choices for synchronization locks, since all equal String literals are the same object (i.e. would exclude each other's synchronized blocks), while a equal non-literal string (e.g. created at runtime) is not the same object, and thus would not exclude synchronized blocks by other such objects or literals, which often leads to subtle bugs.
在此对象上同步的所有线程都将等待,直到当前线程完成其工作。例如,如果您对希望同步的集合进行读/写操作,这非常有用。所以你可以在
set
和get
方法中编写同步块。在这种情况下,如果一个线程正在读取信息,则并非所有其他想要读取或写入的线程都会等待。All threads synchronized on this objects will wait until current thread finishes its work. This is useful for example if you have read/write operation to collection that your wish to synchronized. So you can write sychronized block in methods
set
andget
. In this case if one thread is reading information not all other threads that want to either read or write will wait.那么问题是块同步的对象的功能是什么?
对象的所有实例都有所谓的监视器。在正常执行中,该监视器是无主的。
希望进入同步块的线程必须占有对象监视器。然而,一次只有一个线程可以拥有监视器。因此,如果监视器当前未被拥有,则线程将占有该监视器并执行同步代码块。线程离开同步块时释放监视器。
如果监视器当前被拥有,则需要进入同步块的线程必须等待监视器被释放,以便它可以获得所有权并进入同步块。可以有多个线程在等待,如果是这样,则只有一个线程将被授予监视器的所有权。剩下的就回去等待。
So the question is what is the function of the object that a block synchronizes on?
All instances of Object have what is called a monitor. In normal execution this monitor is unowned.
A thread wishing to enter a synchronized block must take possession of the object monitor. Only one thread can posses the monitor at a time, however. So, if the monitor is currently unowned, the thread takes possession and executes the synchronized block of code. The thread releases the monitor when it leaves the synchronized block.
If the monitor is currently owned, then the thread needing to enter the synchronized block must wait for the monitor to be freed so it can take ownership and enter the block. More than one thread can be waiting and if so, then only one will be given ownership of the monitor. The rest will go back to waiting.