Applied formatter
This commit is contained in:
parent
213d5cfe39
commit
27953f3bb1
|
@ -29,16 +29,14 @@ public class StopExecution {
|
|||
LOG.info("done");
|
||||
}
|
||||
|
||||
|
||||
public void testUsingLoop(){
|
||||
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())
|
||||
{
|
||||
while (System.currentTimeMillis() < end && counter < items.size()) {
|
||||
// Fetch the item from the list.
|
||||
// Some expensive operation on the item.
|
||||
try {
|
||||
|
@ -50,7 +48,7 @@ public class StopExecution {
|
|||
}
|
||||
}
|
||||
|
||||
public static void testThreads(){
|
||||
public static void testThreads() {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -65,7 +63,7 @@ public class StopExecution {
|
|||
}
|
||||
});
|
||||
thread.start();
|
||||
while (thread.getState() != Thread.State.TERMINATED){
|
||||
while (thread.getState() != Thread.State.TERMINATED) {
|
||||
LOG.info(thread.getState().name());
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
|
@ -74,7 +72,8 @@ public class StopExecution {
|
|||
}
|
||||
}
|
||||
}
|
||||
public static void testExecutor(){
|
||||
|
||||
public static void testExecutor() {
|
||||
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||
Future<String> f = null;
|
||||
try {
|
||||
|
@ -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,18 +90,17 @@ 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(){
|
||||
public void testExecutor2() {
|
||||
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||
Future f = null;
|
||||
try {
|
||||
|
@ -120,18 +117,19 @@ public class StopExecution {
|
|||
}
|
||||
}
|
||||
|
||||
public void testScheduledExecutor(){
|
||||
public void testScheduledExecutor() {
|
||||
LOG.info("testScheduledExecutor");
|
||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
|
||||
Future future = executor.submit(new LongRunningTask());
|
||||
executor.schedule(new Runnable(){
|
||||
public void run(){
|
||||
executor.schedule(new Runnable() {
|
||||
public void run() {
|
||||
future.cancel(true);
|
||||
}
|
||||
}, 1000, TimeUnit.MILLISECONDS);
|
||||
executor.shutdown();
|
||||
}
|
||||
public void testThreadAndInterrupt(){
|
||||
|
||||
public void testThreadAndInterrupt() {
|
||||
|
||||
Thread t;
|
||||
try {
|
||||
|
@ -140,7 +138,7 @@ public class StopExecution {
|
|||
LOG.info("testExecutor3");
|
||||
long end = System.currentTimeMillis() + 2000;
|
||||
t.start();
|
||||
while (t.isAlive() && System.currentTimeMillis() < end){
|
||||
while (t.isAlive() && System.currentTimeMillis() < end) {
|
||||
Thread.sleep(50);
|
||||
}
|
||||
t.interrupt();
|
||||
|
@ -148,7 +146,8 @@ public class StopExecution {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public void testTimer(){
|
||||
|
||||
public void testTimer() {
|
||||
LOG.info("Timer test");
|
||||
Thread t = new Thread(new LongRunningTask());
|
||||
Timer timeoutTimer = new Timer();
|
||||
|
@ -156,30 +155,26 @@ public class StopExecution {
|
|||
t.start();
|
||||
}
|
||||
|
||||
class MyRunnableTask implements Runnable{
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
class MyRunnableTask implements Runnable {
|
||||
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;
|
||||
|
||||
TimeOutTask(Thread t, Timer timer){
|
||||
TimeOutTask(Thread t, Timer timer) {
|
||||
this.t = t;
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (t != null && t.isAlive()) {
|
||||
t.interrupt();
|
||||
|
@ -188,26 +183,25 @@ public class StopExecution {
|
|||
}
|
||||
}
|
||||
|
||||
class LongRunningTask implements Runnable{
|
||||
class LongRunningTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
longRunningSort();
|
||||
}
|
||||
|
||||
private void longRunningOperation(){
|
||||
private void longRunningOperation() {
|
||||
LOG.info("long Running operation started");
|
||||
|
||||
try {
|
||||
//Thread.sleep(500);
|
||||
longFileRead();
|
||||
LOG.info("long running operation finished");
|
||||
}
|
||||
catch (InterruptedException e){
|
||||
} catch (InterruptedException e) {
|
||||
LOG.info("long Running operation interrupted");
|
||||
}
|
||||
}
|
||||
|
||||
private void longRunningSort(){
|
||||
private void longRunningSort() {
|
||||
LOG.info("long Running task started");
|
||||
// Do you long running calculation here
|
||||
int len = 100000;
|
||||
|
@ -234,16 +228,16 @@ public class StopExecution {
|
|||
}
|
||||
LOG.info("Index position: " + i);
|
||||
LOG.info("Long running task finished");
|
||||
}catch (InterruptedException e){
|
||||
} catch (InterruptedException e) {
|
||||
LOG.info("long Running operation interrupted");
|
||||
}
|
||||
}
|
||||
|
||||
private void longFileRead() throws InterruptedException{
|
||||
private void longFileRead() throws InterruptedException {
|
||||
String file = "input.txt";
|
||||
ClassLoader classloader = getClass().getClassLoader();
|
||||
|
||||
try (InputStream inputStream = classloader.getResourceAsStream(file)){
|
||||
try (InputStream inputStream = classloader.getResourceAsStream(file)) {
|
||||
Reader inputStreamReader = new InputStreamReader(inputStream);
|
||||
|
||||
int data = inputStreamReader.read();
|
||||
|
@ -252,12 +246,13 @@ public class StopExecution {
|
|||
data = inputStreamReader.read();
|
||||
throwExceptionOnThreadInterrupt();
|
||||
}
|
||||
} catch (IOException e){
|
||||
} catch (IOException e) {
|
||||
LOG.error("Exception: ", e);
|
||||
}
|
||||
}
|
||||
private void throwExceptionOnThreadInterrupt() throws InterruptedException{
|
||||
if (Thread.currentThread().interrupted()){
|
||||
|
||||
private void throwExceptionOnThreadInterrupt() throws InterruptedException {
|
||||
if (Thread.currentThread().interrupted()) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue