Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't my threads start at the same time? Java

I have variable number of threads which are used for parallel downloading. I used this,

for(int i = 0; i< sth; i++){
       thrList.add(new myThread (parameters));
       thrList.get(i).start();
       thrList.get(i).join();

}

I don't know why but they wait for each other to complete. When using threads, I am supposed get mixed print outs, since right then there are several threads running that code. However, when I print them out, they are always in order and one thread waits for the previous one to finish first. I only want them to join the main thread, not wait for each other. I noticed that when I measured time while downloading in parallel.

How can I fix this? Why are they doing it in order?

In my .java, there is MyThread class with run and there is Downloader class with static methods and variables. Would they be the cause of this? The static methods and variables?

How can I fix this problem?

like image 249
Ada Avatar asked Apr 10 '26 02:04

Ada


1 Answers

you are creating a thread, waiting for it to complete (join), creating a new thread, waiting for it to complete (join) etcetera.

you should read the java documentation regarding threads to know what most method do: http://download.oracle.com/javase/tutorial/essential/concurrency/index.html and http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html

like image 91
Salandur Avatar answered Apr 12 '26 14:04

Salandur