JavaOne 2013 San Francisco Asyn-ConcurrencyOnEE7
 

JavaOne 2013 San Francisco Asyn-ConcurrencyOnEE7

on

  • 1,423 views

 

Statistics

Views

Total Views
1,423
Views on SlideShare
1,422
Embed Views
1

Actions

Likes
2
Downloads
53
Comments
0

1 Embed 1

https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

JavaOne 2013 San Francisco Asyn-ConcurrencyOnEE7 JavaOne 2013 San Francisco Asyn-ConcurrencyOnEE7 Presentation Transcript

  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.1
  • [ CON10982 ] Implementation of Async and Concurrent Applications in the Java EE Environment Yoshio Terada Java Evangelist http://yoshio3.com
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3 If  you  have  some  questions, could  you  give  me  a  message   on  Twitter  with  following  hash   tag  ? [#CON10982] My  Twitter  ID  :  @yoshioterada
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle.
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.5 It is very easy to implement concurrent application on EE 7 environment. Customizable flexibly
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.6 Structure  of  My  Sesiosn Review  of   Async/Concurrency on  Java  SE  &  Java  EE6 Concurrency  Utilities  on Java  EE  7   15-‐‑‒20  Min With  Demo 40  Min With  Demo
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.7 History  of  Async   Application  in  Java Java  SE Environment
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.8 1996 1997 JDK1.0 Thread Runnable 2004 2006 JDK1. 1 J2SE1.2 J2SE  1.3 J2SE  1.4 Java  SE  5 JSR-‐‑‒166 Concurrency  Utilities Java  SE  6 JSR-‐‑‒166x Hisotry  of  Java  Thread 1998 2000 2002
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.9 2011 ・・・・・・・・・・ ・・・・・・・・ Java  SE  7 JSR-‐‑‒166y Fork/Join Java  SE  8 Lambda  Expression  &   JSR-‐‑‒166e Concurrency  Utilities 2014 History  of  Java  SE  Thread
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10 class MyWebServer{! public static void main(String argv[]){! ServerSocket socket = new ServerSocket(80);! while(true){! final Socket conn = socket.accept();! Runnable r = new Runnable(){! publiv void run(){ ! addConnQueue(conn);//Server opeartion   }};! new Thread(r).start();! }! } }! Ex  :  Impl  of  MultiThread  Web  Server Create  new   Thread  for   request No  more   Recommend  This  way
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.11 new   Thread(r).start(); Thread-‐‑‒1 Thread-‐‑‒2 Thread-‐‑‒3 Thread-‐‑‒n Generated  Thread  Indefinitely ・・・
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.12 Thread-‐‑‒1 Thread-‐‑‒2 Thread-‐‑‒3 Thread-‐‑‒n Ex  :  Stack  area  is  also  allocated スタック Thread-‐‑‒1  Stack Thread-‐‑‒2  Stack Thread-‐‑‒3  Stack Thread-‐‑‒n  Stack … … -‐‑‒Xss
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.13 Load  of  Java  VM  and  OS Memory  Consumption  for  each  thread UpperLimit  of  the  number  of  thread  creation Overhead  of  the  context  switch Disadvantage  of  unlimited  thread
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.14
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.15 Java  Concurrency  Utilities •  Easy  API  to  implement   concurrency  application •  Provide  simple  and  easy  API •  Scalability,  Performance,   Maintainability,  Thread  safe,  easy  to   understand
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.16 JSR-‐‑‒166  Overview •  Async  task  operation •  Concurrency  Collection •  lock,  synchronizer •  Atomic  operation
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.17 Useful  classes •  Executors,  Thread  Pool,  Futures   •  Collection:  Queues,  Blocking  Queues,  Concurrent   HashMap •  Lock,  synchronization:  Semaphores,  Barriers,  Atomic   variable •  Others  …
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18 public interface Executor{ ! void execute(Runnable command); ! } ! Executor  Interface Async  invocation
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19 public interface ExecutorService extends Executor{ ! void shutdown(); ! List<Runnable> shutdownNow(); ! boolean isShutdown(); ! boolean isTerminated(); ! boolean awaitTermination(long timeout,!     TimeUnit unit); ! <T> Future<T> submit(Callable<T> task) ! // and more …! } ! ExecutorService  Interface Lifecycle  management,  get  return  value
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20 static int CPU_NUM =! Runtime.getRuntime().availableProcessors();! ExecutorService pool = ! Executors.newFixedThreadPool(CPU_NUM);! public static void main(String argv[]){! ServerSocket socket = new ServerSocket(80);! while(true){! final Socket conn = socket.accept();! Runnable r = new Runnable(){! public void run(){;//do something      } };! pool.execute(r);! }}! WebServer    implemented  by  Executor
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.21 newFixedThreadPool ExecutorService execution using fixed Thread Pool LinkedBlockingQueue   (FIFO) ThreadFactory …T1 T2 T3 T4 Tn ExecutorService pool =!    Executors.newFixedThreadPool(CPU_NUM); pool.execute(r); •  Reuse  the  created  Thread •  Number  of  max  Thread •  Queing  of  incoming  request
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.22 Execute  Thread  more  Efficiently Possible  to  manage  the  lifecycle  of   Thread Benefit  of  Concurrency  Utilities
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.23 History  of  Async  on   Java  EE JMS(MDB) Async  Servlet Async  EJB
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.24 1998 2009 JPE J2EE  1.4 Java  EE  6 Async  Servlet Async  EJB History  of  Async  on  Java  EE 1999 2001 2003 Java  EE   5 2006 J2EE  1.2 JMS J2EE  1.3 MDB
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.25 JMS  &  MDB More  Easy  :  Java  EE  7
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.26 Message  Provider JNDI  Naming  Space Conn Factory jms/MyConFactory App  Server   Administrator Destinatio n jms/MyQueue Queue  Name Delegate  the  operation  to   External  Message  Provider
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.27 JNDI  Name  Space Conn Factory Destination JMS  Client Resource   Injection  jms/MyFactory  jms/MyQueue JMS  Client  connect  to  Message   Provider  via  JNDI  lookup Message  Provider Queue  NameDeveloper Developer  refer  to  the  JNDI
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.28 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28 @Stateless! public class MailAddressRegisterEJB {! @Resource(mappedName = "java:comp/JMSConFact")! ConnectionFactory conn;! @Resource(mappedName = "jms/mailRegistQueue")! Queue queue;! ! public void registEmailAddress(String address){! try(JMSContext context = conn.createContext()){!    context.createProducer().send(queue,  !                                address);}}} Send  :  JMS  2.0  (Java  EE  7)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.29 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.29 @MessageDriven(mappedName = "jms/mailRegistQueue")! public class SendMessageMDB implements MessageListener{! public SendMessageMDB(){}! @Inject MailSender mailSender;! @Override! public void onMessage(Message message) {! try {! TextMessage msg = (TextMessage) message;! mailSender.sendMessage(msg.getText());! } catch (JMSException jmse) {! jmse.printStackTrace();! }! }} Receive:  MDB  (Java  EE  7)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.30 Servlet  3.0  :  since  Java  EE  6 (asyncSupported  =  true)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.31 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.31 @WebServlet(name = "MailSenderServlet", ! urlPatterns = {"/MailSenderServlet"}, ! asyncSupported = true)! public class MailSenderServlet extends HttpServlet {! ! protected void processRequest(! HttpServletRequest request, ! HttpServletResponse response)! throws ServletException, IOException {! AsyncContext ac = request.startAsync();! ac.start(new MailSenderRunnable(ac));! }}! Servlet  3.0  :  Async  Servlet
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.32 EJB  3.1  :  since  Java  EE  6 @Asynchronous
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.33 @Stateless! public class SyncEmailSenderEJB {! @Inject! MailSender mailsend;! public void syncSendMessage(String email){! mailsend.sendMessage(email);! }! ! @Asynchronous! public void asyncSendMessage(String email){! mailsend.sendMessage(email);! }}! EJB  3.1  (Java  EE  6)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.34 Concurrency  Utilities   for  EE
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.35 Application  Servers Java  SE Web/EJB  Container EJB JSP Servlet Runnable Callable Other  Java  EE  functionality   (JAX-‐‑‒RS,JavaMail,  CDI,  etc) Not  recommended  to  create  Thread on  Java  EE  environment. Thread  run   outside  of  the   Container
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.36 Application  Servers Java  SE Web/EJB  Container EJB JSP Servlet Runnable Callable Concurrency  Architecture  on  EE  7 Other  Java  EE  function (JAX-‐‑‒RS,JavaMail,  CDI,  etc) ManagedExecutor  Service ManagedScheduledExecutorService ContextService ManagedThreadFactory Concurrency  Utilities  for  EE
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.37 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.37 Memo It  is  easy  to  implement  !! It  is  possible  to  Customize  !! Small  Package  :  Total  12  class
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.38 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.38 Usecase  senario? For  long  running  process Effective  use  of  hardware! For  executing  regularly
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.39 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.39 Usecase  senario Would  like  to  run  a  Task  on  Java  EE   environment Which  is  implemented  on  Java  SE   environment.
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.40 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.40 Important  Interfaces Best  4 • ManagedExecutorService • ManagedScheduledExecutorService • ManagedThreadFactory • ContextService
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.41 Easy  development   To  create  Async  Task (ManagedExecutorService)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.42 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.42 Memo Most easy way !!
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.43 Implements  some   Task A  implements  Runnable B  implements  Callable Implement  Async  Task Use  the  configured   managed  Thread  by   resource  Injection Configure  Server   side Possible  to  use  the   default  configuration 1 2 Step  to  create  Async  Task 3
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.44 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.44 public class MyRunnableTask implements Runnable {! @Override! public void run() { ! try {! Thread.sleep(10000); //do Something } catch (InterruptedException ex) {! logger.log(Level.SEVERE, null, ex);! }! }! }! Implement  Runnable  Task1
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.45 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.45 Implement  Callable  Task         ●  Possible  to  get  the  return  value         ●  Possible  to  throw  the  Exception 1 public class MyCallableTask implements ! Callable<String> {! @Override! public String call() throws Exception {! return “Hello World”;! }! }!
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.46 Configuration  on  Server  Side2
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.47 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.47 @Stateless! public class MyManagedExecutorService {! @Resource(name = ! "concurrent/DefaultManagedExecutorService")! ManagedExecutorService managedExecsvc; ! public void execExecutorService() {! MyRunnableTask task = new MyRunnableTask();! managedExecsvc.submit(task);!       MyCallableTask singleTask = !           new MyCallableTask("Foo Bar");! Future<String> singleFuture =    ! managedExecsvc.submit(singleTask);}! Exec  Async  Task  on  EJB3 Inject  the   resource
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.48 Scheduling  the  Async  Task (ManagedScheduledExecutorService)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.49 Implement  the  Task A  implements  Runnable B  implements  Callable Implement  Async  Task Use  the  configured   managed  Thread  by   resource  Injection Configure  on  Server   Side Possible  to  use  default   configuration 1 2 Scheduling  Async  Task  Impl 3
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.50 Server  Side  Configuration2
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.51 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.51 @Stateless! public class MyManagedScheduledExecutorService{! @Resource(name = "concurrent/! DefaultManagedScheduledExecutorService")! ManagedScheduledExecutorService managedScheduledExecsvc;! ! public void execScheduledExecutorService() {! MyRunnableTask task = new MyRunnableTask();! managedScheduledExecsvc.schedule(! task, 60L, TimeUnit.SECONDS);! }! Exec  Async  Scheduled  Task3 Exec  Task After  1  min.
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.52 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.52 @Stateless! public class MyManagedScheduledExecutorService{! @Resource(name = "concurrent/! DefaultManagedScheduledExecutorService")! ManagedScheduledExecutorService managedScheduledExecsvc;! ! public void execScheduledExecutorService() {! MyRunnableTask task = new MyRunnableTask();! managedScheduledExecsvc.schedule(! task, new MyTrigger(new Date(), 10, 1000) }! You  can   Implement   own  trigger Exec  Async  Scheduled  Task3
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.53 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.53 import javax.enterprise.concurrent.Trigger;! public class MyTrigger implements Trigger {! ! @Override! public Date getNextRunTime(LastExecution le, ! Date date){! }! ! @Override! public boolean skipRun(LastExecution le, ! Date date) {! ! }! }! On  Trigger  you  can  implement   The  next  run  time  and/or  skip
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.54 Management  and  Monitoring of  Task  Lifecycle
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.55 Lifecycle  of  Task taskSubmitt ed Submitted taskStarting taskAborted Started taskDone Done submit() submit success Cancel  or  Abort About  to   Start Running  Task Cancelled  or   aborted Task  has  finished Task  Monitoring     :  can  write Rejected
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.56 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.56 public class MyManagedTaskListener implements ! ManagedTaskListener {! public void taskSubmitted(Future<?> future, ! ManagedExecutorService mes, Object o) {! }! public void taskStarting(Future<?> future, ! ManagedExecutorService mes, Object o) {! }! public void taskAborted(Future<?> future, ! ManagedExecutorService mes, Object o, Throwable thrwbl){! }! public void taskDone(Future<?> future, ! ManagedExecutorService mes, Object o, Throwable thrwbl){! }! }! Create  own  Task  Listener which  is  implemented  by   ManagedTaskListener
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.57 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.57 @Resource(name = "concurrent/! MyManagedExecutorService")! ManagedExecutorService manageExecsvc;! ! public void invokeMyTaskListener() {! MyRunnableTask task = new MyRunnableTask();! MyManagedTaskListener listener = ! new MyManagedTaskListener();! Runnable taskWithListener = ! ManagedExecutors.managedTask(task, listener);! manageExecsvc.execute(taskWithListener);! }!
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.58 Detail  of  Internal  Behavior ContextSerivce
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.59 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.59 ManagedExecutorService ManagedScheduledExecutorService We  can  understand  : it  is  very  easy  to  implement  the  concurrent  task             by  Resource  Injection. We  can  manage  the  lifecycle  of  tasks.
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.60 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.60 How  to  run  the  concurrent   tasks  internally  ?
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.61 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.61 Memo Internally  : Use  “Dynamic  Proxy”  and add  some  context  informations
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.62 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.62 Memo Do  you  remember   Dynamic  Proxy  ? (java.lang.reflect.Proxy)
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.63 Method  Invocation  by   Dynamic  Proxy public class MyRunnable implements Runnable{! @Override! public void run() {!    System.out.println(“Original Method Invoke");!   }! } This  Task  will  be  executed  via   Dynamic  Proxy
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.64 Implement  of  own   InvocationHandler public class MyInvocationHandler implements InvocationHandler {! private Object underlying;! public MyInvocationHandler(Object underlying) {! this.underlying = underlying;! }! @Override! public Object invoke(Object proxy, Method method, !              Object[] args) throws Throwable {! System.out.println(”Pre Invoke of orign method");! Object ret = method.invoke(underlying, args);! System.out.println(“Post Invoke of orign method");! return ret;}! }
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.65 Execute  the  Task  via   Dynamic  Proxy public class MyDynamicProxy {! public static void main(String argv[]){!    //… create proxied Task here! }}! MyRunnable task = new MyRunnable();! InvocationHandler handler = new !                    MyInvocationHandler(task);! Runnable proxy = !     (Runnable)Proxy.newProxyInstance(!        MyRunnable.class.getClassLoader(), !        new Class[]{Runnable.class}, handler);! ExecutorService exec = !            Executors.newSingleThreadExecutor();! exec.submit(proxy);
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.66 Result  of  execute  Task  via   Dynamic  Proxy Pre  Invoke  of  orign  method Original  Method  Invoke Post  Invoke  of  orign  method ExecutorService exec =  ! Executors.newSingleThreadExecutor();! exec.submit(proxy);! Useful  for  AOP
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.67 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.67 Memo InvocationHandler  is   implemented  by  App  Server *  GlassFish  v4.0       org.glassfish.enterprise.concurrent.internal.       ContextProxyInvocationHandler
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.68 Configuration  on  ServerSide1
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.69 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.69 @Stateless! public class ContextServiceManager {! @Resource(name = "concurrent/DefaultContextService")! ContextService ctxSvc;! !   public void execSimpleContextService() {! ExecutorService singleThreadExecutor = ! Executors.newSingleThreadExecutor(threadFactory);! MyRunnableTask task = new MyRunnableTask();! Runnable proxiedTask =! ctxSvc.createContextualProxy(task,Runnable.class);! singleThreadExecutor.submit(proxiedTask);}}! ! Create Dynamic  Proxy Task  Execution  with  Context2
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.70 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.70 Original  Task Task  which  is   created  by Dynamic  Proxy Memo It  is  possible  to   run  the  Task  on  EE   with  Context.  
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.71 Detail  of  Internal  Behavior ThreadFactory
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.72 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.72 Memo • we can implement custom managed thread pool on the program side not server side and can execute from the created pool. ※ It is possible to customize the way to create a Thread by program.
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.73 Configuration  on  ServerSide1
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.74 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.74 @Resource(name = "concurrent/! DefaultManagedThreadFactory")! ManagedThreadFactory threadFactory;! public void execThreadFactory() {! MyRunnableTask task = new MyRunnableTask();! Thread taskThread = ! threadFactory.newThread(task);! taskThread.start();! }!  Create  Thread  via  ThreadFactory (It  is  for  long  running  process) 2 Simple
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.75 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.75 ExecutorService threadPoolExecutor = ! Executors.newFixedThreadPool(4,threadFac);! ! ! threadPoolExecutor = new ThreadPoolExecutor(4, 4,!                     0L, TimeUnit.MILLISECONDS,!                     new LinkedBlockingQueue<Runnable>(), !                     threadFac);!  It  is  possible  to  create  custom  Thread   Pool  by  using  ThreadPoolExecutor Above  two  code  is  same java.util.concurrent.ThreadPoolExecutor
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.76 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.76 @Resource(name = "concurrent/! DefaultManagedThreadFactory")! ManagedThreadFactory threadFactory;! public void execThreadFactory() {!   MyRunnableTask task = new MyRunnableTask();!   ExecutorService exec = !            new ThreadPoolExecutor(4, 4,!            0L, TimeUnit.MILLISECONDS,! new LinkedBlockingQueue<Runnable>(), !            threadFactory);!   exec.submit(task);}! Create  new  Thread  from   Custom  Thread  Pool
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.77 Finally
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.78 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.78 Restriction  : We  can  use  with  CDI  only  for    @ApplicationScoped    @Dependent Note  :  Lifecycle  of  CDI
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.79 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.79 Restriction You  canʼ’t  use  on  Application   Client  Container
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.80 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.80 Restriction We  canʼ’t  use  with   Fork/Join  (ForkJoinPool).
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.81 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.81 Restriction We  canʼ’t  invoke  lifecycle  method  of   ExecutorService. (ex.  shutdown,  shutdownNow) ※  ManagedExecutorService ※  ManagedScheduledExecutorService
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.82 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.82 Conclusion Effective  use  of  computer  assets     from  Async  to  Concurrent Safe  and  easy  way  to  create  the   thread  on  Java  EE  environment Flexible  customization
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.83 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.83 Related  Session [CON7948]  :  JSR  236:  Introduction  to   Concurrency  Utilities  for  Java  EE  1.0 Thursday,  Sep  26,  12:30  PM  -‐‑‒  1:30  PM   Parc  55  -‐‑‒  Cyril  Magnin  I Anthony  Lai  –  Oracle  (Spec  Lead) Fred  Rowe  –  IBM  
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.84 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.84 Resource Sample  Source  Code         https://github.com/yoshioterada/                                 ConcurrentUtil4EE JSR  236  Related  Page         http://jcp.org/en/jsr/detail?id=236         https://java.net/projects/                               concurrency-‐‑‒ee-‐‑‒spec
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.85 Graphic Section Divider
  • Copyright © 2012, Oracle and/or its affiliates. All rights reserved.86