-1

I am working at an android application in android studio using java.

I am creating a runnable with some parameters. Call thread function is creating a new thread and after creating this new thread I want for the main thread to wait for 4 seconds (without crashing the UI). It looked everywhere and I can't seem to get an answer for this question

    Runnable runnable;
    runnable = new timedVisibility(calibratePointList,0,1,ok);
    callThreadFunction(runnable);
    try {
        Thread.currentThread().wait(4000);
    }
    catch (Exception e)
    {

    }

This is the callThreadFunction:

    Thread t1 = new Thread(runnable);
    t1.start();
    try {
        t1.join();
    }
    catch (Exception e)
    {
        System.out.println("Error caught");
    }
  • 2
    "I want for the main thread to wait" - No you don't. If Android detects this, it will show the "App not responding. Close?" dialog. – StackOverthrow Aug 31 '18 at 20:35
  • It might be worth some basic reading up on threading in Java and Android. I can’t imagine why telling the current thread to wait four seconds in an app is a good idea. As @tkk points out, Android won’t like it... –  Aug 31 '18 at 20:38
  • I don't want it exatly to sleep. I want to make some UI elements appear and dissapear in a interval of 4 seconds – JUSTINIAN-IONUT PETCU Aug 31 '18 at 20:39
  • Your `callThreadFunction` seems useless unless for some reason you require `runnable` to be executed on a separate thread. As far as timing, waiting, blocking, etc., the entire code can be replaced with `runnable.run()`. – Ted Hopp Aug 31 '18 at 20:42
  • 1
    Could you please be more specific on what you'd like to achieve? Blocking the main thread sounds like a bad idea. – Anatolii Aug 31 '18 at 20:46
  • 1
    You can't make the main thread wait withtout freezing the UI, you need to have a callback and call it on the main thread instead – Marcos Vasconcelos Aug 31 '18 at 20:48
  • So not really about threading but about displaying for a time? How about https://stackoverflow.com/questions/3247554/how-to-show-a-view-for-3-seconds-and-then-hide-it ? –  Aug 31 '18 at 20:48

2 Answers2

4

I'm not sure what you want to have happen after four seconds, but in Android you can do it using a Handler:

Handler handler = new Handler();
Runnable followUpAction = new Runnable() {
    @Override public void run() { /* something to do after 4 seconds */ }
};
handler.postDelayed(runnable, 4000);

You don't want to have your UI thread waiting for a background thread to finish. If you want to take some action as soon as the background thread does finish, you should use a call-back scheme (passing a Runnable or some other interface with a method to be executed when the background thread finishes).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • @ʍѳђઽ૯ท - So I see. :) – Ted Hopp Aug 31 '18 at 20:48
  • I think this is basically what I was looking for. Though I didn't managed to make it work as intended yet. thank you – JUSTINIAN-IONUT PETCU Aug 31 '18 at 21:06
  • 1
    Don't forget to call `handler.removeCallbacks(followupAction)` in one of the activity teardown methods. Otherwise the `Runnable` leaks the activity and may cause a crash if it runs after `onDestroy`. – StackOverthrow Aug 31 '18 at 21:09
  • But what if I want to call this handler 2 times. I want to give runnable a new value and to call handler again but it doesn't delay anymore. – JUSTINIAN-IONUT PETCU Aug 31 '18 at 21:14
  • EDIT: I found out, the delayed is calculated from delta0 at compilation. so if you want the handler to wait another 4 seconds you have to do handler.postDelayed(runnable,8000); – JUSTINIAN-IONUT PETCU Aug 31 '18 at 21:22
  • @JUSTINIAN-IONUTPETCU - Depending on the logic involved, you may also be able to redesign your `Runnable` so that the last thing it does is reschedule itself to run again 4 seconds later. As with the issue that TKK points out regarding activity teardown, you will need to remove the callback to prevent an infinite loop that will leak your activity. – Ted Hopp Aug 31 '18 at 21:54
-1

If you don't want to use sleep() because of UI behaviors, try using Handler:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //Write your code here
    }
}, 4000); //Timer is in ms here.

Anything you write in the middle, it will wait for 4 seconds or 4000ms. Anyways, it is not recommended to wait the current Thread for a while.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108