Hun*_*nsu 5 java optimization multithreading process
在我读过的问题中,我们建议在进程上使用线程,因为线程更快.我决定使用我的程序的线程来编辑维基百科中某个类别的文章.该程序获取要编辑的文章列表,然后在10个线程之间划分文章.通过这个我每分钟进行6-7次编辑,它的速度与我没有使用线程的速度相同.当我启动我的程序的多个实例并为每个实例提供一个要处理的类别时,我看到每个进程可以每分钟进行6-7次编辑(我测试了5个进程).
为什么流程在我的情况下要快得多?为什么线程没有改变什么?
代码(不完整只是为了有一个想法):
public static wiki = new Wiki();
public process(){
String[] articles = wiki.getArticles(category);
for(int i=0; i< 10; i++){
String[] part = getPart(articles, i, 10);
MyThread t = new MyThread(part);
list.add(t);
}
ExecutorService.invokeAll(list); //I'm not sure about the syntax of the function
}
public class MyThread extends Thread {
public String[] articles ;
public MyThread(String[] articles) {
this.articles = articles;
}
public void run() {
//some logic
wiki.edit(...)
}
}
Run Code Online (Sandbox Code Playgroud)
每个进程都有许多线程来完成它的工作.如果你有一个进程有N个线程或N进程有1个线程,它除了几乎没有什么区别.
我看到每个进程每分钟可以进行6-7次编辑
每次编辑需要10秒钟,听起来很长.也许值得使用CPU分析器优化代码以提高性能.
归档时间: |
|
查看次数: |
5514 次 |
最近记录: |