栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Java处理HTTP呼叫的大文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Java处理HTTP呼叫的大文件

我想并行化调用,但是不确定是否应该将整个文件读入内存

您应该使用

ExecutorService
与bounded的
BlockingQueue
。在读入百万行时,您将作业提交到线程池,直到作业
BlockingQueue
满为止。这样,您将能够同时运行100个(或最佳数量)的HTTP请求,而无需事先读取文件的所有行。

RejectedExecutionHandler
如果队列已满,您将需要设置一个阻止的对象。这比调用方运行处理程序更好。

BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(100);// NOTE: you want the min and max thread numbers here to be the same valueThreadPoolExecutor threadPool =    new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, queue);// we need our RejectedExecutionHandler to block if the queue is fullthreadPool.setRejectedExecutionHandler(new RejectedExecutionHandler() {       @Override       public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {try {     // this will block the producer until there's room in the queue     executor.getQueue().put(r);} catch (InterruptedException e) {     throw new RejectedExecutionException(        "Unexpected InterruptedException", e);}    }});// now read in the urlswhile ((String url = urlReader.readLine()) != null) {    // submit them to the thread-pool.  this may block.    threadPool.submit(new DownloadUrlRunnable(url));}// after we submit we have to shutdown the poolthreadPool.shutdown();// wait for them to completethreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);...private class DownloadUrlRunnable implements Runnable {    private final String url;    public DownloadUrlRunnable(String url) {       this.url = url;    }    public void run() {       // download the URL    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/494820.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号