This answer assumes that when you start the stream, you do not have an OutputStream , but it will be received later. If you already have a reference to the stream object, you should use the Lawrence example instead.
You can use some wrapper class, for example:
// This is a very simplified example; use getters and setters instead. class OutputStreamWrapper { public OutputStream outputStream; }
Then you can do this:
final OutputStreamWrapper wrapper = new OutputStreamWrapper(); Runnable r1 = new Runnable() { public void run() {
Then you pass the object referenced by the wrapper variable to some other method or class, which in turn set the OutputStream property to pass the stream to the stream code.
Note that this is a kind of hack; implementing Runnable in another class and providing it with such a field would be preferable.
source share