There is a task: after executing a certain method in the main thread, start waiting for 10 seconds, and then start the second method, again in the main thread. In this case, the wait should take place not in the main thread (at least, not block it).

The only option that I came up with: at the end of the first method to start the second thread, which after 10 seconds wait - wait(10000) , will call the method in the main thread. But here is the nuance: how to call the method in the main thread from ... not the main one?)

Yes, and I am very much tormented by doubts: is there a standard wait function in Java that I’m waiting to wait for a while, and after that time, it just calls the listener?

How can you accomplish the task (can you somehow run the method in the main thread from the second or does Java have a wait function)?

  • Here is a guide to timers, developer.alexanderklimov.ru/android/java/timer.php can help you. But the timer, as I understand it, still creates a separate stream - jessez
  • What do you want to achieve and what do you use? In general, it is impossible to just take and run a method in an existing stream. You can do as in android - initially run in the event loop ( Looper in android), and already in it from the queue to take commands and execute. - zRrr

2 answers 2

Method using RxJava :

 Observable.just(true).delay(10, TimeUnit.SECONDS).subscribe(new Action1<Boolean>() { @Override public void call(Boolean aBoolean) { callMethod(); //Вызовется через 10 секунд } }); 
  • cannot resolve metod subscribe(void) ... - user189127
  • @ bukashka101 corrected - rjhdby
  • It still gives an error. I will show my way, longer, but working. - user189127
  • @ bukashka101 my fault, I forgot to clarify that it is with lambdas. Either 8 Java, or with retrolambda - rjhdby

Sketched this option:

 import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TestApp extends AbstractTest { public static void main(String[] args) throws Exception { first(); second_delayed(); } public static void first() { format("First method%n"); } public static void second() { format("Second method%n"); } public static void second_delayed() { format("Delayed method start%n"); ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.schedule(new Runnable() { @Override public void run() { TestApp.second(); } }, 10, TimeUnit.SECONDS); format("Delayed method end%n"); } } 

After calling second_delayed() control returns immediately to the main method and the main thread can do something else useful. After 10 seconds, the second() method is called in a separate thread.

The implementation is based on static methods, but it will be easy to rewrite through non-static methods, if needed. No additional libraries are used.

  • Yes, the problem is that you need a separate thread ... - user189127
  • What does "In a separate thread" mean? Need the second method to run in the main thread? Or in some third method? Can you explain why this is necessary? - slava
  • Oh, did not write. It is necessary that both methods work in the same thread. And it is necessary then that the method from the library simply does not work in the second (I'm writing on Android, if that). - user189127
  • “I'm writing on Android” is a very small detail. One could immediately indicate more details about his task. - slava