• Post Reply Bookmark Topic Watch Topic
  • New Topic

advantage of using thread  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found an interesting article about thread

http://marcja.wordpress.com/2007/04/06/four-reasons-to-use-multithreading/

You see, today’s hardware isn’t getting more Hertz, it’s getting more pipelines (as with HyperThreading), more cores (such as the Intel Core Duo), and additional processors. This means that even if you’re maxing out one processor calculating a billion digits of π, the rest of the processors on your multi-way Dell are drinking mint juleps on the veranda wondering what all the fuss is about.


Does it mean that if you deal with a CPU that has 4 core, it is possible 4 threads use this four core at the same time?


The key in using threads to simplify your code is to try to keep your functions and methods on one train of thought. Rather than turn your code into an arthritic mess trying to interleave multiple simple simultaneous tasks, code each task separately. Code each line block with one mind. Let the OS thread scheduler do the interleaving.


May someone explain more about it?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that's exactly what it means. but even with a single core a multi threaded program can run MUCH faster compared to a single threaded one.

Here is a scenario.

Imagine you want to program a crawl robot program. Just like google.
Now, when you open a website, there is a major latency between opening the connection and finally being able to retreive data. Not a surprise, a lot is happening behind the scenes. During that time, the program is idle, doing nothing and waiting for sunshine. Another thread can take advantage of that delay doing other work, for instance opening a connection to another website. Even with a single core this speeds up the process immensely.

Hope you got the idea.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key in using threads to simplify your code is to try to keep your functions and methods on one train of thought. Rather than turn your code into an arthritic mess trying to interleave multiple simple simultaneous tasks, code each task separately. Code each line block with one mind. Let the OS thread scheduler do the interleaving.


What do you understand from this?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The key in using threads to simplify your code is to try to keep your functions and methods on one train of thought. Rather than turn your code into an arthritic mess trying to interleave multiple simple simultaneous tasks, code each task separately. Code each line block with one mind. Let the OS thread scheduler do the interleaving.



There are many reasons to use threads in your Java programs. If you use Swing,
servlets, RMI, or Enterprise JavaBeans (EJB) technology, you may already be using
threads without realizing it.
Some of the reasons for using threads are that they can help to:
• Make the UI more responsive
• Take advantage of multiprocessor systems
• Simplify modeling
• Perform asynchronous or background processing


simplify modeling?May someone explain?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:

The key in using threads to simplify your code is to try to keep your functions and methods on one train of thought. Rather than turn your code into an arthritic mess trying to interleave multiple simple simultaneous tasks, code each task separately. Code each line block with one mind. Let the OS thread scheduler do the interleaving.


May someone explain more about it?


Many years ago, I ran into code that had the comment "threads are hard, please don't thread this". The module had 300+ lines of code, and basically did three independent things in an interweaved fashion. And never quite worked right, because there were a large amount of edge conditions depending on how the web services responded.

I threaded it... knowing full well it will be challenged during a code review. With three threads working independently, there were no edge conditions caused by interweaving, it worked 100% of the time, and total amount of code was about 30+ lines. And besides a mention, there was no code review challenge.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the usage of threads in network programming.

Say we have an application for a company that has 70 persons that want to chat each other on LAN network.

The chat program has code for send messages. if this program does'nt use thread,then the other persons must wait until the person that sends the message.It means if one person uses the program . Then other 69 persons must wait until that person finish sending.and there is not other way ,unless create 70 function for sending. Right?

Is it good example for usage thread in network programming?May you suggest another examples?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider a simulation application, where you simulate the interaction
between multiple entities. Giving each entity its own thread can greatly simplify many
simulation and modeling applications.


It makes no sense for me. May you explain more?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!