2

I have a java application and it starts 2035 threads during the application startup and in which around 1250 threads will wait for 3 seconds until the socket time to out happen other 785 threads establishes the socket connections and completes their jobs quickly.

I can see the CPU usage is not even crossing more than 50% for half n hour when I start my application. As per my understanding if all threads fights for CPU then CPU% will increases more than by 100 % but which is not happening and my application is taking almost half n hour extra to finish the tasks if include this 1250 threads.

I can see number of Threads with top command as 196 till half n hour.I am using Linux vm with 6 cores per socket and it has 6 cores.

If I remove this 1250 threads which are waiting for socket timeout then my application execution is very fast and within 3 mins other 785 threads able to establish the socket connection and completing their jobs.

Can any one help me to understand though CPU% usage is low, which process is causing the slow down of the application?

9
  • 1
    Java thread != OS thread Commented May 11, 2020 at 20:41
  • CPU is not the only resource threads can contend for. Commented May 11, 2020 at 20:51
  • Wait! Are all of these threads executing the same code? Are they all trying to connect to remote sockets? and does it just so happen that 1250 of them time out trying to talk to sockets that don't answer while 785 of them succeed? If that's the case, then your question boils down to this; "Why doesn't my program use much CPU time when it spends most of its time waiting for I/O?" Waiting for I/O is not an activity that uses CPU time. Commented May 11, 2020 at 23:32
  • 2
    Each thread has its own stack memory and has to be scheduled by the operating system to be run on one of the 6 cores and if I understand correctly they are all trying to open connections to a server that also needs to have similar number of threads? Sounds very inefficient to me and may easily fail on Operating Systems limits. A perfect application should have at most the same amount of threads as there are CPU cores available and use asynchronous tasks, maybe with framworks like these: baeldung.com/java-asynchronous-programming or Akka Actor framework or Java Flow Reactive Streams Commented May 12, 2020 at 8:11
  • 1
    @JohannesB, Agreed, the OP's approach is not scaleable, but you said, "A perfect application should have at most the same amount of threads as...CPU cores." That's true for threads that were created for the explicit purpose of exploiting multiple-CPU cores. But, exploiting multiple CPUs is not the only reason for creating threads. There are other reasons why a programmer would want to have two or more independent activities happen concurrently in the same process. Commented May 12, 2020 at 12:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.