java匿名类和同步以及“this”

发布于 2024-07-19 05:34:12 字数 477 浏览 13 评论 0原文

我相信,我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩一世无双 2024-07-26 05:34:12

run 方法在 TimerTask 本身上同步。 同步实例方法始终this 对象上同步。 (类方法在 Class 对象上同步。)

如果要在 foo 所属的对象上同步,则需要 限定 this 关键字。 假设 < code>foo() 是 Bar 类的成员,在 TimerTaskrun() 方法中,可以使用

public void run() {
  synchronized(Bar.this) {
    ...
  }
}

The run method is synchronized on the TimerTask itself. Synchronized instance methods are always synchronized on this object. (Class methods are synchronized on the Class object.)

If you want to synchronize on the object of which foo is a member, you need to qualify the this keyword. Suppose foo() is a member of the Bar class, inside the run() method of TimerTask, you can use

public void run() {
  synchronized(Bar.this) {
    ...
  }
}
酒解孤独 2024-07-26 05:34:12

我很确定这些答案,但我无法挖掘出好的来源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

2024-07-26 05:34:12

只有一个线程可以访问 swing 元素。 那是 AWT-EventQueue-0。 你需要意识到这一点。 如果您的其他线程正在淹没或更改元素,则 gui 很可能会崩溃。
要使用此线程运行您的图形用户界面:

  try {
            SwingUtilities.invokeAndWait(new Runnable(){
                public void run(){
                    Swing_Prozor1 prozor = new Swing_Prozor1();
                }
            });
        } catch (InterruptedException e) {
            //namjerno zanemareno
        } catch (InvocationTargetException e) {
            //namjerno zanemareno
        }

如果您有匿名类,这将为您提供您所在类的实例,因此如果您在匿名类中编写此。 是该类的实例。 要获取您想要编写的类的实例:

类名.this

嗯,你写的上面的代码告诉我这一点。 您对部分代码进行了两次保护。
当您编写同步方法时,这意味着一次只有一个线程可以访问该方法。 当同步方法解锁时,其他线程会等待。

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:

  try {
            SwingUtilities.invokeAndWait(new Runnable(){
                public void run(){
                    Swing_Prozor1 prozor = new Swing_Prozor1();
                }
            });
        } catch (InterruptedException e) {
            //namjerno zanemareno
        } catch (InvocationTargetException e) {
            //namjerno zanemareno
        }

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:

ClassName.this

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.

听不够的曲调 2024-07-26 05:34:12

如果您正在寻找同步 foo() 和 run() 那么您可以创建一个显式锁定对象,例如

Final Object lock = new Object();

然后对其进行同步。

foo() {
    synchronized(lock) {
       someMethod(new TimerTask() {
          public void run() {
              synchronized(lock)  {
                     //stuff
              }
          }
      }

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.

foo() {
    synchronized(lock) {
       someMethod(new TimerTask() {
          public void run() {
              synchronized(lock)  {
                     //stuff
              }
          }
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文