java多线程(newCachedThreadPool),然后将结果写入一个文件?
我尝试使用简单的线程来做到这一点并成功了,但我相信使用线程池我可以更有效地完成同样的事情:)? 简单线程:
public static class getLogFile implements Runnable {
private String file;
public void setFilename(String namefile){
file=namefile;
}
public int run1(String Filenamet) {
connectToServer(XXX, Filenamet, XXX, XXX, XXX, XXX);//creates a file and downloads it
return 0;
}
public void run() {
run1(file);
}
}
in main:
for(x=0 ; x < 36 ; x++){
String Filename1=Filename+x;
getLogFile n=new getLogFile();
n.setFilename(Filename1);
(new Thread(n)).start();
}
程序连接到服务器同时执行 36 个命令(使用线程池/简单线程?!),然后下载 36 个结果文件,然后合并它们,或者可能只写入服务器上的一个文件,然后下载它?
- 如何将这段代码转换为线程池?
- 如何从36个线程向一个文件写入数据?
I have tried to do this with simple Threads and succeded but I believe that using Threadpool I could do the same thing more effeciently:)?
simple threads:
public static class getLogFile implements Runnable {
private String file;
public void setFilename(String namefile){
file=namefile;
}
public int run1(String Filenamet) {
connectToServer(XXX, Filenamet, XXX, XXX, XXX, XXX);//creates a file and downloads it
return 0;
}
public void run() {
run1(file);
}
}
in main:
for(x=0 ; x < 36 ; x++){
String Filename1=Filename+x;
getLogFile n=new getLogFile();
n.setFilename(Filename1);
(new Thread(n)).start();
}
Program connects to the server executes 36 commands(using threadpool/simplethreads?!) at the same time and either downloads 36 result files, than merges them, or maybe it could just write to one file on server and then download it?
- how to transform this code into threadpools?
- how to write data to one file from 36 threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只能给你指路。
为了使用线程池,先看看ServiceExecutor是如何工作的。谷歌的任何例子都会给你足够的信息。举个例子看看:
http://www.deitel.com/articles/java_tutorials/20051126/JavaMultithreading_Tutorial_Part4.html
关于将 36 个线程写入其自己的文件,或写入一个文件。我不能说关于多个线程写入同一个文件的任何事情,但您可以使用 CyclicBarrier 来等待所有线程完成写入时的事件。您可以在这里找到其使用示例:
http://download.oracle.com /javase/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html
I can only offer you directions.
In order to use thread pool, look how ServiceExecutor works. Any example from Google will give you enough information. As an example look at:
http://www.deitel.com/articles/java_tutorials/20051126/JavaMultithreading_Tutorial_Part4.html
Concerning writing 36 threads to its own file, or writing into the one file. I cannot say anything about writing by several threads into the same file, but you may use CyclicBarrier to wait event when all threads will finish writing. Example of its using you may find here:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html
目前尚不清楚你想做什么。我的想法是,创建 36 个与服务器的独立连接将是一个相当大的负载,如果没有,它也可以完成。
服务器可以自行组装这 36 个文件并自行处理线程吗?这似乎是更合乎逻辑的职责划分。服务器将了解这项工作的并行性,并且对服务多个连接的服务器有很大的影响(包括可能阻止其他客户端)。
It's not clear what you want to do. My thoughts are that creating 36 separate connections to the server will be a sizable load that it could well do without.
Can the server assemble these 36 files itself and look after the threading itself ? That seems a more logical partition of duties. The server would have knowledge of how parallelisable this work is, and there's a substantial impact on the server of servicing multiple connections (including potentially blocking out other clients).
一个简单的方法是使用java任务执行器来做到这一点,如下:
你也可以使用spring任务执行器,它会更容易。不过,我也建议使用单个连接,如上所述。
A simple way to do it using java task executors, as follows:
You can also use spring task executors, it will be easier. However, I will also suggest using a single connection, as mentioned above.