1

what i'm trying to do is while loop with delay of 15 seconds and inside the loop i want to execute volley request .. the loop is working but with no delay

this is my code : (client2 is the volley request method )

new Thread(new Runnable() {@Override
    public void run() {
        while (h < taxiidlist.size() && assined == false && requested == true) {
            handler2.post(new Runnable() {@Override
                public void run() {

                    client2(user.id, taxiidlist.get(h));

                    h++;
                }

            });
                try {
                    Thread.sleep(15000);
                } catch(InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }).start();
Neo Algeria
  • 235
  • 2
  • 3
  • 13

1 Answers1

1

I don't see why the code isn't working. One possible issue might be that you have forgotten to invoke run() within the handler.post() where your inner Runnable instance is being passed.

Try with this sample code (loop executed just once) and see if you can spot the issue in yours.

private static List<String> taxiidlist = new ArrayList<>();
static int h = 0;

public static void main(String[] args) {

    int id = 0;
    boolean assined = false;
    boolean requested = true;
    taxiidlist.add("One");

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (h <= taxiidlist.size() && assined == false && requested == true) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        client2(id, taxiidlist.get(h));
                        h++;

                        try {
                            Thread.sleep(1500);
                            System.out.println("slept!");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });

                break;
            }
        }
    }).start();

}

static void post(Runnable runnable) {
    System.out.println("post!");
    runnable.run();

}

static void client2(int id, String s) {
    System.out.println("client2!");
}

Hope this helps :)

Prathap
  • 178
  • 1
  • 1
  • 9
  • thank you , i don't know why but your code is working – Neo Algeria Dec 31 '16 at 20:38
  • The only change I made was to add code to invoke the inner Runnable with the `run()` method. I have kept everything else as close as I could with yours so that it makes it easier for you to track down the issue. Is your `client2()` being called? – Prathap Dec 31 '16 at 21:22