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