Search code examples
javaxmlmultithreadingurlconnection

Attempting to Multithread URL Connections to Reduce Load Time


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.


Solution

  • 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();