Processes and threads in Java

In the questions I read, we suggest using threads over processes, because threads are faster. I decided to use themes for my program that edit articles in categories on Wikipedia. The program receives a list of articles for editing, and then splits the articles between 10 threads. In doing so, I make 6-7 changes per minute, and this is the same speed as if I were not using threads. When I run several instances of my program and give a category for processing for each instance, I see that each process can perform 6-7 changes per minute (I tested this with 5 processes).

Why are processes much faster in my case? and why didn't the threads change anything?

Code (not complete to have an idea):

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(...) } } 
+5
source share
2 answers

Each process has multiple threads to make it work. If you have one process with N threads or N process with 1 thread, this makes a little difference except.

  • threads are lighter and have slightly lower overhead. The difference they make is milliseconds, so you are unlikely to win here.
  • using more processes indirectly allows your program to use more memory (since each process has a limited heap size, you can change). If you are going to have N processes, a fair comparison is to limit the memory of each process to 1 / Nth amount of memory.
  • what is more likely to be that you are a bottleneck on a shared resource such as a lock. This means that additional threads add little or no value, because your program cannot use them effectively. Using multiple processes, you break the connection between threads.

I see that each process can perform 6-7 changes per minute

Each edit for 10 seconds sounds pretty long. You might want to optimize your code with a processor profiler to improve performance.

+6
source

First of all, you are using threads incorrectly. Themes are Runnables, so you can send them to the executor, but they will not execute as threads. The executor will run the run () methods on its own threads. The amount of parallel execution of your code above depends on the Contractor you are using.

Secondly, 6-7 edits the second for the stream of sounds suspecting, I would like to believe that more is possible. Perhaps you are facing a bottleneck on a shared resource, as @PeterLawrey offers or can use I / O lock (or the library you use with IO lock), in which case you can increase your throughput by increasing the number of threads. It is hard to say what kind of bottleneck you are facing without any profiling data.

+1
source

Source: https://habr.com/ru/post/1200481/


All Articles