Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why to use thread

 
priyanaka jaiswal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


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


Thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic