Reputation: 59
Snip from the main
public void xmlQuery(String x,String y){
//takes spaces off the input
String k = x.trim();
String v = y.trim();
//calling the threading class
for(int i = 0; i<18; i++){
callThreading.threadedCall(i,k,v);
}
}
The threading class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class callThreading {
public static void threadedCall(int th, String k, String v){
switch(th){
case 0:Thread currentThread0 = new Thread(threadedCall0(th, k, v)).start();
break;
case 1:Thread currentThread1 = new Thread(threadedCall1(th, k, v)).start();
break;
case 2:Thread currentThread2 = new Thread(threadedCall2(th, k, v)).start();
break;
case 3:Thread currentThread3 = new Thread(threadedCall3(th, k, v)).start();
break;
case 4:Thread currentThread4 = new Thread(threadedCall4(th, k, v)).start();
break;
case 5:Thread currentThread5 = new Thread(threadedCall5(th, k, v)).start();
break;
case 6:Thread currentThread6 = new Thread(threadedCall6(th, k, v)).start();
break;
case 7:Thread currentThread7 = new Thread(threadedCall7(th, k, v)).start();
break;
case 8:Thread currentThread8 = new Thread(threadedCall8(th, k, v)).start();
break;
case 9:Thread currentThread9 = new Thread(threadedCall9(th, k, v)).start();
break;
case 10:Thread currentThread10 = new Thread(threadedCall10(th, k, v)).start();
break;
case 11:Thread currentThread11 = new Thread(threadedCall11(th, k, v)).start();
break;
case 12:Thread currentThread12 = new Thread(threadedCall12(th, k, v)).start();
break;
case 13:Thread currentThread13 = new Thread(threadedCall13(th, k, v)).start();
break;
case 14:Thread currentThread14 = new Thread(threadedCall14(th, k, v)).start();
break;
case 15:Thread currentThread15 = new Thread(threadedCall15(th, k, v)).start();
break;
case 16:Thread currentThread16 = new Thread(threadedCall16(th, k, v)).start();
break;
case 17:Thread currentThread17 = new Thread(threadedCall17(th, k, v)).start();
break;
}
}
public static Runnable threadedCall0(int th, String k, String v){
System.out.println("call0");
return null;
}
public static Runnable threadedCall1(int th, String k, String v){
System.out.println("call1");
return null;
}
}
...and the methods go on to threadedCall17
CODE EDITED to reflect MVCE as best I can.
I am trying to multithread all my URL connections at the same time so that load time of my program is reduced. Current load time is ~11 seconds. The parameter int th in the callThreading class is passed from a for loop in the main method and int th goes from 0++ to 17.
It has been pointed out to me there would be diminishing return if I were to do all 18 calls at once. I will tinker with different rates once the threading is working.
The code listed results in “Type mismatch: cannot convert from void to Thread” errors.
If I am lacking any detail, let me know.
Upvotes: 0
Views: 89
Reputation: 310913
Thread currentThread1 = new Thread(...).start();
Thread.start()
doesn't return a Thread
. It is a void method. So you can't use the result to initialize anything.
You could solve this in various ways, but the simplest is:
Thread currentThread1 = new Thread(...);
currentThread1.start();
I don't really see why you have all those Thread
variables. One would do. Then you could move the start()
call to after the switch
statement:
Thread currentThread;
switch th)
{
case 0:
currentThread = new Thread(...);
break;
// ...
default:
// unreachable, just to shut up the compiler
return;
}
currentThread.start();
Upvotes: 2
Reputation: 718826
First comment. Your threadCall
method is odd. Each time it is called, it creates 18 Thread objects, starts one of them and throws the rest away. Odd. However, that won't cause a threading bottleneck. (The unused threads never get started ...)
There is also some poor coding in the threadedCallNN
methods, but nothing that would cause a threading bottleneck.
Basically, nothing in the code you have shown us explains the problem.
In the absence of an MCVE that we could run for ourselves we can only guess as to why you are not getting a speed up. A couple of possible explanations:
The limiting factor could be the throughput of your end-to-end network connection.
The limiting factor could be the rate at which the remote server can deliver data.
The remote server could be rate limiting you; e.g. by processing your requests one at a time.
If the real explanation is any one of those, then multi-threading won't help.
You may be able to get some insights by adding some tracing. For example, try to see if the threads are actually attempting to read data at the same time, or if something is causing them to do something one thread at a time.
Upvotes: 2