mob_web_dev
mob_web_dev

Reputation: 2362

How to give time interval for 'for loop' in java android

I know that the question is simple,but i'm confused.

I have a list of array.I want to iterate it with a for loop.On each excecution for doing a particular purpose inside the loop i want to *wait the loop for 10 sec* for the next excecution.

Please Help Me

Upvotes: 1

Views: 2207

Answers (5)

SpringLearner
SpringLearner

Reputation: 13844

This is simple example

String arr[]=new String[]{"a","b","c"};
    for(int i=0;i<arr.length;i++)
    {
try{
        Thread.sleep(10000);
        System.out.println(arr[i]);}
catch(Exception e)
{}
    }

Upvotes: 0

Tim B
Tim B

Reputation: 41188

Thread.sleep() will do what you want but is not recommended for any serious programming tasks.

Timer is much better than Thread.sleep() but is still not the best solution as it uses one Thread per Timer and Timer has a lot of limitations.

You are much better using a ScheduledThreadPoolExecutor.

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

Upvotes: 0

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

You can use timer class of android to perform task at regular interval

new Timer().scheduleAtFixedRate(task, after, interval);

Details

new Timer().scheduleAtFixedRate(new TimerTask() 
{
   @Override
   public void run() {
         method(); // call your method
   }
}, 0, 100000);

Upvotes: 3

shreyansh jogi
shreyansh jogi

Reputation: 2102

    for(iterate array){
    try{
         Thread.sleep(10000);
    catch(InterruptedException ie){
   }
}

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Use Thread.sleep(10000);

for(String s: stringArray){
   try{
         Thread.sleep(10000);
   catch(InterruptedException ie){
   }
}

Upvotes: 1

Related Questions