0

I have created 2 threads. I need to submit a runnable from one thread to the other and execute it in that other thread. Is it possible?

Edit: actually I need to use main thread instead of just another one. So it is impossible to use ExecutorService.

Edit: there is the solution to this problem: Running code on the main thread from a secondary thread?.

I've posted below my example:

public class SomeClass {

    private static Thread thread;
    private static Thread another_thread;

    public static void main(String[] args) {

        thread = new Thread(() -> {
            //do something
            Runnable runnable = () -> {
              //do something
            };
            //submit runnable to another_thread
            //do something else while the Runnable runnable is being executed in another_thread
        });

        another_thread = new Thread(() -> {
            //do something
        });

        another_thread.start();
        thread.start();
    }
}
Skriep
  • 16
  • 2
  • 1
    What are you trying to do exactly? Why are you creating `Thread`s directly rather than using `ExecutorService`s? – daniu Jul 02 '19 at 12:45
  • why are you trying to change it to another thread? also the .run() method on a thread doesn't actually start a thread, it just runs the runnable form your contructor in the current thread – Joris D R. Jul 02 '19 at 13:00
  • @daniu, actually I need to execute Runnable in the main program thread, so it is impossible to use ExecutorService. – Skriep Jul 02 '19 at 13:01
  • So you want to post tasks from another thread back to the main thread? – JimmyB Jul 02 '19 at 13:03
  • @JorisDR.I agree with you. My bad, the code is just as an example to show what I am trying to do. I didn't even run it. The real situation is much more difficult. – Skriep Jul 02 '19 at 13:05
  • @JimmyB Exactly. – Skriep Jul 02 '19 at 13:05
  • Maybe what you need is a [`ForkJoinPool`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html). Otherwise, the general approach also implemented in the `Executor`s is to have a *queue* of e.g. `Runnable`s to which tasks are added by different threads and from wich tasks are taken and executed by the "worker" thread when it has time to do so. – JimmyB Jul 02 '19 at 13:06
  • This (executing tasks from different threads in the main thread) is a common requirement in Java-based GUIs, including Android. For these cases, the GUI frameworks provide the appropriate means, see e.g. https://stackoverflow.com/questions/7229284/refreshing-gui-by-another-thread-in-java-swing or https://developer.android.com/reference/android/os/Handler. – JimmyB Jul 02 '19 at 13:10
  • @JimmyB My project is not for android. I don't think that's what I am looking for. – Skriep Jul 02 '19 at 13:22
  • And I don't think you give us enough information to answer your question, not even the use case. – JimmyB Jul 02 '19 at 14:14

1 Answers1

1

Here's what you usually want to do:

class YourClass {
    public static void main(String[] args) {
        ExecutorService executor1 = Executors.newSingleThreadExecutor();
        ExecutorService executor2 = Executors.newSingleThreadExecutor();

        Runnable run1 = () -> {
            Runnable run2 = createRunnable();
            // submit in second thread
            executor2.submit(run2);
        }

        // submit in first thread
        executor1.submit(run1);
    }
daniu
  • 14,137
  • 4
  • 32
  • 53
  • I need to execute Runnable in the main program thread, so it is impossible to use ExecutorService – Skriep Jul 02 '19 at 13:03
  • I don't think you really know what you want to do. Running a Runnable in a main thread wouldn't require a runnable at all. You'd just run the code as-is. – Christopher Schneider Jul 02 '19 at 13:37
  • @ChristopherSchneider, the thing is, I need to run a Runnable in a main thread from another thread, while main thread is busy doing something else. I don't have direct access to the main thread, so I think I do need a Runnable. Don't you agree? [That's what I needed](https://stackoverflow.com/questions/16268693/running-code-on-the-main-thread-from-a-secondary-thread). Do you know a better solution than [this one](https://stackoverflow.com/a/16268778/11729219)? – Skriep Feb 02 '21 at 21:10