简体   繁体   中英

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

I have a java applet class which implements runnable. In the run() method of this class, I have a call to repaint(). The contents of the run method are contained in an infinite loop. In the init() of the applet class I use new Thread(this).start() to start the second thread. 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. If the former, then will the second thread continue its loop without waiting (if possible) for paint() to be executed?

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 . The short answer basically is, the RepaintManager will schedule repaints to occur on the Event Dispatching Thread.

Is it safe to call repaint directly from a separate thread? 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. Saves a lot of second guessing, IMHO

My understanding is that repaint is generally considered thread-safe. That doesn't mean it will remain so into the future though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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