ArrayList data output by threads got mingled..
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Brian_Tsang
Posted On:   Wednesday, July 19, 2006 10:13 PM

Hello All Guru, Hope someone can shed some lights here please. I am working on a multiple search tasks web-app with a requirement to provide an intermediate screen to display each selected search task status, e.g. "processing", or a number of result returned. This is what I'd done so far: 1. Upon receiving the search request I kick off a thread for each target search task. i.e. if three target sites are specified there will be three search agent threads running for each of the search task. 2. Each thread generates an ArrayList of result and put it into the session then I flag it to stop/kill itself by setting/checking a boolean variable in their "run&qu   More>>

Hello All Guru,


Hope someone can shed some lights here please.


I am working on a multiple search tasks web-app with a requirement to provide an intermediate screen to display each selected search task status, e.g. "processing", or a number of result returned.


This is what I'd done so far:


1. Upon receiving the search request I kick off a thread for each target search task. i.e. if three target sites are specified there will be three search agent threads running for each of the search task.


2. Each thread generates an ArrayList of result and put it into the session then I flag it to stop/kill itself by setting/checking a boolean variable in their "run" methods.


3. My main process, the coordinating servlet (say PerformSearch) dispatches a status page (JSP) and the status page then enters a 10 seconds polling state until the user press a "Stop" button, for which I send a request to stop the polling and get the individual list and combine/sort them to a final list before dispatching it to the user.


So far so good. I can simulate the "wait for more" and interrupt states of the behaviour. But, when I start another browser to kick off another search on different search word, the worker threads seem to mingle the results.
e.g. say in 1st search task, browser A, I specify "beijing" and kick off the threads and they go ahead to search the selected target (it's web-sites or databases). While the 1st search tasks' threads are still running, 1 or 2 threads may return and indicate they have result during the status polling, and the rest of the threads ( form other search targets) may still be running due to the nature of the target sites/databases ( slow response, unavilable, etc. ). At this time if I start another brower, say browser B ( or separate window of the same browser - supposedly they're separate http session ) and kick off a 2nd search task on "taiwan" then those threads still runnning may receive this new search term and suddenly decided to put the search result of the "taiwan" into part of its generating result list !!! Disaster ! Know what I mean?


So, my question are:

  • 1. Does java thread not live within a session?

  • 2. As I gathered, you can't call Thread.stop() now, is there any better way to end a running thread?

  • 3. How can I avoid this data mingling from happening?


  • Really hope someone can give any clue/hints please.


    Appreciate that in advance.


    Cheers

    -Brian

       <<Less

    Re: ArrayList data output by threads got mingled..

    Posted By:   Anonymous  
    Posted On:   Thursday, July 20, 2006 08:17 AM

    Brian,


    First of all; in many browsers opening a new window does NOT guarantee you a new session. This is something I unfortunately had to figure out the hard way.

    Your question is all about session management and where you keep your 'reference point' to the running search.


    Keeping this 'reference point' in the session context is the way to go for you, so probably there is another problem why things get mixed up. My advise:

    Maybe you should convert your 'thread' into a 'searchjob' type class that extends thread and keeps a private, non-static list for the results and exposes a Collections.unmodifiableList() through a getter.


    That way you're sure that no thread modifies any other thread's list content and you can still query the results through the job's getter.


    About stopping: If your 'run' is repetitive (e.g. looping with a wait statement), you could simply create an abort method that sets a boolean which in turn will flag your 'while' condition to fail. This is a commonly used elegant way to abort a running process.


    If your job is a single-call to an external library however, this is of not much use and unless the library itself has a way to be signalled (e.g. by sending some interrupt to the thread), you probably just have to let the search finish..


    Hope this helps,

    Sjoerd.

    About | Sitemap | Contact