Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Multithread Multiple Requests Approach

I want to send to "test.com" a request from 0 to 100, the code i have will send a request every one second... In this way the program will take 100 seconds in order to complete.

What i would like to do is set 10 threads running all at the same time, making the thread 1 going from (0,10); thread 2 going from (10,20) ... and so on, in this way the program should take only 10 seconds or so in order to complete, is that possible ? how can acomplish it ?

import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class Palomo implements Runnable {
    String url = "http://test.com";
    HttpClient client = null;
    PostMethod method = null;
    BufferedReader br = null;
    String contents = null;

    public void run() {
        for (int i = 0; i <= 100; i++) {
            synchronized (this) {
                doPost(i);
            }
                      try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        }
    }

    public void doPost(int i) {
        try {
            client = new HttpClient();
            method = new PostMethod(url);

            this.method.addParameter("myPostRequest", Integer.toString(i));

            client.executeMethod(method);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        }
    }

    public static void main(String[] args) {
        new Thread(new Palomo()).start();
    }
}

Thanks a lot !

EDIT

Reading the indications you gave me, i created this horrible monster...

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SimpExec {
    public static void main(String args[]) {

        ExecutorService es = Executors.newFixedThreadPool(4);

        es.execute(new MyThread("A"));
        es.execute(new MyThread("B"));
        es.execute(new MyThread("C"));
        es.execute(new MyThread("D"));

        es.shutdown();
    }
}

class MyThread implements Runnable {
    String name;

    MyThread(String n) {
        name = n;
        new Thread(this);
    }

    public void run() {
        if (name=="A"){
            for (int i=1;i<=10;i++){
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        if (name=="B"){
            for (int i=10;i<=20;i++){
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        if (name=="C"){
            for (int i=20;i<=30;i++){
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        if (name=="D"){
            for (int i=30;i<=40;i++){
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

I know this is probably the most awful piece of code you ever watched, but is making exactly what i want, if you can give me some directions on how i should accomplish this in the right way that would be great.

THANKS A LOT FOR ALL YOUR GREAT ADVICES

like image 532
D.Snap Avatar asked Oct 11 '12 08:10

D.Snap


1 Answers

You should have a look at ExecutorService which has been created to achieve this kind of things.

You can create a pool of 10 Threads using Executors.newFixedThreadPool(10); and then submit the tasks (Runnable) you want to be executed. The pool takes care of dispatching the tasks among the Threads.

like image 140
mkhelif Avatar answered Sep 30 '22 04:09

mkhelif