I'm working on a project where I need to pass data asynchronously between threads in Java and cannot find a decent solution. This needs to happen live because the sub thread can be going on forever.
Below is some sample code which illustrate what I'm trying to do. package test.project;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class TestProject {
public static void main(String[] args) {
Thread thread = new Thread(new Generator());
thread.start();
}
}
class Generator implements Runnable {
@Override
public void run() {
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
Random r = new Random();
// I need to get this back to the main thread to update the UI
System.out.println(r.nextInt(50000));
}
}, 200, 200);
}
}
The code above is just meant for illustration purpose. The actual problem I'm trying to solve is having a couple data streams in background threads and once data comes in I need to pass it back to the main threads to update the UI.
This is JavaFX project not Android.