From: Gustavo Martin Morcuende Date: Wed, 26 Jun 2013 23:51:21 +0000 (+0200) Subject: A few improvements in tests about Java Executors X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=d509d1c0b1439435fcd7a274abf118e401aa657b;p=JavaForFun A few improvements in tests about Java Executors --- diff --git a/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadTest.java b/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadTest.java index 82af00a..c83c736 100644 --- a/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadTest.java +++ b/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadTest.java @@ -4,73 +4,80 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadTest { - private final ExecutorService exec = Executors.newFixedThreadPool(2); + private final ExecutorService exec = Executors.newFixedThreadPool(2); - public void start () { - - //We have a pool with 2 threads. - //The third task will wait in a Queue until one thread of that pool is freed. - for (int i = 0; i < 3 ; i++) { - exec.execute(new ThreadExecutor(i)); - } - - try { - synchronized(this) { - wait(10000); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - - //We have a pool with 2 threads. - //The third task will wait in a Queue until one thread of that pool is freed. - for (int i = 0; i < 3 ; i++) { - exec.execute(new ThreadExecutor(i)); - } - - try { - synchronized(this) { - wait(5000); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - - exec.shutdown(); - } - - private class ThreadExecutor implements Runnable { - int i; + public void start () { - private ThreadExecutor(int i) { - this.i=i; - } - - @Override - public void run() { - //With execute method from Executors you can not use ExceptionHandler. - //The only way to catch unexpected exceptions is using try/catch RuntimeException in your code. - //Another way is using submit method instead of execute method. There is an example with submite method - //called ExecutorwithFuture - //Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); - try { - try { - synchronized(this) { - wait(2000); - } - } catch (InterruptedException e) { - e.printStackTrace(); - } - throw new RuntimeException("EXCEPTION: " + i); - } catch (RuntimeException e) { - //We catch unexpected exceptions with this code. - //From Effective Java Item 58: all of the unchecked throwables - //you implement should subclass RuntimeException (directly or indirectly). - //Code throwing unexpected exceptions not being a RuntimeException subclass is a flawed code - //or at least the API should have a nice Javadoc about that flaw. - System.out.println(Thread.currentThread().getName() + "\n"); - e.printStackTrace(); - } - } - } + //We have a pool with 2 threads. + //The third task will wait in a Queue until one thread of that pool is freed. + for (int i = 0; i < 3 ; i++) { + exec.execute(new ThreadExecutor(i)); + } + + try { + synchronized(this) { + wait(10000); + } + } catch (final InterruptedException e) { + e.printStackTrace(); + } + + //We have a pool with 2 threads. + //The third task will wait in a Queue until one thread of that pool is freed. + for (int i = 0; i < 3 ; i++) { + exec.execute(new ThreadExecutor(i)); + } + + try { + synchronized(this) { + wait(5000); + } + } catch (final InterruptedException e) { + e.printStackTrace(); + } + + exec.shutdown(); + + // After exec.shutdown if we try to execute more tasks a RejectedExecutionException + // exception will be thrown to the main thread. + System.out.println("Going to receive a RejectedExecutionException"); + for (int i = 0; i < 3; i++) { + exec.execute(new ThreadExecutor(i)); + } + } + + private class ThreadExecutor implements Runnable { + int i; + + private ThreadExecutor(final int i) { + this.i=i; + } + + @Override + public void run() { + //With execute method from Executors you can not use ExceptionHandler. + //The only way to catch unexpected exceptions is using try/catch RuntimeException in your code. + //Another way is using submit method instead of execute method. There is an example with submite method + //called ExecutorwithFuture + //Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); + try { + try { + synchronized(this) { + wait(2000); + } + } catch (final InterruptedException e) { + e.printStackTrace(); + } + throw new RuntimeException("EXCEPTION: " + i); + } catch (final RuntimeException e) { + //We catch unexpected exceptions with this code. + //From Effective Java Item 58: all of the unchecked throwables + //you implement should subclass RuntimeException (directly or indirectly). + //Code throwing unexpected exceptions not being a RuntimeException subclass is a flawed code + //or at least the API should have a nice Javadoc about that flaw. + System.out.println(Thread.currentThread().getName() + "\n"); + e.printStackTrace(); + } + } + } } diff --git a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadTest.java b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadTest.java index 473ca47..9183b63 100644 --- a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadTest.java +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadTest.java @@ -6,72 +6,80 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; public class ThreadTest { - private final ExecutorService exec = Executors.newFixedThreadPool(2); + private final ExecutorService exec = Executors.newFixedThreadPool(2); - public void start () { - Future [] future = new Future [3]; + public void start () { + final Future [] future = new Future [3]; - Thread.currentThread().setName("main"); - //We have a pool with 2 threads. - //The third task will wait in a Queue until one thread of that pool is freed. - for (int i = 0; i < 3 ; i++) { - future[i] = exec.submit(new ThreadExecutor(i)); - } - - for (int i = 0; i < 3 ; i++) { - try { - future[i].get(); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - //The exception thrown in the threads is caught by the main thread here. - System.out.println(Thread.currentThread().getName() + "\n"); - } - } - - try { - synchronized(this) { - wait(4000); - } - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - //We have a pool with 2 threads. - //The third task will wait in a Queue until one thread of that pool is freed. - for (int i = 0; i < 3 ; i++) { - //Code without implementing the Future class. The exception from the threads is not treated. - exec.submit(new ThreadExecutor(i)); - } - - exec.shutdown(); - } - - private class ThreadExecutor implements Runnable { - int i; + Thread.currentThread().setName("main"); + //We have a pool with 2 threads. + //The third task will wait in a Queue until one thread of that pool is freed. + for (int i = 0; i < 3 ; i++) { + future[i] = exec.submit(new ThreadExecutor(i)); + } - private ThreadExecutor(int i) { - this.i=i; - } - @Override - public void run() { - //With submit method from Executors you can not use ExceptionHandler. - //Anyway you do not need it, because using the submit method instead of execute method the exception - //from the threads is caught by the Executors implementation and if you have a Future Task you can - //treat it in you main thread. - //Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); - try { - synchronized(this) { - wait(4000); - } - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - System.out.println(Thread.currentThread().getName() + " number: " + i + "\n"); - throw new RuntimeException("EXCEPTION: " + i); - } - } + for (int i = 0; i < 3 ; i++) { + try { + future[i].get(); + } catch (final InterruptedException e) { + e.printStackTrace(); + } catch (final ExecutionException e) { + e.printStackTrace(); + //The exception thrown in the threads is caught by the main thread here. + System.out.println("Exception from task " + i + ": " + + Thread.currentThread().getName() + "\n"); + } + } + + try { + synchronized(this) { + wait(4000); + } + } catch (final InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + //We have a pool with 2 threads. + //The third task will wait in a Queue until one thread of that pool is freed. + for (int i = 0; i < 3 ; i++) { + //Code without implementing the Future class. The exception from the threads is not treated. + exec.submit(new ThreadExecutor(i)); + } + + exec.shutdown(); + + // After exec.shutdown if we try to execute more tasks a RejectedExecutionException + // exception will be thrown to the main thread. + System.out.println("Going to receive a RejectedExecutionException"); + for (int i = 0; i < 3; i++) { + exec.execute(new ThreadExecutor(i)); + } + } + + private class ThreadExecutor implements Runnable { + int i; + + private ThreadExecutor(final int i) { + this.i=i; + } + @Override + public void run() { + //With submit method from Executors you can not use ExceptionHandler. + //Anyway you do not need it, because using the submit method instead of execute method the exception + //from the threads is caught by the Executors implementation and if you have a Future Task you can + //treat it in you main thread. + //Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); + try { + synchronized(this) { + wait(4000); + } + } catch (final InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(Thread.currentThread().getName() + " number: " + i + "\n"); + throw new RuntimeException("EXCEPTION: " + i); + } + } }