Is it safe to run over 1,000 threads in java?

I am making a program that tries to get all possible results. The threads in the program generate more threads (just over a thousand). I am very bad at multithreading, afraid that thread generation will not stop. I am using eclipse IDE which has an end button, will it stop the entire current thread if there is no other way? Can the JVM handle this?

+3


source to share


2 answers


Yes. Pressing the end button in an eclipse-launched JVM will stop that JVM, and this will stop all current threads (like killing the JVM process).



As far as running thousands of threads, I would not recommend ... it sounds like a very slow approach (since each thread can run at most ~ n / 1000th times on n

CPU cores).

+6


source


There is really no limit to opening 1000 or more threads in the Java programming language. However, the problem is slowing down.

Do you know how a thread works? Thread simply creates an environment with the OS so that the user of the application can feel like programs are running in parallel. But the real scenario is different. A computer with a single core processor can process one operation at a time. Our OS just sends operations one by one, so we can feel like they are running in parallel.

For example, consider a three-threaded application. Each of its threads has a for loop. The first thread adds numbers inside the loop and stores the result in a variable named result1, the second thread multiplies the numbers inside the loop and stores the result in a variable named result2, and the third thread subtracts the numbers inside the loop and stores the result in a variable named result3.

Now, if all these threads start at the same point in time, and let all have the same priority, the OS will send commands one by one. If you can post the number to add with the result1. The next moment he can send the number multiplied by result2. In the next instant, he can send a number to subtract from result3. The next moment he can add again.



This means that a single-core processor cannot actually compute three computations at the same time. It computes one computation and pauses the rest and goes through that way.

I think you now understand why starting 1000 threads will slow down the whole process. If there is no performance in the specified task and you only need output, you can run 1000+ threads.

But, if you want improved performance, you need to think about something else. Are you finding it difficult to reduce the map ? Map injection with Hadoop can improve performance for these types of problems. However, you first need to work out your problem on a downsize map. And this structure will compute your task in parallel using multiple computers.

Another solution might be to set a priority. In java, you can set priorities on a thread. You can prioritize critical tasks higher than simpler tasks. If your problematic tasks can be distinguished by their high and low priority tasks, this will improve performance.

+1


source







All Articles