From 5d44d831aad45cd5fbe6f3822d65ba776d2ece3d Mon Sep 17 00:00:00 2001 From: gumartinm Date: Sun, 1 Jan 2012 10:45:12 +0100 Subject: [PATCH] Examples with Executors (Java Concurrent library) --- .../test/thread/executor/execute/ThreadMain.java | 13 ++++ .../test/thread/executor/execute/ThreadTest.java | 76 +++++++++++++++++++++ .../de/test/thread/executor/future/ThreadMain.java | 11 ++++ .../de/test/thread/executor/future/ThreadTest.java | 77 ++++++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadMain.java create mode 100644 Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadTest.java create mode 100644 Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadMain.java create mode 100644 Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadTest.java diff --git a/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadMain.java b/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadMain.java new file mode 100644 index 0000000..a5042f3 --- /dev/null +++ b/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadMain.java @@ -0,0 +1,13 @@ +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(); + } +} 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 new file mode 100644 index 0000000..82af00a --- /dev/null +++ b/Allgemeines/Threads/Executor/ExecuteMethod/src/de/test/thread/executor/execute/ThreadTest.java @@ -0,0 +1,76 @@ +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(); + } + } + } +} diff --git a/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadMain.java b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadMain.java new file mode 100644 index 0000000..b85f5d7 --- /dev/null +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadMain.java @@ -0,0 +1,11 @@ +package de.test.thread.executor.future; + + +public class ThreadMain { + + public static void main(String[] args) { + ThreadTest test = new ThreadTest(); + + test.start(); + } +} 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 new file mode 100644 index 0000000..473ca47 --- /dev/null +++ b/Allgemeines/Threads/Executor/ExecutorwithFuture/src/de/test/thread/executor/future/ThreadTest.java @@ -0,0 +1,77 @@ +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); + } + } +} -- 2.1.4