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.List;
import java.util.Set;
......@@ -8,37 +7,36 @@ public class InvokeAll {
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();
Set<Callable<String>> callables = new HashSet<Callable<String>>();
try {
Set<Callable<String>> callables = new HashSet<>();
callables.add(new Callable<String>() {
public String call() throws Exception {
return Thread.currentThread().getName() + " Task 1";
}
});
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";
}
});
// Use lambda expressions for concise callable implementations
callables.add(() -> Thread.currentThread().getName() + " Task 1");
callables.add(() -> Thread.currentThread().getName() + " Task 2");
callables.add(() -> Thread.currentThread().getName() + " Task 3");
// invokeAll blocks until all tasks are complete
List<Future<String>> futures = executorService.invokeAll(callables);
try {
List<Future<String>> result = executorService.invokeAll(callables); //Once of task is fully executed the other waiting task will get canceled
for (Future<String> res : result) {
System.out.println(res.get());
for (Future<String> future : futures) {
try {
// get() will not block here because invokeAll has already completed.
System.out.println(future.get());
} catch (ExecutionException e) {
// This is where exceptions from the callables are thrown
e.printStackTrace();
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
// This is thrown if the thread is interrupted while waiting on invokeAll
Thread.currentThread().interrupt();
e.printStackTrace();
} finally {
executorService.shutdown();
}
executorService.shutdown();
}
}
......@@ -7,10 +7,12 @@ import okhttp3.*;
public class RestApiLoadTester {
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" +
"user-agent: Mozilla/5.0";
int threadCount = 5;
int threadCount = 10;
int callCountPerThread = 2;
RestApiLoadTester.runLoadTest(url, headers, threadCount,callCountPerThread);
......
......@@ -4,11 +4,11 @@ public class SubmitCallable {
public static void main(String arg[]){
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());
try {
String msg = (String) future.get(); //blocked
String msg = (String) future.get(); //blocked
System.out.println(msg);
} catch (InterruptedException e) {
throw new RuntimeException(e);
......@@ -19,7 +19,7 @@ public class SubmitCallable {
executorService.shutdown();
}
private static Callable newRunnable(String msg){
private static Callable newCallable(String msg){
return new Callable() {
@Override
public Object call() throws Exception {
......
......@@ -4,28 +4,29 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class SubmitRunnable {
public static void main(String arg[]){
public static void main(String arg[]) {
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());
try {
future.get(); //blocked
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
System.out.println(future.isDone());
System.out.println(future.isDone());
try {
future.get(); //blocked if the task not finished yet
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
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
public void run() {
String report = Thread.currentThread().getName()+" :"+msg;
String report = Thread.currentThread().getName() + " :" + msg;
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