aspose file tools*
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "why to use thread" Watch "why to use thread" New topic
Author

why to use thread

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79
Hi,


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


Thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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.

Henry


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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Roberto Ramirez
Greenhorn

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
Greenhorn

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why to use thread