This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Error on threads 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 "Error on threads" Watch "Error on threads" New topic

Error on threads

R Sugan
Ranch Hand

Joined: Mar 13, 2009
Posts: 48

I am new to threads concept.I tried to get the output checking current thread and display relevent page.But i am not able to get successful output.Please help to solve this problem.

The above program checking one condition at a time .I need to if calleridstatus zero means the corresponding output will be displayed

otherwise if calleridstatus one means the corresponding output will be displayed.How to solve this problem.

Manoj Maniraj
Ranch Hand

Joined: Mar 25, 2009
Posts: 38
Its not good practice to implementing runnable in Servlet.

Have a separate class implements runnable and have your logic there and call it from the servlet.
Steve Luke

Joined: Jan 28, 2003
Posts: 4167

Hi Suganya,

The above program checking one condition at a time .I need to if calleridstatus zero means the corresponding output will be displayed

otherwise if calleridstatus one means the corresponding output will be displayed.How to solve this problem.

I am not exactly sure of your problem. How exactly is the application behaving? How is that different than what you expect?

Unfortunately, there is a lot of problems with your code which will eventually cause problems. The rest of this post is less about fixing the problem you describe, and more about suggesting improvements.

You should NEVER store instance variables in a Servlet. Every request to a servlet uses the same Servlet instance, which means every user who reaches your site will be sharing the same Statement, ResultSets, Connection, and User. Then they will also be sharing the same reference to the request and response. This is very bad. If two users attempt to use the Servlet at the same time these variables will begin to be traded between users, their states will get messed up, and the application will break (possible very badly).

So NEVER store instance varables. When possible, pass variables from one method to another through parameters and return values.

So run() doesn't do anything. Like the previous poster said the Servlet should not be a Runnable. Since the run() method doesn't do anything it is best to just remove it, and remove the 'implements Runnable'.

You create a new Thread, then check its name. Since you have not set the name of the Thread yet, the Thread name will NEVER be "crmp1thread". It would only become that after you call th.setName("crmp1thread"). So this whole if construct is rather pointless. It should just be:

Danger! Danger! Danger!

You are calling while(true) inside a Servlet's doGet() method. This leaves the possibility that a request to the Servlet may never end. After just a few users it would use up all the allowed incoming connections, and requests to your application will be blocked and time out. You should not do while(true) in a request service method (or request thread). The request should be controlled, as short as possible, and have a specific end point. If you need to run a long-lasting task then you should move the task to a background thread to free up incoming requests.

Just to be clear. In this while loop, you are running the same query over and over again and only looking at the first entry returned from the query each time. Is that the behavior you want?

To improve your code you should look at using PreparedStatements.

It is a little safer to call:

since it prevents the possibility of a null pointer exception... just a style preference, but if you don't use it you should check for calleridstatus == null.

You are commenting out your connection closing. Where as I agree that these places are not the proper place for the closing, I would suggest that you do not forget to close them entirely - it is another way of eating resources and calling future problems. You want to close the connection in a finally block, probably after the catch(...) block you have below.

It is generally not a good idea to catch the general Exception. Rather catch specific exceptions. Also, since you are running in a Servlet environment, printStackTrace may not be the best way of reporting the exception (if your lucky the exception gets buried in some log deep in your server's path somewhere). You are better off using a logging tool, and wrapping the exception in a ServletException and re-throwing it, so the Servlet engine's exception handling tool can report it properly.

I agree. Here's the link:
subject: Error on threads
Similar Threads
calling java inside a servlet
cookies problem
issue in getting more than one data
datas storing twice from servlet to mysql database