Valhalla Legends Forums Archive | Java Programming | java && threads

AuthorMessageTime
Arta
I need to modify a java application for a Uni assignment. Part of the task is to make a 'display' form, which lists the status of orders in a shopping system. The assignment just says this can be a thread that updates the form every 5 seconds, but I think that sucks. I'd like to do it properly. If this were C++ & Windows, I'd create an event, wait on it, and signal it in the order class when an order's status changed. I'm sure something similar must be possible in java, but I don't know how.

I've tried this kind of thing...

[code]
class X
{
public void run()
{
wait();
// do stuff
}
}

class Y
{
public synchronized void something()
{
// Update the order
notifyAll();
}
}
[/code]

...but it doesn't seem to work. I've read somewhere about 'monitors' and thought perhaps that perhaps both classes need to derive from something else which keeps track of the event, or something like that. Could someone point me in the right direction?

nb: I can't drastically change the design of this, as the assignment is to modify the program, not to rewrite it (if wishing made it so...) :)
February 27, 2004, 5:50 PM
St0rm.iD
I'm having trouble understanding what you're saying, but here's my best shot.

Having a thread update the form every five seconds is whats known as "pull from above", that is, the upper layer(s) (the GUI) constantly queries the lower level(s) (the subsystem/logic). Generally, this system is the easiest to implement, but is not ideal, especially from a performance standpoint.

The preferred method is "push from below". Generally, you pass a callback or a listener the lower layer(s), and it will execute that callback when required. In Java, you would create an interface, have the subsystem accept that interface, and have the form implement it. You can also take a look at the Observable class, although I prefer to do it myself the OOP way.

Hope this all makes sense.
February 27, 2004, 7:59 PM
Arta
It makes a great deal more sense to do it that way, I agree, but the assignment specifically states that we should use a thread - I don't want it to look like I've done that as a workaround because I couldn't figure out 'extends Thread' :)

What I'm saying is that I'd like my thread to enter a wait state as soon as it's updated the GUI. Then, I'd like the Order class to wake it up when something changes, so that it can update the GUI once again, after which it should reenter a wait state. And so on.

Hope that's clearer.
February 27, 2004, 11:25 PM

Search