Applied formatter
This commit is contained in:
parent
213d5cfe39
commit
27953f3bb1
@ -29,7 +29,6 @@ public class StopExecution {
|
||||
LOG.info("done");
|
||||
}
|
||||
|
||||
|
||||
public void testUsingLoop() {
|
||||
long start = System.currentTimeMillis();
|
||||
long end = start + 5000;
|
||||
@ -37,8 +36,7 @@ public class StopExecution {
|
||||
int counter = 0;
|
||||
|
||||
// Let this loop run only upto 5 seconds
|
||||
while (System.currentTimeMillis() < end && counter < items.size())
|
||||
{
|
||||
while (System.currentTimeMillis() < end && counter < items.size()) {
|
||||
// Fetch the item from the list.
|
||||
// Some expensive operation on the item.
|
||||
try {
|
||||
@ -74,6 +72,7 @@ public class StopExecution {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void testExecutor() {
|
||||
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||
Future<String> f = null;
|
||||
@ -82,8 +81,7 @@ public class StopExecution {
|
||||
// Do you long running calculation here
|
||||
try {
|
||||
Thread.sleep(2737); // Simulate some delay
|
||||
}
|
||||
catch (InterruptedException e){
|
||||
} catch (InterruptedException e) {
|
||||
LOG.info("Interrupted");
|
||||
return "interrupted";
|
||||
}
|
||||
@ -92,17 +90,16 @@ public class StopExecution {
|
||||
});
|
||||
|
||||
LOG.info(f.get(2, TimeUnit.SECONDS));
|
||||
} catch (final TimeoutException e) {
|
||||
} catch (TimeoutException e) {
|
||||
f.cancel(true);
|
||||
LOG.error("Calculation took to long");
|
||||
} catch (final Exception e) {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testExecutor2() {
|
||||
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||
Future f = null;
|
||||
@ -131,6 +128,7 @@ public class StopExecution {
|
||||
}, 1000, TimeUnit.MILLISECONDS);
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
public void testThreadAndInterrupt() {
|
||||
|
||||
Thread t;
|
||||
@ -148,6 +146,7 @@ public class StopExecution {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void testTimer() {
|
||||
LOG.info("Timer test");
|
||||
Thread t = new Thread(new LongRunningTask());
|
||||
@ -157,21 +156,16 @@ public class StopExecution {
|
||||
}
|
||||
|
||||
class MyRunnableTask implements Runnable {
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
public void run() {
|
||||
try {
|
||||
LOG.info("MyRunnable...");
|
||||
Thread.sleep(10000);
|
||||
}
|
||||
catch (InterruptedException ie)
|
||||
{
|
||||
} catch (InterruptedException ie) {
|
||||
LOG.info("MyRunnable interrupted...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TimeOutTask extends TimerTask {
|
||||
private Thread t;
|
||||
private Timer timer;
|
||||
@ -180,6 +174,7 @@ public class StopExecution {
|
||||
this.t = t;
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (t != null && t.isAlive()) {
|
||||
t.interrupt();
|
||||
@ -201,8 +196,7 @@ public class StopExecution {
|
||||
//Thread.sleep(500);
|
||||
longFileRead();
|
||||
LOG.info("long running operation finished");
|
||||
}
|
||||
catch (InterruptedException e){
|
||||
} catch (InterruptedException e) {
|
||||
LOG.info("long Running operation interrupted");
|
||||
}
|
||||
}
|
||||
@ -256,6 +250,7 @@ public class StopExecution {
|
||||
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