File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Advise in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Advise in Threads" Watch "Advise in Threads" New topic
Author

Advise in Threads

rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Hey all,

I knew the basics of Threads in Java. I wanted to implement this in my application(I am using Apache tomcat + struts 2) to execute a job and upon completion trigger another job.

Basically upon a action, I wanted to throw a message to the user in the GUI and run a thread in the background to execute the Database process which is time consuming ..

Can this be implemented using Thread or without. Please advise ..
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You will probably want to use a Thread. If the action takes a long time to accomplish that you would be preventing the Thread used by the server from returning to the thread pool. If enough requests are made then the thread pool that serves requests could be blocked, waiting for past requests to finish.

A few things to note:
1) Web servers are usually tuned to the resources on the computer they are hosted. They have configurations for the number of threads in the thread pool. Assuming that your web server is properly tuned, you may be disrupting the balance between responsiveness and resources the server was configured for, which could adversely affect all the applications running in the server. So be careful, spawn threads only when you need to, and check resource availability once your app is up and running so you can re-tune the web server as appropriate (perhaps adding more memory, or reducing the thread pool used for hosting requests).

2) If you spawn a new Thread, you won't be able to communicate with the client to send messages. Once the main thread hosting the request/response is done, the response is closed and you won't be able to talk to send anything their way again.

3) If you plan on using anything like the session, or any other shared object/scope make sure you go through the process of synchronized blocks to prevent corrupting data.


Steve
rahulJ james
Ranch Hand

Joined: Oct 03, 2008
Posts: 123
Basically the 1st message which I am sending to the user is a constant which will be sent immediately wherein the task which has to run in the background takes lot of time to ccomplish the task. So I wanted a solution to figure out this. So as per your suggestion I got to use the Thread and Do you see will there be any impact. In the background, I will be updating a particular table and which is taking lot of time..

Please throw your thought If I can continue using the thread for this situation or Is there any better approach?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Advise in Threads