dcsimg
Multi-Threading Problem
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pinder_Ji
Posted On:   Monday, August 8, 2005 01:06 PM

Hello, Currently I have a servlet when called is responsible for connecting to the database, running the appropriate query, and streaming the resulting xml out. Now I want to change this process so that if the time it takes to run the query is too long (set by a predefined time), I let the user know right away (send URL for them to use at a later time), but still continue to process the request. So the long taking query continues to finish its task and stores/writes the results to some location on the server to issue at a later time. if the query takes longer than the predefined time limit, it will have to notify its parent thread that the time limit has been reached, but then also continue/finish its work. If the query finishes in less    More>>

Hello,



Currently I have a servlet when called is responsible for connecting to the database, running the appropriate query, and streaming the resulting xml out. Now I want to change this process so that if the time it takes to run the query is too long (set by a predefined time), I let the user know right away (send URL for them to use at a later time), but still continue to process the request. So the long taking query continues to finish its task and stores/writes the results to some location on the server to issue at a later time. if the query takes longer than the predefined time limit, it will have to notify its parent thread that the time limit has been reached, but then also continue/finish its work. If the query finishes in less than the predefined time limit, it lets the parent thread know, sends its results, and then dies. Does anyone know how I can go about implementing this?



Hopefully I’m not talking in circles, please let me know if you need me to clarify. :)

   <<Less

Re: Multi-Threading Problem

Posted By:   Almagest_FUTT  
Posted On:   Monday, August 8, 2005 02:01 PM

Pack your data retrieval process into a class implementing Runnable and supporting the following features:


  • Pass an Object (i'd suggest in the constructor) as a lock

  • call notify on the lock when the computation has finished (don't forget to synchronize on it before)

  • A method to query whether the computation has terminated

  • A method to get the data loaded




Then, there where you process your request, you create:

  • an Object to use as the lock

  • one instance of the class described above using the Object you just created.

  • an instance of Thread using the processing instance you just created (which in its turn implements Runnable)


Then write something like the following:

try{
synchronized (lock){
processthread.start();

lock.wait( WAITTIME );

//now, either you've been notified using notify(),
//which means the process has finished
//or the timeout expired

//check it
if( process.hasFinished() ){
//get the data from the processor and send it
} else {
//notify the processor in some way that
//when he'll be finished, he'll have to store
//his results in some way
}
}
}
catch(InterruptedException xx){
// ??
}
About | Sitemap | Contact