Skip to content

Commit 8f10a7b

Browse files
authored
BAEL-4559 Stop Execution After Certain Time improvements (eugenp#12076)
1 parent b748dc7 commit 8f10a7b

10 files changed

Lines changed: 303 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
public class FixedTimeTask implements Runnable {
9+
10+
private static final Logger LOG = LoggerFactory.getLogger(FixedTimeTask.class);
11+
12+
final int fixedTime; // milliseconds
13+
14+
public FixedTimeTask(int fixedTime) {
15+
this.fixedTime = fixedTime;
16+
}
17+
18+
@Override
19+
public void run() {
20+
LOG.info(fixedTime + " milliseconds running task");
21+
try {
22+
TimeUnit.MILLISECONDS.sleep(fixedTime);
23+
} catch (InterruptedException e) {
24+
LOG.info("interrupted");
25+
}
26+
LOG.info("finished");
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class LongRunningTask implements Runnable {
7+
8+
private static final Logger LOG = LoggerFactory.getLogger(LongRunningTask.class);
9+
10+
@Override
11+
public void run() {
12+
LOG.info("running");
13+
for (int i = 0; i < Long.MAX_VALUE; i++) {
14+
if (Thread.interrupted()) {
15+
LOG.info("stopping");
16+
return;
17+
}
18+
}
19+
LOG.info("finished");
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import java.util.Random;
4+
5+
public class Step {
6+
7+
private static int MAX = Integer.MAX_VALUE / 2;
8+
int number;
9+
10+
public Step(int number) {
11+
this.number = number;
12+
}
13+
14+
public void perform() throws InterruptedException {
15+
Random rnd = new Random();
16+
int target = rnd.nextInt(MAX);
17+
while (rnd.nextInt(MAX) != target) {
18+
if (Thread.interrupted()) {
19+
throw new InterruptedException();
20+
}
21+
}
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.List;
7+
8+
public class SteppedTask implements Runnable {
9+
private static final Logger LOG = LoggerFactory.getLogger(SteppedTask.class);
10+
11+
private List<Step> steps;
12+
13+
public SteppedTask(List<Step> steps) {
14+
this.steps = steps;
15+
}
16+
17+
@Override
18+
public void run() {
19+
LOG.info("running stepped process");
20+
for (Step step : steps) {
21+
LOG.info("running step " + step.number);
22+
try {
23+
step.perform();
24+
} catch (InterruptedException e) {
25+
LOG.info("interrupting task");
26+
return;
27+
}
28+
}
29+
LOG.info("stepped process finished");
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.List;
7+
import java.util.Timer;
8+
import java.util.concurrent.ExecutorService;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.Future;
11+
import java.util.concurrent.ScheduledExecutorService;
12+
import java.util.concurrent.TimeUnit;
13+
import java.util.concurrent.TimeoutException;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
17+
public class StoppingExecution {
18+
19+
private static final Logger LOG = LoggerFactory.getLogger(StoppingExecution.class);
20+
21+
public static void main(String[] args) {
22+
StoppingExecution.testUsingLoop();
23+
StoppingExecution.testUsingTimer();
24+
StoppingExecution.testUsingFuture();
25+
StoppingExecution.testScheduledExecutor();
26+
StoppingExecution.testSteppedProcess();
27+
}
28+
29+
public static void testUsingLoop() {
30+
LOG.info("using loop started");
31+
long start = System.currentTimeMillis();
32+
long end = start + 30 * 1000; // 30 seconds
33+
while (System.currentTimeMillis() < end) {
34+
LOG.info("running task");
35+
new FixedTimeTask(7 * 1000).run(); // 7 seconds
36+
}
37+
LOG.info("using loop ended");
38+
}
39+
40+
public static void testUsingTimer() {
41+
LOG.info("using timer started");
42+
Thread thread = new Thread(new LongRunningTask());
43+
thread.start();
44+
45+
Timer timer = new Timer();
46+
TimeOutTask timeOutTask = new TimeOutTask(thread, timer);
47+
48+
LOG.info("scheduling timeout in 3 seconds");
49+
timer.schedule(timeOutTask, 3000);
50+
LOG.info("using timer ended");
51+
}
52+
53+
public static void testUsingFuture() {
54+
LOG.info("using future started");
55+
ExecutorService executor = Executors.newSingleThreadExecutor();
56+
Future future = executor.submit(new LongRunningTask());
57+
try {
58+
LOG.info("future get with 7 seconds timeout");
59+
future.get(7, TimeUnit.SECONDS);
60+
} catch (TimeoutException e) {
61+
LOG.info("future timeout");
62+
future.cancel(true);
63+
} catch (Exception e) {
64+
LOG.info("future exception", e);
65+
} finally {
66+
executor.shutdownNow();
67+
}
68+
LOG.info("using future ended");
69+
}
70+
71+
public static void testScheduledExecutor() {
72+
LOG.info("using future schedule executor started");
73+
74+
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
75+
Future future = executor.submit(new LongRunningTask());
76+
Runnable cancelTask = () -> future.cancel(true);
77+
78+
LOG.info("cancel task in 3 seconds");
79+
executor.schedule(cancelTask, 3000, TimeUnit.MILLISECONDS);
80+
executor.shutdown();
81+
LOG.info("using future schedule executor ended");
82+
}
83+
84+
public static void testSteppedProcess() {
85+
List<Step> steps = Stream.of(new Step(1), new Step(2), new Step(3), new Step(4)).collect(Collectors.toList());
86+
87+
LOG.info("stepped process started");
88+
Thread thread = new Thread(new SteppedTask(steps));
89+
thread.start();
90+
91+
Timer timer = new Timer();
92+
TimeOutTask timeOutTask = new TimeOutTask(thread, timer);
93+
timer.schedule(timeOutTask, 10000);
94+
}
95+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import java.util.Timer;
4+
import java.util.TimerTask;
5+
6+
public class TimeOutTask extends TimerTask {
7+
private Thread thread;
8+
private Timer timer;
9+
10+
public TimeOutTask(Thread thread, Timer timer) {
11+
this.thread = thread;
12+
this.timer = timer;
13+
}
14+
15+
@Override
16+
public void run() {
17+
if (thread != null && thread.isAlive()) {
18+
thread.interrupt();
19+
timer.cancel();
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class FixedTimeTaskUnitTest {
8+
9+
@Test
10+
public void run() throws InterruptedException {
11+
long start = System.currentTimeMillis();
12+
Thread thread = new Thread(new FixedTimeTask(10));
13+
thread.start();
14+
thread.join();
15+
long end = System.currentTimeMillis();
16+
assertTrue(end - start >= 10);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class LongRunningTaskUnitTest {
9+
10+
@Test
11+
public void run() {
12+
Thread thread = new Thread(new LongRunningTask());
13+
thread.start();
14+
assertTrue(thread.isAlive());
15+
16+
thread.interrupt();
17+
assertTrue(thread.isInterrupted());
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class SteppedTaskUnitTest {
12+
13+
@Test
14+
public void run() throws InterruptedException {
15+
List<Step> steps = Stream.of(
16+
new Step(1),
17+
new Step(2),
18+
new Step(3))
19+
.collect(Collectors.toList());
20+
21+
Thread thread = new Thread(new SteppedTask(steps));
22+
thread.start();
23+
thread.interrupt();
24+
thread.join();
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.concurrent.stopexecution;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Timer;
6+
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class TimeOutTaskUnitTest {
10+
11+
@Test
12+
public void run() {
13+
Thread thread = new Thread(new LongRunningTask());
14+
Timer timer = new Timer();
15+
TimeOutTask timeOutTask = new TimeOutTask(thread, timer);
16+
thread.start();
17+
timeOutTask.run();
18+
assertTrue(thread.isInterrupted());
19+
}
20+
}

0 commit comments

Comments
 (0)