同步问题
public class PingPong implements Runnable {
synchronized void hit(long n) {
for (int i = 1; i < 3; i++)
System.out.print(n + "-" + i + " ");
}
public static void main(String[] args) {
new Thread(new PingPong()).start();
new Thread(new PingPong()).start();
}
public void run() {
hit(Thread.currentThread().getId());
}
}
上面的代码给我输出 8-1 9-1 8-2 9-2
但随着函数同步,它应该给出输出 8-1 8-2 9-1 9-2 或 9-1 9-2 8-1 8-2
谁能解释一下吗?
public class PingPong implements Runnable {
synchronized void hit(long n) {
for (int i = 1; i < 3; i++)
System.out.print(n + "-" + i + " ");
}
public static void main(String[] args) {
new Thread(new PingPong()).start();
new Thread(new PingPong()).start();
}
public void run() {
hit(Thread.currentThread().getId());
}
}
The above code gives me output 8-1 9-1 8-2 9-2
But as the function is synchronized it should give output 8-1 8-2 9-1 9-2 or 9-1 9-2 8-1 8-2
Can anyone explain please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
方法上的“同步”会同步该方法对特定对象的所有访问。
因此,如果您有 1 个
PingPong
对象,则没有 2 个线程会同时进入其hit
方法,但如果有 2 个对象,则一个线程可以进入其hit
方法其中一个对象,而另一个线程运行另一个对象的hit
对象。这是有道理的,因为您通常使用同步来确保不受干扰地访问当前对象本地的内容。如果您的对象代表某些线程有时需要不受干扰地访问的外部实体,请将您的对象设置为单例。
'synchronized' on a method synchronizes all accesses of that method on a particular object.
So if you have 1
PingPong
object no 2 threads will simultaneously enter itshit
method, but with 2 objects one thread can enter thehit
method of one of the objects while another thread runs thehit
object of the other.This makes sense because you usually use
synchronized
to ensure undisturbed access to stuff local to the current object. If your object represents some external entity to which threads sometimes need undisturbed access, make your object a singleton.要获得您想要的行为,请尝试进行以下更改:
仅使用
PingPong
的单个实例,hit()
上的synchronized
修饰符将防止一个线程中断另一个线程,您的输出将是X-1 X-2 Y-1 Y-2
或反之亦然。To get the behaviour you want, try making the following change:
With only a single instance of
PingPong
, thesynchronized
modifier onhit()
will prevent one thread from interrupting the other, and your output will be eitherX-1 X-2 Y-1 Y-2
or visa-versa.根据 http://download.oracle.com/javase/tutorial/essential /concurrency/syncmeth.html
因此,由于您有两个 PingPong 对象,synchronized 关键字无法按您的预期工作。
As per http://download.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
So since you have two PingPong objects the synchronized keyword does not work as you expected.
不..同步只是意味着特定方法不会同时由两个线程执行,但是如果有两个线程正在工作,它们无论如何都可以以任何顺序调用该方法。简单来说,他们不会同时访问。
此外,它还同步对象实例。作为两个实例,根本不存在同步。
Nope .. synchronized simply mean that a specific method will not be executed by two threads at the same time, but if you have two threads working they can call that method in any order anyway. Simply, they will not access at the same time.
Moreover, it synchronizes on the object instances. Being two instances, there is no synchronization at all.
您没有锁定或解锁标志。所以他们会同时运行。
You have no lock or unlock flags. so they will both run at the same time.
将您的代码更改为
Change your code to