aspose file tools*
The moose likes Java Micro Edition and the fly likes Wait - Hold function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Wait - Hold function" Watch "Wait - Hold function" New topic
Author

Wait - Hold function

Gabriel Collet
Greenhorn

Joined: Feb 15, 2005
Posts: 7
Hi.
I need to have my program wait for a couple of seconds before doing the next instruction or wait for an event to happen. How can I do it?

Thanks a lot for your help
Andy Robinson
Greenhorn

Joined: Feb 14, 2005
Posts: 10
j2me does have a Timer function that could be of use to you.
Check out java.util.Timer and .TimerTask, these let you create a Timer and schedule tasks to happen after a certain number of milliseconds.
vishwas bhatt
Ranch Hand

Joined: Nov 30, 2000
Posts: 129
Hi Andy,

can we do this without using timer.I mean we have two thread one is a main thread and another one which handels enents. can we do wait()/notify() between these two thread. if yes please tell me how?

Thanks
Waiting for reply.
Andy Robinson
Greenhorn

Joined: Feb 14, 2005
Posts: 10
Well I don't think j2me is any different from normal java when it come to threads so you should be able to use wait() and notify().
So if you want to make the main thread wait while the other thread does something your code would go something like this -
Main thread
try{
wait();
}catch (InterruptedException e) {}
}

Second thread
//some action code
notify();
or, if you have more than one thread waiting
notifyAll();

Or you could use the System.currentTimeMillis() method to time exactly when events happen, although this harder to do.
Hope that helps.
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
You can share a state variable between the two threads. Thread A writes to that variable when its state changes. Thread B checks the variable value in a loop and perform some action once it detects a change


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wait - Hold function