如何从线程获取输出流

时间:2014-11-19 11:26:19

标签: java multithreading

我目前有几个可运行的类,每个类在完成时使用System.out.println()打印一个字符串。

main()我使用ExecutorService执行它们,executor.execute()为每个执行它们。

我想知道在执行这些线程how to get the output stream from them for future use ?

之后

非常类似于使用.getInputStream进程,但Thread类中没有这样的方法。谢谢!

这是一个实现可运行界面的类:

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

在主要功能中,我需要获取打印的数字并存储

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();
    ......
}

现在我需要获取打印的数字5并将其存储到整数变量中。

3 个答案:

答案 0 :(得分:2)

我认为以下代码将满足您的要求。

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();
    }
}

答案 1 :(得分:0)

你可以使用新的静态类:

public class Global{

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

答案 2 :(得分:0)

线程中的Runnable和Callable:

runnable接口有一个方法public abstract void run(); void - 这意味着在完成run方法后,它不会返回任何内容。 Callable<V>接口有一个方法V call() throws Exception;,这意味着在完成调用方法后,它将返回参数化为

的对象V
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);
        }
    }
}