java匿名类和同步以及“this”
我相信,我正在 JAVA GUI 中处理竞争条件。
我有一些方法可以在匿名类中创建“匿名方法”,如下所示:
synchronized foo()
{
someMethod(new TimerTask()
{
public synchronized run()
{
//stuff
}
};
}
问题:该 run 方法是否在 TimerTask 对象或 foo 所在的类上同步?
问题2:如果我去掉了 run() 声明中的“synchronized”,而是在 run() 主体内添加了一个 synchronized(this) {} 块,那么“this”会引用 TimerTask 对象还是引用该对象是包含 foo() 的方法的实例吗?
请帮我一下。
谢谢, 吉布
I am dealing with a race condition, I believe, in my JAVA GUI.
I have some methods that create an "anonymous method" inside an anonymous class like this:
synchronized foo()
{
someMethod(new TimerTask()
{
public synchronized run()
{
//stuff
}
};
}
QUESTION: is that run method synchronized on the TimerTask object or the class that foo is in?
QUESTION2: if I got rid of the "synchronized" in the run() declaration, and instead have a synchronized(this) {} block inside the run() body, would "this" refer to the TimerTask object or to the object that is an instance of the method that contains foo()?
Please help me out here.
Thanks,
jbu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
run
方法在TimerTask
本身上同步。 同步实例方法始终 在this
对象上同步。 (类方法在Class
对象上同步。)如果要在
foo
所属的对象上同步,则需要 限定this
关键字。 假设 < code>foo() 是Bar
类的成员,在TimerTask
的run()
方法中,可以使用The
run
method is synchronized on theTimerTask
itself. Synchronized instance methods are always synchronized onthis
object. (Class methods are synchronized on theClass
object.)If you want to synchronize on the object of which
foo
is a member, you need to qualify thethis
keyword. Supposefoo()
is a member of theBar
class, inside therun()
method ofTimerTask
, you can use我很确定这些答案,但我无法挖掘出好的来源atm。
第一个问题:
同步将锁定 TimerTask。
第二个问题:
this 指的是 TimerTask; 如果你想锁定包含对象,你可以使用 MyContainingObject.this
I'm pretty sure of these answers, but I can't dig up a good source atm.
The first question:
synchronized will lock on the TimerTask.
Second question:
this refers to the TimerTask; if you wanted to lock on the containing object you'd use MyContainingObject.this
只有一个线程可以访问 swing 元素。 那是 AWT-EventQueue-0。 你需要意识到这一点。 如果您的其他线程正在淹没或更改元素,则 gui 很可能会崩溃。
要使用此线程运行您的图形用户界面:
如果您有匿名类,这将为您提供您所在类的实例,因此如果您在匿名类中编写此。 是该类的实例。 要获取您想要编写的类的实例:
嗯,你写的上面的代码告诉我这一点。 您对部分代码进行了两次保护。
当您编写同步方法时,这意味着一次只有一个线程可以访问该方法。 当同步方法解锁时,其他线程会等待。
There is only one thread that can have access to swing elements. Thats AWT-EventQueue-0. You need to be aware of this. If other of your threads are drowing or changeing elements there is very good probability that gui will crash.
To run your gui with this thread:
and if you have anonymus classes this will give you instance of class in which you are, so if you are writteing in anonymus class this. is instance of that class. To get instance of class you want write:
hmm this above code you wrote tells me this. You pretected part of the code twice.
When you write syncronized method it means that only one thread can access this method at one time. Other threads waits while syncronized method is unlocked.
如果您正在寻找同步 foo() 和 run() 那么您可以创建一个显式锁定对象,例如
Final Object lock = new Object();
然后对其进行同步。
If you are looking for synchronizing the foo() and run() then you may create an explicit lock object like
final Object lock = new Object();
and then synchronize on it.