Commit 3f281d5d authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

optimization and update

parent f4a50188
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -8,37 +7,36 @@ public class InvokeAll { ...@@ -8,37 +7,36 @@ public class InvokeAll {
public static void main(String[] args) { public static void main(String[] args) {
// Use a try-with-resources statement for the ExecutorService
// if you are using Java 19 or later.
// For earlier versions, a try-finally block is a good practice.
ExecutorService executorService = Executors.newSingleThreadExecutor(); ExecutorService executorService = Executors.newSingleThreadExecutor();
Set<Callable<String>> callables = new HashSet<Callable<String>>(); try {
Set<Callable<String>> callables = new HashSet<>();
callables.add(new Callable<String>() { // Use lambda expressions for concise callable implementations
public String call() throws Exception { callables.add(() -> Thread.currentThread().getName() + " Task 1");
return Thread.currentThread().getName() + " Task 1"; callables.add(() -> Thread.currentThread().getName() + " Task 2");
} callables.add(() -> Thread.currentThread().getName() + " Task 3");
});
callables.add(new Callable<String>() {
public String call() throws Exception {
return Thread.currentThread().getName() + " Task 2";
}
});
callables.add(new Callable<String>() {
public String call() throws Exception {
return Thread.currentThread().getName() + " Task 3";
}
});
// invokeAll blocks until all tasks are complete
List<Future<String>> futures = executorService.invokeAll(callables);
try { for (Future<String> future : futures) {
List<Future<String>> result = executorService.invokeAll(callables); //Once of task is fully executed the other waiting task will get canceled try {
for (Future<String> res : result) { // get() will not block here because invokeAll has already completed.
System.out.println(res.get()); System.out.println(future.get());
} catch (ExecutionException e) {
// This is where exceptions from the callables are thrown
e.printStackTrace();
}
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); // This is thrown if the thread is interrupted while waiting on invokeAll
} catch (ExecutionException e) { Thread.currentThread().interrupt();
throw new RuntimeException(e); e.printStackTrace();
} finally {
executorService.shutdown();
} }
executorService.shutdown();
} }
} }
...@@ -7,10 +7,12 @@ import okhttp3.*; ...@@ -7,10 +7,12 @@ import okhttp3.*;
public class RestApiLoadTester { public class RestApiLoadTester {
public static void main(String[] args){ public static void main(String[] args){
String url = "http://172.29.3.203:30152/hello/fib/30"; // String url = "http://172.29.3.203:30152/hello/fib/30";
String url = "http://localhost:8080/test";
String headers = "content-type: application/json\n" + String headers = "content-type: application/json\n" +
"user-agent: Mozilla/5.0"; "user-agent: Mozilla/5.0";
int threadCount = 5; int threadCount = 10;
int callCountPerThread = 2; int callCountPerThread = 2;
RestApiLoadTester.runLoadTest(url, headers, threadCount,callCountPerThread); RestApiLoadTester.runLoadTest(url, headers, threadCount,callCountPerThread);
......
...@@ -4,11 +4,11 @@ public class SubmitCallable { ...@@ -4,11 +4,11 @@ public class SubmitCallable {
public static void main(String arg[]){ public static void main(String arg[]){
ExecutorService executorService = Executors.newSingleThreadExecutor(); ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future =executorService.submit(newRunnable(" Task")); //not blocked Future future =executorService.submit(newCallable(" Task")); //not blocked
System.out.println(future.isDone()); System.out.println(future.isDone());
try { try {
String msg = (String) future.get(); //blocked String msg = (String) future.get(); //blocked
System.out.println(msg); System.out.println(msg);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -19,7 +19,7 @@ public class SubmitCallable { ...@@ -19,7 +19,7 @@ public class SubmitCallable {
executorService.shutdown(); executorService.shutdown();
} }
private static Callable newRunnable(String msg){ private static Callable newCallable(String msg){
return new Callable() { return new Callable() {
@Override @Override
public Object call() throws Exception { public Object call() throws Exception {
......
...@@ -4,28 +4,29 @@ import java.util.concurrent.Executors; ...@@ -4,28 +4,29 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
public class SubmitRunnable { public class SubmitRunnable {
public static void main(String arg[]){ public static void main(String arg[]) {
ExecutorService executorService = Executors.newSingleThreadExecutor(); ExecutorService executorService = Executors.newSingleThreadExecutor();
Future future =executorService.submit(newRunnable(" Task")); //not blocked Future future = executorService.submit(newRunnable(" Task")); //not blocked
System.out.println(future.isDone()); System.out.println(future.isDone());
try { try {
future.get(); //blocked future.get(); //blocked if the task not finished yet
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (ExecutionException e) { } catch (ExecutionException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
System.out.println(future.isDone()); System.out.println(future.isDone());
executorService.shutdown(); // executorService.shutdown();
} }
private static Runnable newRunnable(String msg){
return new Runnable() { private static Runnable newRunnable(String msg) {
return new Runnable() {
@Override @Override
public void run() { public void run() {
String report = Thread.currentThread().getName()+" :"+msg; String report = Thread.currentThread().getName() + " :" + msg;
System.out.println(report); System.out.println(report);
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment