synchronized块没有锁定对象引用 [英] synchronized block not locking the object reference
问题描述
class Demo
{
void demo()
{
System.out.println("Inside demo of "+Thread.currentThread().getName());
try
{
Thread.sleep(1000000);
}
catch(InterruptedException exc)
{
System.out.println(Thread.currentThread().getName()+" interrupted");
}
}
}
class MyThread1 implements Runnable
{
Thread thread;
Demo d;
MyThread1(String name, Demo ob)
{
d = ob;
thread = new Thread(this, name);
thread.start();
}
@Override
public void run()
{
System.out.println(thread.getName()+" starting");
synchronized(d)
{
d.demo();
}
System.out.println(thread.getName()+" ending");
}
}
class MyThread2 implements Runnable
{
Thread thread;
Demo d;
MyThread2(String name, Demo ob)
{
d = ob;
thread = new Thread(this, name);
thread.start();
}
@Override
public void run()
{
System.out.println(thread.getName()+" starting");
d.demo();
System.out.println(thread.getName()+" ending");
}
}
class TimePass
{
public static void main(String args[])
{
Demo d = new Demo();
MyThread1 mt1 = new MyThread1("Thread 1", d);
MyThread2 mt2 = new MyThread2("Thread 2", d);
}
}
输出
线程1开始
Thread 1 starting
线程1的内部演示
线程2开始
线程2的内部演示
由于 Thread.sleep(1000000)
,执行尚未结束。
我已经传递了相同的类实例演示
到类 MyThread1
和 MyThread2
的构造函数。
I have passed the same instance of class Demo
to both the constructors of classes MyThread1
and MyThread2
.
Demo d = new Demo();
MyThread1 mt1 = new MyThread1("Thread 1", d);
MyThread2 mt2 = new MyThread2("Thread 2", d);
d.demo
的调用 MyThread1
位于 synchronized
块中。
调用 d。
在 MyThread2
中的演示 synchronized
块中不。
The call to d.demo
in MyThread1
is in a synchronized
block.
The call to d.demo
in MyThread2
is not in a synchronized
block.
因此,当 MyThread1
正在执行时,由于已同步
阻止, d
的监视器应该被锁定,导致拒绝访问 d.demo()
MyThread2
。
So, when MyThread1
is executing, due to the synchronized
block, the monitor of d
should be locked, resulting in denial of access to d.demo()
by the MyThread2
.
但这种情况不会发生。
预期输出为
Thread 1 starting
Inside demo of Thread1
Thread 2 starting
(输出在 Thread.sleep(1000000)
之前完成。)
所以,我的基本问题是:如何 MyThread2.d.demo()
即使<$ c也成功执行$ c> MyThread1.d.demo()尚未完成已同步
块?
So, my basic question is: How MyThread2.d.demo()
is executing successfully even if MyThread1.d.demo()
is not yet finished the synchronized
block?
推荐答案
因此,当MyThread1执行时,由于
synchronized
块,应锁定d
的监视器,导致MyThread2拒绝访问d.demo()
。 / p>
So, when MyThread1 is executing, due to the
synchronized
block, the monitor ofd
should be locked, resulting in denial of access tod.demo()
by the MyThread2.
只有当MyThread2还有 synchronized
块时才会发生这种情况。当一个线程在一个对象上同步时,如果其他线程也尝试在同一个对象上进行同步,则会阻塞其他线程。如果他们不同步,他们就不会。没有什么可以阻止从不同步的线程访问对象。
That would only happen if MyThread2 also had a synchronized
block. When one thread is synchronized on an object, other threads will be blocked if they also try to synchronize on that same object. If they don't synchronize, they won't be. There's nothing that stops access to an object from threads that don't synchronize on it.
同步是一种合作机制。它只适用于所有线程一起工作。
Synchronization is a cooperative mechanism. It only works when all threads work together.
这篇关于synchronized块没有锁定对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!