--- /dev/null
+package de.test.thread.executor.execute;
+
+import de.test.thread.executor.execute.ThreadTest;
+
+
+public class ThreadMain {
+
+ public static void main(String[] args) {
+ ThreadTest test = new ThreadTest();
+
+ test.start();
+ }
+}
--- /dev/null
+package de.test.thread.executor.execute;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class ThreadTest {
+ 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;
+
+ 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();
+ }
+ }
+ }
+}
--- /dev/null
+package de.test.thread.executor.future;
+
+
+public class ThreadMain {
+
+ public static void main(String[] args) {
+ ThreadTest test = new ThreadTest();
+
+ test.start();
+ }
+}
--- /dev/null
+package de.test.thread.executor.future;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+public class ThreadTest {
+ private final ExecutorService exec = Executors.newFixedThreadPool(2);
+
+ public void start () {
+ 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;
+
+ 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);
+ }
+ }
+}