java - 如何同时从 URL 列表中获取 InputStream?

标签 java multithreading api url concurrency

目前,我正在尝试返回一个输入流,该输入流由一系列有序输入流组成,每个输入流都是通过 API 从自己的 url 生成的。问题是,按顺序完成时,API 需要几秒钟才能生成数据。因此,API 在连接过程中会延迟几秒钟。 api的结构如下:

www.api.com?q=ENCODEDTEXT

我目前有此代码可以按顺序添加它们。

public InputStream getMP3Data(List<String> synthText){
    InputStream complete = getMP3Data(synthText.remove(0));
    for(String part: synthText){
        complete = new java.io.SequenceInputStream(complete, getMP3Data(part));//Concatenate with new MP3 Data
    }
    return complete;
}

getMP3Data(String) 是我用来通过自动编码 URL 并获取输入流来访问 URL 的方法。 getMP3Data(List) 的目的是对整个数据列表执行相同的操作。我遇到的问题是如何通过多线程来实现这一点。我想同时为列表中的每个项目调用 getMP3Data(String) 。我将如何实现这个目标?我事先不知道 list 的大小。任何帮助将不胜感激。

编辑:这就是我最终使用的。上述代码的执行速度提高了超过900%

    /**
 * Gets an InputStream to MP3Data for the returned information from a request
 * @param synthText List of Strings you want to be synthesized into MP3 data
 * @return Returns an input stream of all the MP3 data that is returned from Google
 * @throws IOException Throws exception if it cannot complete the request
 */
public InputStream getMP3Data(List<String> synthText) throws IOException{
    //Uses an executor service pool for concurrency
    ExecutorService pool = Executors.newFixedThreadPool(synthText.size());
    //Stores the Future (Data that will be returned in the future)
    Set<Future<InputStream>> set = new LinkedHashSet<Future<InputStream>>();
    //Iterates through the list
    for(String part: synthText){
        Callable<InputStream> callable = new MP3DataFetcher(part);//Creates Callable
        Future<InputStream> future = pool.submit(callable);//Runs the Callable
        set.add(future);//Adds the response that will be returned to a set.
    }
    List<InputStream> inputStreams = new ArrayList<InputStream>(set.size());
    for(Future<InputStream> future: set){
        inputStreams.add(future.get());//Gets the response that will be returned, returned.
    }
    return new SequenceInputStream(Collections.enumeration(inputStreams));//Sequences the stream
}


     /**
 * This class is a callable.
 * A callable is like a runnable except that it can return data and throw exceptions.
 * Useful when using futures. 
 * @author Skylion
 *
 */
private class MP3DataFetcher implements Callable<InputStream>{
    private String synthText;

    public MP3DataFetcher(String synthText){
        this.synthText = synthText;
    }

    public InputStream call() throws Exception{
        return getMP3Data(synthText);
    }
}

最佳答案

public InputStream getMP3Data(List<String> synthText){
  List<GetMP3Stream> threads=new ArrayList<GetMP3Stream>();
  // start a thread for each MP3 source
  for(String part: synthText){
    Thread thr=new GetMP3Stream(part);
    thr.start();
    threads.add(thr);
  }
  // collect opened input streams in a list
  List<InputStream> streams=new ArrayList<InputStream>();
  for(GetMP3Stream thr: threads){
    thr.join();
    streams.add(thr.res);
  }
  //Concatenate all input streams 
  return  new java.io.SequenceInputStream(streams);
}

class GetMP3Stream extends Thread {
 String url;
 InputStream res;
 GetMP3Stream (String url) {
   this.url=url;
 }
 public void run() {
   res=getMP3Data(url);
 }
}

关于java - 如何同时从 URL 列表中获取 InputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593336/

相关文章:

android - Activity 中的线程是否在启动另一个时终止?

c++ - vsnprintf 返回超过给定缓冲区大小的大小

javascript - 如何通过手机、Outlook 电子邮件或城市获取用户的本地时间(时区)?

java - JPassword 字段和用户名字段(登录系统)未显示

java - 带有打包依赖项的 Maven 构建 jar

java - Java中ExecutorService关闭时显示友好消息?

c++ - 如何检测当前的屏幕分辨率?

Java 多个关键字搜索

java - 现在 Handler() 已被弃用,我该使用什么?

vb.net 调用问题