?
Solved

AWT thread scheduling?

Posted on 1999-12-06
7
Medium Priority
?
197 Views
Last Modified: 2010-04-16
I've had this problem several times now and I wonder what can be done about it. The general pattern of it is:

f = new Frame();
...
f.setVisible();
computesomethingtorenderinframe();
// only here the frame is refreshed

in the mean time the window sits there unrefreshed. It seems like the awt thread only wakes up (and calls paint() on its components) when all other threads have stopped computing? How do I get around this? I tried lowering the priority, yielding and sleeping, or manually calling repaint() but nothing helps. I'd like to understand how the AWT thread works in terms of scheduling and how to force it to wake up.
0
Comment
Question by:Aardappel
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 600 total points
ID: 2259415
Well, mister potatoe,

I ran into a similar situation, and resolved it. It seems like repainting should be able to go on in "parallel" to the subsequent computing, but the JVM proffers few guarantees on the timing of such things. I have a program that must load a bunch of data over the network, and I wished to show its progress as it was loading, but before it was fully operational. The mechanism I used is to call the show method for the frame, then return.
In the paint method I check whether the display has been initialized yet. If not, I paint the loading display and post an event which will be caught elsewhere to start the actual loading. The loading calls a method which changes a page number for the display and when it is finished the functioning display is painted.
So there is a three state variable in paint: uninitialized, loading, and regular. During loading, whenever the page number needs changing a method is called that changes the page number variable and issues a repaint request. When loading is done, the state variable is changed, and another repaint issued.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2259764
just a note: I hope that you don't call this code
computesomethingtorenderinframe();

from some eventhandler method :) (or some paint() method) - this way you effectively block the AWT Thread.
0
 

Author Comment

by:Aardappel
ID: 2261320
Like heyhey notes, moving it to paint is not going to solve the problem. It seems there is not any one spot where I can spend time computing without AWT blocking, not even in a seperate thread: AWT only kicks in when the rest of the system is idle.

Even worse, it is not always possible to encapsulate "whatever happens after .show()" such that it can be conveniently called from paint(), in my case opening the window is called from some place which then afterwards does some computation, and spread out over multiple threads as well. I can't somehow freeze the state of computation of all these threads and functions and then wake em up again from paint().

I'll give you some points anyway for your answer but sadly it doesn't solve it for me.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 16

Expert Comment

by:heyhey_
ID: 2261392
if you have a good designb (based on message notification, not direct method calling) it should be possible to make exaustive computations without blocking the AWT threads (all computations in separate Threads).

btw. I've never experienced situations where 'AWT only kicks in when the rest of the system is idle.' - what's your OS / JDK version ?

we can't help without knowning more details (even better - sample code) about your design.
0
 

Author Comment

by:Aardappel
ID: 2261451
windows95 r1, jbuilder 2/1.1.6, 1.2.x all give the same results.

providing code is going to be difficult, as the code that opens the Frame is being called from compiled code, i.e. I generate bytecodes from a totally different language and then run it using a classloader. Ican only give the general structure of it which I did above.

Thanks for the help though, I will try some stuff by launching even more threads, see if that helps.
0
 
LVL 16

Expert Comment

by:imladris
ID: 2262249
Note that I proposed that you POST an event from the paint method, not call something. Yes, as long as you have the system thread (the main thread of control) tied up, AWT threads will often be sluggish. My structure also returns, after the show, and then doesn't start the loading (which would be computation in your case), until after the paint is done. To get a responsive display in GUI systems in general you have to keep giving control back to the system (or processing messages). Rather than trying to run more threads, I would consider whether the computation can be broken into pieces.
0
 

Author Comment

by:Aardappel
ID: 2262340
Yeah thanks that's a good trick to use. But like I said above, you aren't always able to structure a program at will, i.e. have it continue from somewhere else. In my case I have no choice.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month4 days, 16 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question