Uncategorized

Java Threads for Browser Hits

The following java program will open an URL repeatedly through a browser.

How it works?

1. Creates a constant thread pool of size 10.

2. 15 tasks will be created and handed over to executor service to execute these task.

3. all ten thread will be used to execute 15 tasks.

4. And shut down the thread service once all thread stop execution.

a. main class for execution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author kvn prabhu
*
*/

/**
*
* This class will repeatedly open a URL in the browser and will close after
* 5000ms.
*
*/
public class OpenBrowser {

static int size = 0;

public static void main(String[] args) {

openBrowser();
}

/**
* 1.This method will create a Thread Pool of 10 threads. 2. 15 workers will
* be created and handled over to execute service to run the threads.
*/
private static void openBrowser() {
// Thread Pool with 10 threads.
ExecutorService service = Executors.newFixedThreadPool(10);
// create the workers and hand over to thread service.
for (int i = 0; i < 15; i++) {

BrowserWorker worker = new BrowserWorker();
service.execute(worker);
}
// shut down the pool once all thread completed execution.
service.shutdown();
// Print all running threads at any given time.
while (!service.isTerminated()) {

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

if (size != threadSet.size()) {

System.out.println(“All Running Threads:” + threadSet);
size = threadSet.size();
}

}

// Print after service termination.
while (service.isTerminated()) {
System.out.println(“Execution Completed.”);
break;
}

}

/**
*
*/

}

b. worker thread for executing a task.

/**
* This class will open 10 browsers on each run.
*
* @author kvn
*
*/
class BrowserWorker implements Runnable {

public void run() {

fire();

}

public void fire() {
for (int i = 0; i < 10; i++) {
try {
Process process = Runtime
.getRuntime()
.exec(“C:\\Program Files\\Internet Explorer\\iexplore.exe \”http://www.youtube.com/watch?v=V5Qk6oRsNmw&feature=youtu.be\””);
Thread.sleep(10000);
process.destroy();
//    System.out.println(“Status:” + process.waitFor());
} catch (Exception e) {
}
}
}
}

Result:

Note: Use any text editor to unwrap the content.

All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-9,5,main], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-4,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Finalizer,8,system], Thread[Reference Handler,10,system], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-9,5,main], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Finalizer,8,system], Thread[Reference Handler,10,system], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-7,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-7,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-7,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[Attach Listener,5,system], Thread[Finalizer,8,system], Thread[Reference Handler,10,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-7,5,], Thread[Signal Dispatcher,9,system]]
All Running Threads:[Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main]]
Execution Completed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s