简体   繁体   English

从Java Applet中的非主线程调用repaint()

[英]Calling repaint() from non-main thread in java applet

I have a java applet class which implements runnable. 我有一个实现可运行的java applet类。 In the run() method of this class, I have a call to repaint(). 在此类的run()方法中,我调用了repaint()。 The contents of the run method are contained in an infinite loop. run方法的内容包含在无限循环中。 In the init() of the applet class I use new Thread(this).start() to start the second thread. 在applet类的init()中,我使用new Thread(this).start()启动第二个线程。 My question is if calling repaint() from the run() method will signal the main thread to execute paint(), or if paint() will be executed as part of the second thread. 我的问题是,是否从run()方法调用repaint()会发出信号通知主线程执行paint(),或者是否将paint()作为第二个线程的一部分执行。 If the former, then will the second thread continue its loop without waiting (if possible) for paint() to be executed? 如果是前者,那么第二个线程是否将继续其循环而不等待(如果可能)等待paint()被执行?

Technically, the Event Dispatching Thread is it's own thread, separate from the (main) thread that launched the applet. 从技术上讲,事件调度线程是它自己的线程,与启动小程序的(主)线程分开。

The repaint process is scheduled by the RepaintManager . 重新绘制过程由RepaintManager调度。 The short answer basically is, the RepaintManager will schedule repaints to occur on the Event Dispatching Thread. 简短的回答基本上是, RepaintManager将安排在事件调度线程上发生的重新绘制。

Is it safe to call repaint directly from a separate thread? 从单独的线程直接调用repaint是否安全? It would argue it's not good practice. 它会说这不是一个好习惯。

As a matter of cause, I tend to err on the side of caution and simply ensure all interactions with the UI and synced by to the EDT regardless. 出于某种原因,我倾向于保持谨慎,仅确保与UI的所有交互并同步到EDT,无论如何。 Saves a lot of second guessing, IMHO 节省大量的第二个猜测,恕我直言

My understanding is that repaint is generally considered thread-safe. 我的理解是, repaint通常被认为是线程安全的。 That doesn't mean it will remain so into the future though. 但这并不意味着它将在未来保持如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM