Applied formatter

This commit is contained in:
sahil.singla 2020-09-22 22:18:17 +05:30
parent 213d5cfe39
commit 27953f3bb1

View File

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