Austin
Austin

Reputation: 21

How to get the output stream from a thread

I currently have several runnable classes, each printing a string upon completion using System.out.println().

In the main() I execute them using a ExecutorService ,executor.execute() for each of them.

I am wondering after executing those threads, how to get the output stream from them for future use ?

Pretty much like using .getInputStream for processes but there's no such method in the Thread class. Thanks!

There's a class which implements runnable interface like this:

public class A implements Runnable {
   public void run() {
       System.out.println(5);         //this thread always print out number 5
   }
}

and in the main function I need to get the printed number and store it

public static void main(String[] args) {

    ExecutorService ThreadPool = Executors.newFixedThreadPool(1);
    ThreadPool.execute(new A());     //This statement will cause the thread object A 
                                     //to print out number 5 on the screen
    ThreadPool.shutdown();
    ......
}

Now I need to get the printed number 5 and store it into, say an integer variable.

Upvotes: 2

Views: 1863

Answers (3)

Jaya Ananthram
Jaya Ananthram

Reputation: 3463

Runnable and Callable in thread:

runnable interface has a method public abstract void run(); void - which means after completing run method, it will not return anything. Callable<V> interface has a method V call() throws Exception; which means after completing call method, it will return Object V that is parametrized as

public class Run_Vs_Call {
    public static void main(String...args){
        CallableTask call = new CallableTask();
        RunnableTask run = new RunnableTask();
        try{
            FutureTask<String> callTask = new FutureTask<String>(call);
            Thread runTask = new Thread(run);
            callTask.run();
            runTask.start();
            System.out.println(callTask.get());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static class CallableTask implements Callable<String>{
        public String call( ){
            String stringObject = "Inside call method..!! I am returning this string";
            System.out.println(stringObject);
            return stringObject;
        }
    }   
    public static class RunnableTask implements Runnable{
        public void run(){
            String stringObject = "Inside Run Method, I can not return any thing";
            System.out.println(stringObject);
        }
    }
}

Upvotes: 1

Jaya Ananthram
Jaya Ananthram

Reputation: 3463

I think below code will satisfy your requirement.

class MyCallable implements Callable<InputStream>
{
    @Override
    public InputStream call() throws Exception {
        //InputStream inputStreamObject = create object for InputStream
        return inputStreamObject;
    }
} 
class Main
{
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        List<Future<InputStream>> list = new ArrayList<Future<InputStream>>();
        for (int i = 0; i < 25; i++) {
            Callable<InputStream> worker = new MyCallable();
            Future<InputStream> submit = executor.submit(worker);
            list.add(submit);
        }
        InputStream inputStreamObject = null;
        for (Future<InputStream> future : list) {
            try {
                inputStreamObject = future.get();
                //use inputStreamObject as your needs
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
    }
}

Upvotes: 2

Mauro Midolo
Mauro Midolo

Reputation: 1958

you can use new static class:

public class Global{

//example
 public static ..
 public static ..
} 

Upvotes: -1

Related Questions