File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Wait - Hold function" Watch "Wait - Hold function" New topic

Wait - Hold function

Gabriel Collet

Joined: Feb 15, 2005
Posts: 7
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

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?

Waiting for reply.
Andy Robinson

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
}catch (InterruptedException e) {}

Second thread
//some action code
or, if you have more than one thread waiting

Or you could use the System.currentTimeMillis() method to time exactly when events happen, although this harder to do.
Hope that helps.
Michael Yuan
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:
I agree. Here's the link:
subject: Wait - Hold function
It's not a secret anymore!