java - How to get the output stream from a thread -
i have several runnable classes, each printing string upon completion using system.out.println()
.
in main()
execute them using executorservice ,executor.execute()
each of them.
i wondering after executing threads, how output stream them future use ?
pretty using .getinputstream
processes there's no such method in thread class. thanks!
there's class implements runnable interface this:
public class implements runnable { public void run() { system.out.println(5); //this thread print out number 5 } }
and in main function need printed number , store it
public static void main(string[] args) { executorservice threadpool = executors.newfixedthreadpool(1); threadpool.execute(new a()); //this statement cause thread object //to print out number 5 on screen threadpool.shutdown(); ...... }
now need printed number 5 , store into, integer variable.
i think below code satisfy requirement.
class mycallable implements callable<inputstream> { @override public inputstream call() throws exception { //inputstream inputstreamobject = create object inputstream return inputstreamobject; } } class main { public static void main(string[] args) { executorservice executor = executors.newfixedthreadpool(5); list<future<inputstream>> list = new arraylist<future<inputstream>>(); (int = 0; < 25; i++) { callable<inputstream> worker = new mycallable(); future<inputstream> submit = executor.submit(worker); list.add(submit); } inputstream inputstreamobject = null; (future<inputstream> future : list) { try { inputstreamobject = future.get(); //use inputstreamobject needs } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } executor.shutdown(); } }
Comments
Post a Comment