5

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

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

5

You can create an Executor with Executors.newFixedThreadPool(10) and use it to execute each request as its own Runnable.

Comments

0

Why don't you minize the time between the PostRequest. Using threads would have the same impact, as the time between the sends is smaller. the only thing you would accomplish by using threads on this changing the order in which the Posts are send To

1, 11, 21,31,41 ... 2, 12,22,32,...

Use

Thread.CurrentThread.Sleep(100)

to lower the time between sends in your main application.

Also you could run into trouble by letting multiple threads access the same HTTPClient. Even, if you synchronize the acces, the sending process would be serial, as the httpclient can only post 1 request at a time.

Comments

0

To add on the other comments, suggesting to use the ExecutorService( which is a good solution)
each one of the submitted Runnable objects should have the range of requests to handle.
. Your should consider having class extending Runnable.
The code for it can loook like -

public class RangedPosts extends Runnable {
    private int start;
    private int end;
    public RangedPosts(int start,int end) {
        this.start = start;
        this.end = end;
    }

    public void run() {
       //Perform here a loop from start to end
    }
}

And then the usage should be a for loop, creating 10 runnable objects of RangePosts, passing them the range definition (start and end)

Comments

0

I have done something similar, I will share my first POC as it feels good to share.

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ThreadCreater {

    // use getShape method to get object of type shape
    public Thread threadRunner(int start, int end, int threadCount, String url) {
    Thread thread = null;
    int tempEnd = 0;
    for (int i = 0; i <= end; i = i + 10) {
        start = i;
        tempEnd = i + 10;
        thread = getThread(start, tempEnd, threadCount, url);
        thread.start();
    }

    return null;
    }

    public static Thread getThread(int start, int end, int threadCount, String url) {
    Thread thread = new Thread() {
        public void run() {
        try {
            sendGET(start, end, url);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

        private void sendGET(int start, int end, String url) throws Exception {
        url += "start=" + start + "&end=" + end;
        URL obj = new URL(url);
        // Send post request
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // basic reuqest header to simulate a browser request
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/51.0");
        con.setRequestProperty("Upgrade-Insecure-Requests", "1");
        con.setRequestProperty("Connection", "keep-alive");
        con.setDoOutput(true);

        // reading the HTML output of the POST HTTP request
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
        }
    };
    return thread;
    }

    public static void main(String[] args) {
    ThreadCreater obj = new ThreadCreater();
    int start = 0;
    int end = 100;
    int threadCount = 10;
    String url = "http://iseebug.com/test.php?";

    obj.threadRunner(start, end, threadCount, url);
    }

}

simple test.php code below

<?php
/**
 * Created by PhpStorm.
 * User: polo
 * Date: 02-04-2017
 * Time: 22:28
 */


$q1 = isset($_GET['start']) ? $_GET['start'] : 'fakeMIP';
$q2 = isset($_GET['end']) ? $_GET['end'] : 'fakeMIP';


if($q1>=0&&$q2<=10){
echo $q2;
}elseif ($q1>=10&&$q2<=20){
    echo $q2;
}elseif ($q1>=20&&$q2<=30){
    echo $q2;
}elseif ($q1>=30&&$q2<=40){
    echo $q2;
}elseif ($q1>=40&&$q2<=50){
    echo $q2;
}elseif ($q1>=50&&$q2<=60){
    echo $q2;
}elseif ($q1>=60&&$q2<=70){
    echo $q2;
}elseif ($q1>=70&&$q2<=80){
    echo $q2;
}elseif ($q1>=80&&$q2<=90){
    echo $q2;
}elseif ($q1>=90&&$q2<=100){
    echo $q2;
}

Good luck.

But i will prefer java's ExecutorService for high usage, for limited requirement , you can go for basic threading.

Ping me on any help.

1 Comment

I create a lot of web automation apps and spamming tools :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.