introducing lambda (#2305)
* moving jmh into libraries module * refactoring jmh * Update pom.xml * manual algorightm * with BM result * fix for space issue * Fixed indentation * change as per suggestion * vavr either * adding unit test and othe rutilities * adding concurrent module * concurrent package description * concurrent package description * Update EitherUnitTest.java * introducing lambda expression
This commit is contained in:
parent
f58c41de4d
commit
1be49ebc7f
|
@ -1,6 +1,5 @@
|
||||||
package com.baeldung.concurrent.Scheduledexecutorservice;
|
package com.baeldung.concurrent.Scheduledexecutorservice;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
@ -12,32 +11,21 @@ public class ScheduledExecutorServiceDemo {
|
||||||
public void execute() {
|
public void execute() {
|
||||||
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
ScheduledFuture<?> scheduledFuture = executorService.schedule(new Runnable() {
|
ScheduledFuture<?> scheduledFuture = executorService.schedule(() -> {
|
||||||
@Override
|
// Task
|
||||||
public void run() {
|
|
||||||
// task details
|
|
||||||
}
|
|
||||||
}, 1, TimeUnit.SECONDS);
|
}, 1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
executorService.scheduleAtFixedRate(new Runnable() {
|
executorService.scheduleAtFixedRate(() -> {
|
||||||
@Override
|
// Task
|
||||||
public void run() {
|
|
||||||
// task details
|
|
||||||
}
|
|
||||||
}, 1, 10, TimeUnit.SECONDS);
|
}, 1, 10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
executorService.scheduleWithFixedDelay(new Runnable() {
|
executorService.scheduleWithFixedDelay(() -> {
|
||||||
@Override
|
// Task
|
||||||
public void run() {
|
|
||||||
// task details
|
|
||||||
}
|
|
||||||
}, 1, 10, TimeUnit.SECONDS);
|
}, 1, 10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
Future<String> future = executorService.schedule(new Callable<String>() {
|
Future<String> future = executorService.schedule(() -> {
|
||||||
@Override
|
// Task
|
||||||
public String call() throws Exception {
|
return "Hellow world";
|
||||||
return "Hello World";
|
|
||||||
}
|
|
||||||
}, 1, TimeUnit.SECONDS);
|
}, 1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
|
|
|
@ -5,11 +5,9 @@ import java.util.concurrent.CyclicBarrier;
|
||||||
public class CyclicBarrierExample {
|
public class CyclicBarrierExample {
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
|
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
|
||||||
@Override
|
// Task
|
||||||
public void run() {
|
|
||||||
System.out.println("All previous tasks are completed");
|
System.out.println("All previous tasks are completed");
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Thread t1 = new Thread(new Task(cyclicBarrier), "T1");
|
Thread t1 = new Thread(new Task(cyclicBarrier), "T1");
|
||||||
|
|
|
@ -6,11 +6,8 @@ public class ExecutorDemo {
|
||||||
|
|
||||||
public void execute() {
|
public void execute() {
|
||||||
Executor executor = new Invoker();
|
Executor executor = new Invoker();
|
||||||
executor.execute(new Runnable() {
|
executor.execute(()->{
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
// task to be performed
|
// task to be performed
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,15 +10,10 @@ public class ExecutorServiceDemo {
|
||||||
|
|
||||||
public void execute() {
|
public void execute() {
|
||||||
|
|
||||||
executor.execute(new Runnable() {
|
executor.submit(() -> {
|
||||||
@Override
|
new Task();
|
||||||
public void run() {
|
|
||||||
// task details
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
executor.submit(new Task());
|
|
||||||
|
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
executor.shutdownNow();
|
executor.shutdownNow();
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.baeldung.concurrent.future;
|
package com.baeldung.concurrent.future;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
@ -16,12 +15,10 @@ public class FutureDemo {
|
||||||
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(10);
|
ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||||
|
|
||||||
Future<String> future = executorService.submit(new Callable<String>() {
|
Future<String> future = executorService.submit(() -> {
|
||||||
@Override
|
// Task
|
||||||
public String call() throws Exception {
|
|
||||||
Thread.sleep(10000l);
|
Thread.sleep(10000l);
|
||||||
return "Hello World";
|
return "Hellow world";
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
future.cancel(false);
|
future.cancel(false);
|
||||||
|
|
Loading…
Reference in New Issue