Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从线程获取输出流_Java_Multithreading - Fatal编程技术网

Java 如何从线程获取输出流

Java 如何从线程获取输出流,java,multithreading,Java,Multithreading,我目前有几个可运行的类,每个类在完成时使用System.out.println()打印一个字符串 在main()中,我使用ExecutorService执行它们,executor.execute() 我想知道在执行这些线程之后,如何从它们那里获取输出流以供将来使用? 非常类似于对进程使用.getInputStream,但是在Thread类中没有这样的方法。谢谢 有一个类实现如下可运行接口: public class A implements Runnable { public void r

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

main()
中,我使用ExecutorService执行它们,
executor.execute()

我想知道在执行这些线程之后,如何从它们那里获取输出流以供将来使用?

非常类似于对进程使用
.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,并将其存储到一个整数变量中

您可以使用新的静态类:

public class Global{

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

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

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();
    }
}
类MyCallable实现可调用
{
@凌驾
公共InputStream调用()引发异常{
//InputStream inputStreamObject=为InputStream创建对象
返回inputStreamObject;
}
} 
班长
{
公共静态void main(字符串[]args){
ExecutorService executor=Executors.newFixedThreadPool(5);
列表=新的ArrayList();
对于(int i=0;i<25;i++){
Callable worker=new MyCallable();
未来提交=执行人提交(工人);
列表。添加(提交);
}
InputStream inputStreamObject=null;
用于(未来:列表){
试一试{
inputStreamObject=future.get();
//根据需要使用inputStreamObject
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
executor.shutdown();
}
}

线程中的可运行和可调用:

runnable
接口有一个方法
public abstract void run()无效-这意味着在完成run方法后,它将不会返回任何内容
Callable
接口有一个方法
V call()抛出异常
这意味着在完成调用方法后,它将返回参数化为的对象
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);
        }
    }
}
公共类运行与调用{
公共静态void main(字符串…参数){
CallableTask call=新建CallableTask();
RunnableTask run=新建RunnableTask();
试一试{
FutureTask callTask=新建FutureTask(调用);
线程运行任务=新线程(运行);
callTask.run();
runTask.start();
System.out.println(callTask.get());
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态类CallableTask实现可调用{
公共字符串调用(){
String stringObject=“内部调用方法..!!我正在返回此字符串”;
System.out.println(stringObject);
返回字符串对象;
}
}   
公共静态类RunnableTask实现Runnable{
公开募捐{
String stringObject=“内部运行方法,我不能返回任何东西”;
System.out.println(stringObject);
}
}
}

线程不会写入与父进程相同的输出流,这很自然,因为它们共享内存,它们自然会共享底层文件描述符。我不太明白?每个线程使用System.out.println()输出的输出流不是共享资源,也就是说,您需要一次在一个线程中访问它-我可能错了。@hagubear它是共享的,但您不需要强制独占访问,除非您的消息是用多个命令打印的。@Austin luk32问的是:为什么您需要“获取”它是从线上掉下来的吗?System.out是按原样共享的,无论您从.Thx使用它作为回复的线程或执行器。但是我想知道如何从call()方法中的线程构造inputstream对象?通过获取inputstream对象将实现什么?在问题中你没有恰当地提到它。请更新您的全部要求。。你的问题仍然含糊不清。。根据您的问题,我返回inputstream的对象。我已经更新了描述。如果您能看一下,我将不胜感激:)而不是Callable使用Callable now future.get()将返回String对象。您可以使用字符串进行处理。我的回答是否适合你的问题?对不起,我能问一下关于可调用类的更多细节吗?