Print

Print


> How do I write a computer program that spawns many processes but
> returns one result?
>
> I suppose the classic example of my query is the federated search. Get
> user input. Send it to many remote indexes. Wait. Combine results.
> Return. In this scenario when one of the remote indexes is slow things
> grind to a halt.
>
> I have a more modern example. Suppose I want to take advantage of many
> Web Services. One might be spell checker. Another might be a
> thesaurus. Another might be an index. Another might be a user lookup
> function. Given this environment, where each Web Service will return
> different sets of streams, how do I query each of them simultaneously
> and then aggregate the result? I don't want to so this sequentially. I
> want to fork them all at once and wait for their return before a
> specific time out. In Perl I can use the system command to fork a
> process, but I must wait for it to return. There is another Perl
> command allowing me to fork a process and keep going but I don't
> remember what it is. Neither one of these solutions seem feasible. Is
> the idea of threading in Java suppose to be able to address this
> problem?

Yes. I do this thing all the time for various things (and taking
advantage of multi-cpu and multi-core). Java threading is more
lightweight than forking.

---
MyThread[] myThreads = new MyThreads[20];

// start all threads
for(int i=0; i<20; i++)
        {
        MyThread m = new MyThread();
        m.start();
        myThreads[i] = m;
        }

for(int i=0; i<20; i++)
        {
        // wait for each to complete. Note that a thread may be
        // completed before this method is called.
        myThreads[i].join()
        }

Note that there is a join(long timeoutMillis) method.
Note that the threads can be doing all sorts of different things (like
the situation you describe).

-Glen

--

Glen Newton | [log in to unmask]
Researcher, Information Science, CISTI Research
& NRC W3C Advisory Committee Representative
http://tinyurl.com/yvchmu
tel/tél: 613-990-9163 | facsimile/télécopieur 613-952-8246
Canada Institute for Scientific and Technical Information (CISTI)
National Research Council Canada (NRC)| M-55, 1200 Montreal Road
http://www.nrc-cnrc.gc.ca/
Institut canadien de l'information scientifique et technique (ICIST)
Conseil national de recherches Canada | M-55, 1200 chemin Montréal
Ottawa, Ontario K1A 0R6
Government of Canada | Gouvernement du Canada
--