BAEL-2346: modifying concurrency code to add more examples (#5843)
This commit is contained in:
parent
a370f6e781
commit
c4d92d17b5
|
@ -1,34 +1,52 @@
|
||||||
package com.baeldung.concurrent.Scheduledexecutorservice;
|
package com.baeldung.concurrent.Scheduledexecutorservice;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class ScheduledExecutorServiceDemo {
|
public class ScheduledExecutorServiceDemo {
|
||||||
|
|
||||||
public void execute() {
|
private void execute() {
|
||||||
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
getTasksToRun().apply(executorService);
|
||||||
ScheduledFuture<?> scheduledFuture = executorService.schedule(() -> {
|
|
||||||
// Task
|
|
||||||
}, 1, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
executorService.scheduleAtFixedRate(() -> {
|
|
||||||
// Task
|
|
||||||
}, 1, 10, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
executorService.scheduleWithFixedDelay(() -> {
|
|
||||||
// Task
|
|
||||||
}, 1, 10, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
Future<String> future = executorService.schedule(() -> {
|
|
||||||
// Task
|
|
||||||
return "Hellow world";
|
|
||||||
}, 1, TimeUnit.SECONDS);
|
|
||||||
|
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void executeWithMultiThread() {
|
||||||
|
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
|
||||||
|
getTasksToRun().apply(executorService);
|
||||||
|
executorService.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Function<ScheduledExecutorService, Void> getTasksToRun() {
|
||||||
|
return (executorService -> {
|
||||||
|
ScheduledFuture<?> scheduledFuture1 = executorService.schedule(() -> {
|
||||||
|
// Task
|
||||||
|
}, 1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
ScheduledFuture<?> scheduledFuture2 = executorService.scheduleAtFixedRate(() -> {
|
||||||
|
// Task
|
||||||
|
}, 1, 10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
ScheduledFuture<?> scheduledFuture3 = executorService.scheduleWithFixedDelay(() -> {
|
||||||
|
// Task
|
||||||
|
}, 1, 10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
ScheduledFuture<String> scheduledFuture4 = executorService.schedule(() -> {
|
||||||
|
// Task
|
||||||
|
return "Hellow world";
|
||||||
|
}, 1, TimeUnit.SECONDS);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String... args) {
|
||||||
|
ScheduledExecutorServiceDemo demo = new ScheduledExecutorServiceDemo();
|
||||||
|
demo.execute();
|
||||||
|
demo.executeWithMultiThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.concurrent.daemon;
|
||||||
|
|
||||||
|
public class MultipleThreadsExample {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NewThread t1 = new NewThread();
|
||||||
|
t1.setName("MyThread-1");
|
||||||
|
NewThread t2 = new NewThread();
|
||||||
|
t2.setName("MyThread-2");
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,18 @@
|
||||||
package com.baeldung.concurrent.daemon;
|
package com.baeldung.concurrent.daemon;
|
||||||
|
|
||||||
public class NewThread extends Thread {
|
public class NewThread extends Thread {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
while (true) {
|
while (true) {
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
System.out.println("New Thread is running..." + i);
|
System.out.println(this.getName() + ": New Thread is running..." + i);
|
||||||
|
try {
|
||||||
|
//Wait for one sec so it doesn't print too fast
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent the Thread to run forever. It will finish it's execution after 2 seconds
|
// prevent the Thread to run forever. It will finish it's execution after 2 seconds
|
||||||
if (System.currentTimeMillis() - startTime > 2000) {
|
if (System.currentTimeMillis() - startTime > 2000) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.concurrent.daemon;
|
||||||
|
|
||||||
|
public class SingleThreadExample {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NewThread t = new NewThread();
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue