Baran İpek Oracle and Java

February 12, 2010

Multithread Url Call with Java

Filed under: java — baranipek @ 9:34 am

Assume that you want to make sure  your web application’s concurrency as a curious developer:). We can call url with get method through a simple java class. We will test two cases. First test will show us 20 concurent user call url at the same time.However,we will use Executors classes for concurrency call and  Thread classes for consecutive call. Thanks to this test,you can monitor Heap usage and memory allocated in the application server.

————–Test-1
import java.io.IOException;
import java.util.Calendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MultiproccesingIndependent {
ExecutorService executor = Executors.newFixedThreadPool(1);

public void start() throws IOException {
int i=0;

while (!executor.isShutdown() && i<20)
executor.submit(new MyThread(i++));
}

public void shutdown() throws InterruptedException {
executor.shutdown();
executor.awaitTermination(30, TimeUnit.SECONDS);
executor.shutdownNow();
}

public static void main(String argv[]) throws Exception {
new MultiproccesingIndependent().start();
}
}

class MyThread implements Runnable {
private int i;
MyThread(int i) {
this.i = i;
}

public void run() {

Calendar cal = Calendar.getInstance();

MultiProcessing urlReader = new MultiProcessing();
try {

urlReader
.callAdress(url);

} catch (IOException e) {

e.printStackTrace();
}

}
}

——-Test-2

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;

public class MultiProcessing {

static void threadMessage() {

String threadName = Thread.currentThread().getName();

System.out.print(threadName);

}

private static class MessageLoop implements Runnable {

public MessageLoop() {

}

public void run() {

synchronized (MessageLoop.class) {

try {

threadMessage();

MultiProcessing urlReader = new MultiProcessing();

urlReader.callAdress(“your url“);

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void callAdress(String urlEnd) throws IOException

{

URL url = new URL(urlEnd);

URLConnection conn = null;

try {

conn = url.openConnection();

} catch (IOException e) {

e.printStackTrace();

}

BufferedReader in = new BufferedReader(new InputStreamReader(conn

.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

System.out.println(line);

}

}

public static void main(String args[]) throws InterruptedException {

for (int i = 0; i < 30; i++)

{

MessageLoop mLopp = new MessageLoop();

new Thread(mLopp).start();

}

}

For further reading

http://java.sun.com/docs/books/tutorial/essential/concurrency/

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.