• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting in thread

 
Sara Brown
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we do sorting using threads that consume less memory and faster than the old version(non-thread)?


how to make sure the thread programming gonna help us running the program a lot faster n consume less memory?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asparagus white wrote:can we do sorting using threads that consume less memory and faster than the old version(non-thread)?


how to make sure the thread programming gonna help us running the program a lot faster n consume less memory?


Everything that happens in a Java program is done by a Thread so "non-thread" makes no sense.

I have used multiple Threads in a program that read files, parsed and sorted and wrote files where the sort thread could work while the IO threads were waiting for reading or writing resulting in very high CPU utililization. Needless to say this was a very special case - so the answer to your question is "it depends."

Bill
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asparagus white wrote:can we do sorting using threads that consume less memory and faster than the old version(non-thread)?


Some sorting algorithms lend themselves to multithreading better than others. This is not really a Java topic. It's a general CS topic. Usually, however, increasing speed also means increasing memory usage.

how to make sure the thread programming gonna help us running the program a lot faster n consume less memory?


You can't guarantee that a multithreaded approach will run faster than a single-threaded one. But in general, for CPU-bound tasks (which sorting is, if the set to be sorted isn't huge), setting the number of threads equal to the number of CPUs or cores will give you the best performance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic