BAEL-4558: Article for stopping execution after a certain time
This commit is contained in:
parent
20a6e042f2
commit
213d5cfe39
@ -1,4 +1,268 @@
|
|||||||
package com.baeldung.concurrent.stopexecution;
|
package com.baeldung.concurrent.stopexecution;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class StopExecution {
|
public class StopExecution {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(StopExecution.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
StopExecution stopExecution = new StopExecution();
|
||||||
|
//stopExecution.testUsingLoop();
|
||||||
|
//stopExecution.testTimer();
|
||||||
|
stopExecution.testScheduledExecutor();
|
||||||
|
LOG.info("done");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testUsingLoop(){
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
long end = start + 5000;
|
||||||
|
List<String> items = new ArrayList<>();
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
// Let this loop run only upto 5 seconds
|
||||||
|
while (System.currentTimeMillis() < end && counter < items.size())
|
||||||
|
{
|
||||||
|
// Fetch the item from the list.
|
||||||
|
// Some expensive operation on the item.
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testThreads(){
|
||||||
|
Thread thread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
LOG.info("inside run");
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(10000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
LOG.info("exit run");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
while (thread.getState() != Thread.State.TERMINATED){
|
||||||
|
LOG.info(thread.getState().name());
|
||||||
|
try {
|
||||||
|
Thread.sleep(500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void testExecutor(){
|
||||||
|
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||||
|
Future<String> f = null;
|
||||||
|
try {
|
||||||
|
f = service.submit(() -> {
|
||||||
|
// Do you long running calculation here
|
||||||
|
try {
|
||||||
|
Thread.sleep(2737); // Simulate some delay
|
||||||
|
}
|
||||||
|
catch (InterruptedException e){
|
||||||
|
LOG.info("Interrupted");
|
||||||
|
return "interrupted";
|
||||||
|
}
|
||||||
|
LOG.info("Sleep finished");
|
||||||
|
return "42";
|
||||||
|
});
|
||||||
|
|
||||||
|
LOG.info(f.get(2, TimeUnit.SECONDS));
|
||||||
|
} catch (final TimeoutException e) {
|
||||||
|
f.cancel(true);
|
||||||
|
LOG.error("Calculation took to long");
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
service.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testExecutor2(){
|
||||||
|
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||||
|
Future f = null;
|
||||||
|
try {
|
||||||
|
f = service.submit(new LongRunningTask());
|
||||||
|
LOG.info("testExecutor2");
|
||||||
|
f.get(1, TimeUnit.SECONDS);
|
||||||
|
} catch (final TimeoutException e) {
|
||||||
|
f.cancel(true);
|
||||||
|
LOG.error("Calculation took to long");
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
service.shutdownNow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testScheduledExecutor(){
|
||||||
|
LOG.info("testScheduledExecutor");
|
||||||
|
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
|
||||||
|
Future future = executor.submit(new LongRunningTask());
|
||||||
|
executor.schedule(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
future.cancel(true);
|
||||||
|
}
|
||||||
|
}, 1000, TimeUnit.MILLISECONDS);
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
public void testThreadAndInterrupt(){
|
||||||
|
|
||||||
|
Thread t;
|
||||||
|
try {
|
||||||
|
t = new Thread(new LongRunningTask());
|
||||||
|
|
||||||
|
LOG.info("testExecutor3");
|
||||||
|
long end = System.currentTimeMillis() + 2000;
|
||||||
|
t.start();
|
||||||
|
while (t.isAlive() && System.currentTimeMillis() < end){
|
||||||
|
Thread.sleep(50);
|
||||||
|
}
|
||||||
|
t.interrupt();
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void testTimer(){
|
||||||
|
LOG.info("Timer test");
|
||||||
|
Thread t = new Thread(new LongRunningTask());
|
||||||
|
Timer timeoutTimer = new Timer();
|
||||||
|
timeoutTimer.schedule(new TimeOutTask(t, timeoutTimer), 1000);
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyRunnableTask implements Runnable{
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
LOG.info("MyRunnable...");
|
||||||
|
Thread.sleep(10000);
|
||||||
|
}
|
||||||
|
catch (InterruptedException ie)
|
||||||
|
{
|
||||||
|
LOG.info("MyRunnable interrupted...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TimeOutTask extends TimerTask {
|
||||||
|
private Thread t;
|
||||||
|
private Timer timer;
|
||||||
|
|
||||||
|
TimeOutTask(Thread t, Timer timer){
|
||||||
|
this.t = t;
|
||||||
|
this.timer = timer;
|
||||||
|
}
|
||||||
|
public void run() {
|
||||||
|
if (t != null && t.isAlive()) {
|
||||||
|
t.interrupt();
|
||||||
|
timer.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LongRunningTask implements Runnable{
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
longRunningSort();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void longRunningOperation(){
|
||||||
|
LOG.info("long Running operation started");
|
||||||
|
|
||||||
|
try {
|
||||||
|
//Thread.sleep(500);
|
||||||
|
longFileRead();
|
||||||
|
LOG.info("long running operation finished");
|
||||||
|
}
|
||||||
|
catch (InterruptedException e){
|
||||||
|
LOG.info("long Running operation interrupted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void longRunningSort(){
|
||||||
|
LOG.info("long Running task started");
|
||||||
|
// Do you long running calculation here
|
||||||
|
int len = 100000;
|
||||||
|
List<Integer> numbers = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
for (int i = len; i > 0; i--) {
|
||||||
|
//Thread.sleep(5)
|
||||||
|
numbers.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
int minIndex = i;
|
||||||
|
for (int j = i + 1; j < len; j++) {
|
||||||
|
if (numbers.get(minIndex) > numbers.get(j))
|
||||||
|
minIndex = j;
|
||||||
|
}
|
||||||
|
if (minIndex != i) {
|
||||||
|
int temp = numbers.get(i);
|
||||||
|
numbers.set(i, numbers.get(minIndex));
|
||||||
|
numbers.set(minIndex, temp);
|
||||||
|
}
|
||||||
|
throwExceptionOnThreadInterrupt();
|
||||||
|
}
|
||||||
|
LOG.info("Index position: " + i);
|
||||||
|
LOG.info("Long running task finished");
|
||||||
|
}catch (InterruptedException e){
|
||||||
|
LOG.info("long Running operation interrupted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void longFileRead() throws InterruptedException{
|
||||||
|
String file = "input.txt";
|
||||||
|
ClassLoader classloader = getClass().getClassLoader();
|
||||||
|
|
||||||
|
try (InputStream inputStream = classloader.getResourceAsStream(file)){
|
||||||
|
Reader inputStreamReader = new InputStreamReader(inputStream);
|
||||||
|
|
||||||
|
int data = inputStreamReader.read();
|
||||||
|
while (data != -1) {
|
||||||
|
char theChar = (char) data;
|
||||||
|
data = inputStreamReader.read();
|
||||||
|
throwExceptionOnThreadInterrupt();
|
||||||
|
}
|
||||||
|
} catch (IOException e){
|
||||||
|
LOG.error("Exception: ", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void throwExceptionOnThreadInterrupt() throws InterruptedException{
|
||||||
|
if (Thread.currentThread().interrupted()){
|
||||||
|
throw new InterruptedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user