Can using thread solve this problem?
I have gone through the documentation for threads, but could not come up with a suitable solution to this problem.
Currently I have this:
for (int i =1; i <10000; i++) {
//do some processing.
//I want to create multiple parallel tasks here using different value of i running simultaneously.
}
This is taking a long time to run, so I want to split this up such that it runs 10 times faster. I was thinking of using threads.
Let us say I run this using 10 threads, then each thread will get the value from 1 to 10 and on completion 11 to 20 and so on...
The examples I have seen so far indicate the loop within the run method:
publicvoid run(){
for (int i = 0; i < 10; i++){
System.out.println(i +" " + getName());
try{
sleep((long)(Math.random() * 1000));
}catch (InterruptedException e){}
}
System.out.println("DONE! " + getName());
}
How do I get control over the variable within the loop and run the task independently?
Any suggestions are welcome.
[1574 byte] By [
sbarangaa] at [2007-10-1 21:44:18]

Using multiple threads won't make your program run any faster, unless you're using a computer that is capable of running multiple execution units simultaneously (e.g., a multi-processor machine) and the JVM you're running can take advantage of the computer's abilities. In fact, it'll run a little slower because now there's some additional overhead.
Usually you use multiple threads because it makes the program design cleaner.
If you wanted multiple threads over a loop like that, I suppose you could do something like:
public class LoopParts implements Runnable {
private int start, stop;
public LoopParts(int start, int stop) {
this.start = start;
this.stop = stop;
}
public void run() {
for (int i = start; i < stop; i++) {
// do something
}
}
}
new Thread(new LoopParts(0, 100)).start();
new Thread(new LoopParts(100, 200)).start();
I guess. Maybe someone else on the forum will suggest something better...
If the tasks involved I/O of any kind then maybe threads would help (i.e. as one thread is blocked, the others can do other stuff)... but for tasks that are mostly calculations, multiple threads won't help.
If you have access to other computers on a local network, maybe you can do some distributive processing..
Actually the appliction I was coding was to go and fetch data from the internet. Data was specific and to be retrieved for certain URLs which were stored in a file. So while one thread sent a request and was waiting for a response, the other threads could send requests. Unfortunately I do not have access to a cluster, so I would need to make my 1 computer do multitasking :(
The sample code provided by paulcw seems to be the way to go. I will implement this and see. Thanks Paul.
> Actually the appliction I was coding was to go and
> fetch data from the internet. Data was specific and
> to be retrieved for certain URLs which were stored
> d in a file. So while one thread sent a request and
> was waiting for a response, the other threads could
> send requests.
Then multithreading may help you. How many threads depends on how long it takes to get a response and how much processing there is to do on a response.
> Unfortunately I do not have access to
> a cluster, so I would need to make my 1 computer do
> multitasking :(
You don't need it to benefit from multithreading when there's I/O involved.