Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes why to use thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "why to use thread" Watch "why to use thread" New topic

why to use thread

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79

why to use thread in application, when already we can use servlet and jsp which are multhithreaded?

Thanks in advance
Henry Wong

Joined: Sep 28, 2004
Posts: 18545

priyanaka jaiswal wrote:
why to use thread in application, when already we can use servlet and jsp which are multhithreaded?

Not all applications are servlets.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke

Joined: Jan 28, 2003
Posts: 4167

If you are talking just about Web Applications then I would say you should tread lightly when using threads - you may not want to use them at all but if you do make your own threads then make sure you absolutely need to.

But either way your Web Application will be using multiple threads and so you have to worry about save data management, synchronization, and how data is shared across requests.

Roberto Ramirez

Joined: Sep 11, 2012
Posts: 3
priyanaka jaiswal wrote:Hi,

why to use thread in application, when already we can use servlet and jsp which are multhithreaded?

Thanks in advance

Sometimes you need to manage tasks behind the scenes, you need to create thrads and be responsible for making your applications (web or desktop) as efficient as posible, threading not only gives you better performance but gives you the chance to write more complex and robust applications
Richa Sinha

Joined: Sep 14, 2012
Posts: 5
The web applications are multi-threaded, so we don't need to explicitly use threads there but all the applications that we develop are not web applications that use JSP or Servlets.

Threads are used when you want to execute two or more tasks in parallel. One example where threads are required could be an application where the application loads something from the internet on the startup and we want the loading icon to come during the loading time. Here there are two tasks running in parallel. Suppose we have a function public void loadFromInternet() that loads the data from internet and we have one more function public void showLoadingScreen() that shows the loading icon. Now if we call the loadFromInternet() and showLoadingScreen() function one after the other, then the first of all, the data will be loaded and after that the loading icon will be displayed, which is not what we want. We want that both the functions should work at the same time. Here threads come to rescue. We can create two threads and then call the two functions, one on each thread and acheive our desired goal.

But while using threads, we need to take care of the synchronization issues as well. We need to ensure that two threads should not modify the same resource at the same time.
jQuery in Action, 2nd edition
subject: why to use thread
Similar Threads
Daemon Thread
about thread and Runnable interface
servlet and thread clarification
What is a daemon thread?
regarding daemon thread