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();
}
}