views:

62

answers:

1

hi

any simple program for single thread with timer in java.

that thread will run with particular time.

i am new to this thread.

thanks in advance

+1  A: 

You'll want a ScheduledExecutorService, which can be obtained from one of the static factory methods on Executors.

Check out:

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/concurrent/Executors.html

http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html

SimonC
hii want to call a one function, the name of function is addition(), this function take some time for execute. some time this is take over time for execute, so i would like to avoid the function execute more time, in that time i would like to end the function execution, the addition function executes in while loop, so i would like to set 10 seconds for each time calling the function. can u understand what is my problem?
zahir hussain
Do you want to call addition repeatedly or just once? Also, can't you just test the current time in addition's loop? while (System.currentTimeMillis() < startTime + (1000 * 10)) { ... }
SimonC
i called repeatedly,but the input of the addition function is different,so what will i do?
zahir hussain
Could you provide a bit more context of what you're trying to achieve? Also, are you able to modify the addition method? I.e. could you put in the check I mentioned above?
SimonC