You are on page 1of 5

6/13/13

advantage of using thread (Threads forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author
abalfazl hossein Ranch Hand Joined: Sep 06, 2007 Posts: 602

advantage of using thread


posted 1/6/2010 3:07:51 AM

I found an interesting article about thread http://marcja.wordpress.com/2007/04/06/four-reasons-to-use-multithreading/

You see, todays hardware isnt getting more Hertz, its getting more pipelines (as with HyperThreading), more cores (such as the Intel C ore Duo), and additional processors. This means that even if youre 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?

www.coderanch.com/t/477417/threads/java/advantage-thread

1/5

6/13/13

advantage of using thread (Threads forum at JavaRanch)

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. C ode each line block with one mind. Let the OS thread scheduler do the interleaving.

May someone explain more about it?


Sebastian Janisch Ranch Hand Joined: Feb 23, 2009 Posts: 1183

posted 1/6/2010 3:47:11 AM

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.

JDBC Support - An easy to use, light-weight JDBC framework abalfazl hossein Ranch Hand Joined: Sep 06, 2007 Posts: 602

posted 1/6/2010 10:38:48 PM

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. C ode each line block with one mind. Let the OS thread scheduler do the interleaving.

What do you understand from this?


abalfazl hossein Ranch Hand Joined: Sep 06, 2007 Posts: 602

posted 1/26/2010 7:10:06 PM

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.

www.coderanch.com/t/477417/threads/java/advantage-thread

2/5

6/13/13

advantage of using thread (Threads forum at JavaRanch)

C ode 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 Joined: Sep 28, 2004 Posts: 16787

posted 1/26/2010 7:25:21 PM


abalfazl hossein wrote:

19
I like...

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. C ode 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
www.coderanch.com/t/477417/threads/java/advantage-thread 3/5

6/13/13

advantage of using thread (Threads forum at JavaRanch)

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor) abalfazl hossein Ranch Hand Joined: Sep 06, 2007 Posts: 602

posted 1/27/2010 1:18:22 AM

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 Joined: Sep 06, 2007 Posts: 602

posted 1/31/2010 1:20:54 AM

C onsider 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?

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: advantage of using thread

Similar Threads Running java on multiple processors on SuSE 10.1 Is Concurency dependent on CPU architecture? 100% CPU usage on Dual Core PC
www.coderanch.com/t/477417/threads/java/advantage-thread 4/5

6/13/13

advantage of using thread (Threads forum at JavaRanch)

Multithreading: Usage scenarios Multi-Core Vs. Single-Core (For the serious Java Archs)
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 22:44:33 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/477417/threads/java/advantage-thread

5/5

You might also like