• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Pre-empt lock?

Is there any way to get code working in this way:

        thread.start();
        int result = messageBox.getResult();

the run() method of thread (a thread based on messageBox) is synchronised. getResult is also synchronised.

I want to be able to start the thread, then for main execution to halt until the lock on messageBox is released. However, main execution always gets to getResult before the run() method is called. What am I doing wrong?

This is pretty urgent

Thanks in advance,
_Blue
0
eeBlueShadow
Asked:
eeBlueShadow
  • 15
  • 9
  • 5
2 Solutions
 
CEHJCommented:
thread.start();
thread.join();
0
 
objectsCommented:
After you start the thread call wait().
And from another thread call notify() when the lock is released.
0
 
objectsCommented:
> thread.join();

That waits for the thread to complete, not for the lock to be released.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
> What am I doing wrong?

As far as your synchronisation goes the problem is that the two synch on different objects.
0
 
CEHJCommented:
>>That waits for the thread to complete, not for the lock to be released.

I'm aware of that. It just happens to be a way for eeBlueShadow to get his result at the right time
0
 
objectsCommented:
Be easier to just do the processing from the same thread then.

thread.run();
int result = messageBox.getResult();
0
 
eeBlueShadowAuthor Commented:
OK, still no result yet:

> thread.start();
> thread.join();

Does the job at pausing the main execution, but also seems to lock other things up:

    public synchronized void run(){
        validate();
        requestFocus();
        setVisible(true);
        while (result == 0) {
            try {
                wait(100);
            } catch(InterruptedException e) {}
        }
        notifyAll();
        setVisible(false);
    }

This shows a container for the user to select an option, which then changes the result variable.

However, with thread.join() the container never appears, everything stopsresponding and the run method keeps going around the while...wait() loop

thread.run() does the same.

Could you explain "As far as your synchronisation goes the problem is that the two synch on different objects." I imagine this is related to the "java.lang.IllegalMonitorStateException: current thread not owner" I get when I use wait() in the main execution body?

Thanks again,
_Blue
0
 
CEHJCommented:
If your thread is performaing GUI operations, and it appears to be, it should be run in the context of the event dispatch thread:

EventQueue.invokeAndWait(thread);
0
 
CEHJCommented:
It would help if you told us what you're trying to achieve. See the following:

http://www.catb.org/~esr/faqs/smart-questions.html#goal
0
 
objectsCommented:
> EventQueue.invokeAndWait(thread);

You definitley do not want to do that, in fact by the sounds of you are already making the call from the EDT.
0
 
objectsCommented:
> However, with thread.join() the container never appears

Thats most liokely because you are blocking the EDT. Swing is suingle threaded so if that thread is buisy then the gui cannot get updated.
The SwingWorker class allows you to run your processing in the background, and also includes callbacks to inform the gui when it has completed.
0
 
eeBlueShadowAuthor Commented:
> You definitley do not want to do that, in fact by the sounds of you are already making the call from the EDT.

Apparently so - "java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread"

> It would help if you told us what you're trying to achieve.

OK, I am trying to show a custom message box, which waits for the user to select an option and returns that result to the calling function.

Swing is out, I'm afraid, as I'm writing for an xlet which needs to be compatible with javax.microedition - I'm using the HAVi toolkit.

And no, I don't know if I'm necessarily going about it the right way

_Blue
0
 
objectsCommented:
A modal dialog should achieve that
0
 
CEHJCommented:
All you need is a modal dialog. Are you sure there's none already in j2me?
0
 
eeBlueShadowAuthor Commented:
http://blueshadow.no-ip.com/web/HAVi1.1Specification-Level2-ui.pdf

Table 1 lists the classes HAVi can "reliably interact with". I have to say I'm not sure about j2me itself
0
 
objectsCommented:
0
 
CEHJCommented:
Alert:

" Use  setTimeout(int time) to set the time (in milliseconds) for which the alert is displayed on the screen. If you pass Alert.FOREVER as the value of time, you will show the alert forever and make the alert a modal dialog."

(

from

http://today.java.net/pub/a/today/2005/05/03/midletUI.html

)
0
 
objectsCommented:
How would you use an Alert????
0
 
objectsCommented:
Instead of waiting for the user to enter something, you could also take the approach of performing an action once the user has entered a value.
That way you would not need to wait, and instead you would be notified via a callback.
0
 
CEHJCommented:
>> How would you use an Alert????

You're asking a lot of questions today ;-) Read the link i posted - it tells you how
0
 
objectsCommented:
I had already read the link, it did not seem to be at all relevant. Thats why I asked.
0
 
CEHJCommented:
No - i was forgetting you need input from the user. There's an example here:

http://www.cn-java.com/download/data/book/j2me_stock_example.pdf
0
 
objectsCommented:
Already suggested, though I don't think it'll work in that environment.
0
 
eeBlueShadowAuthor Commented:
The callback solution was easy enough to implement, as suggested at http://www.javaworld.com/javaworld/javatips/jw-javatip10.html

Although it was objects' solution, I don't think you would have got there without bouncing off each other, so 20% split to CEHJ. Hope that's ok

_Blue
0
 
eeBlueShadowAuthor Commented:
And thanks for the involved help at short notice :)
0
 
CEHJCommented:
OK ;-)
0
 
objectsCommented:
I reckon he needs the points considering the number of times he complains about points allocation ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 15
  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now