Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

creating 100 threads versus running 10 application instances of 10 threads each

 
Ranch Foreman
Posts: 1788
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is expected to give better performance out of creating 100 threads and running 10 application instances of 10 threads each and why ? Thanks
 
Saloon Keeper
Posts: 12417
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No way to tell. It depends on the hardware, operating system and virtual machine.

It's also not important. If you have to ask questions like this, you're probably designing your software wrongly.
 
Monica Shiralkar
Ranch Foreman
Posts: 1788
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many years back , I worked on an application where I had created a multi threaded program which did not work fast and I was told to increase the number of instances instead of the number of threads specified and this resolved the issue.However, I did not understand that time that how did this way help in resolving the issue and that what difference does increasing number of instances makes in comparison to increasing the number of threads.
 
lowercase baba
Posts: 12907
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
better performance in terms of what - speed, memory use, CPU use...or something else?  You can often increase one at the cost of the others...possibly two...but really never all three.

It may also depend on what the jobs are doing and what is your primary constraint.  If your DB can't handle 100 workers requesting data, it doesn't matter if the are in one or ten JVMs...
 
Monica Shiralkar
Ranch Foreman
Posts: 1788
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In terms of speed. The program was doing binary encoding of large number of files and moving them to other folder once done. All of a sudden we saw there are lakhs of file waiting to be processed and the lag kept increasing. I increased the number of threads but beyond a number the performance in terms of speed did not increase. I was adviced to create multiple instances of this application and run.This resolved the issue.

So, all I am trying to know is that I know what effect does creating more threads have but I do not know what effect does running multiple instances of the application have ?
 
Stephan van Hulst
Saloon Keeper
Posts: 12417
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, it's hardware and operating system dependent.

It sounds like the lagging program was not well written. Also note that using more threads/processes will not improve performance if there are no idle processors. In general, having 100 threads will not give any speed improvement over using just 4 threads when you're running on a system with 4 processors.
 
Monica Shiralkar
Ranch Foreman
Posts: 1788
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:In general, having 100 threads will not give any speed improvement over using just 4 threads when you're running on a system with 4 processors.


Thanks
I finally understood that in which case will increasing the number of threads give speed improvement. So means if you run multiple threads you will utilise more of the CPU capacity but this will happen only until max CPU utilization.

The second thing( which I am still trying to understand ) is that relatively what kind of benefit does increasing the number of instances of application have (in comparison to increasing the number of threads)
 
Stephan van Hulst
Saloon Keeper
Posts: 12417
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is hardware and operating system dependent.
 
Saloon Keeper
Posts: 22638
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JVM is a System Process. It has heavyweight resource requirements, including both time-scheduling and memory allocation resources.

A Thread is a lightweight process. It gets its memory from the JVM memory resource pools, for example amd runs in the same virtual memory address space as the other threads in the JVM.

As a general rule in a uniprocessor multi-tasking system, only one Process is active at a time, so only one JVM - and its associated Threads - are likely to be executing at any given instance. In a multi-processor/multi-core system, the same basic constraint applies except that of course multiple Processes are going to be active, up to one per processor/core.

I'm going to ignore what happens when you enlist a GPU to do massively parallel processing, since that's a specialized case.

But as I said, ,expect that only one VM per core will be able to execute at one time, max.

Threads can actually be handled in various ways. In the simplest, you have co-operative multi-threading, where only one thread can be active at a time and each thread has to willingly relinguish the CPU. Next step up is pre-emptive multi-threading, where interrupts/time slices can be involved. Many people consider this to be the actual lowest level of "true" multi-tasking.

Then you have intelligent schedulers. There are a number of ways to schedule tasks and threads, and often you'll have some sort of system setting to allow choosing the optimal one for your workload. Round-robin within priority levels is very popular. You also may have the option within a System Process to run threads on a single core (for the entire Process) or on multiple cores.

So, in theory, more threads in a single JVM would be less overhead (since Task dispatching is more overhead than thread dispatching, especially if Virtual Memory spaces get swapped), but Your Mileage May vary, depending on the OS, and on options set at both the OS and JVM levels. And, of course, by just how efficiently the threads themselves were programmed. An extra JVM may actually give significally beter overhead if the threads internal to each JVM are butting heads with each other.
 
Monica Shiralkar
Ranch Foreman
Posts: 1788
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.Does that mean that if I have a quad core processor than to improve performance with respect to time ,one can run upto 4 instances of the app?
 
Marshal
Posts: 25938
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to tell. Not without knowing what the app does.
 
Tim Holloway
Saloon Keeper
Posts: 22638
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Restating what Paul said: Performance depends far more on how the application is written than it is on how many OS Processes it's running on. And for that matter, how many threads are spawned.
 
Greenhorn
Posts: 18
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also different ways to use your threads.  For example, Akka uses very lightweight Actors to distribute work for concurrent "reactive" processing, and is available for Scala or Java.  Kotlin has coroutines, which are similar and built into the language.

But it still boils down to what you're trying to do, what your resources are, and where you're trying to do it. No free lunches.
 
Wanna see my flashlight? How about this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic