See, here the word Activity better to replace with MainThread (UI Thread ). That is, you have two streams, one main, the other generated. If you need to transfer data to later before start (the start method), then everything is very simple, you can create your own class and inherit from Thread. Here is a small example
// Somewhere in activity String someText = "Hello World!"; Thread myThread = new CustomThread(someText); public class CustomThread { private passedString; public CustomThread(String inputText) { this.passedString = inputText; } @Override public void run() { System.out.println(this.passedString); //Do some stuff } }
If you want to transfer data to an already running working stream, you can do it in several ways. You can make a boolean variable in the stream class and in the loop, for example, until it is true, do something, as soon as it is true, then read the global static static shared or make a method that sets the variable of the custom stream object when setting the flag will reread the new value. If you need to add, I will show an example.
Or it’s a better way to use a blocking queue .. Here the multithreaded programming pattern Consumer && Producer used. Here is an example.
final BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); Thread myThread = new Thread(new Runnable() { @Override public void run() { while (true) { try { System.out.println(queue.take()); } catch (InterruptedException e) { // Handle Error System.err.println("Error:" + e); } } } }); myThread.start(); queue.offer("Hello World"); }
I honestly don’t know for sure that these are all possible ways, maybe someone else knows.