Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1Yoshio TeradaJava Evangelisthttp://yoshio3.com,Twitte...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするも...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3はじめに
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4Java EE 7 へ含まれる予定の JSR 一覧Connector1.6JPA 2.1 JTA 1.2 ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5JSR-236 :Concurrency Utilities forJava™ EE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6アプリケーション・サーバのデフォルト任せでかんたん実装柔軟にカスタマイズも可能
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7はじめにJava の非同期処理の歴史Java SE 環境
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.819961997JDK1.0ThreadRunnable20042006JDK1.1J2SE1.2J2SE...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.92011Java SE 7JSR-166yFork/JoinJava SE 8JSR-166eConcur...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11new Thread(r).start();Thread-1Thread-2Thread-3Thread...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.12Thread-1Thread-2Thread-3Thread-n例:スレッド生成時スタック領域も確保スタ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.13Java VM や OS に負荷スレッド生成ごとにメモリを消費スレッド生成数には上限がありスレッドを無制...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.14
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.15Java Concurrency Utilities とは• 並列処理の実装をかんたんに !!• 並列処...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.16JSR-166 で提供された機能• タスクの非同期実行• 並行コレクション• ロック、シンクロナイザ• ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.17利用可能なクラスと拡張• Executors, Thread Pool, Futures• コレクション...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.21newFixedThreadPool固定長スレッドプールを利用したExecutorService の実行...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.22効率的にスレッド実行が可能スレッド・ライフサイクル管理も可能Concurrency Utilities ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23Java EE の非同期処理の歴史JMS(MDB)Async ServletAsync EJB
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2419982009JPEJ2EE 1.4Java EE 6非同期 Servlet非同期 EJBJava E...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.25JMS & MDBJava EE 7 からより簡単に
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.26メッセージプロバイダJNDI 名前空間接続ファクトリjms/MyConFactoryアプリケーション・サ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.27JNDI 名前空間接続ファクトリ宛先JMS クライアントリソース・インジェクションjms/MyFacto...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.29 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.30Servlet 3.0 : Java EE 6 から(asyncSupported = true)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.31 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.32EJB 3.1 : Java EE 6 から@Asynchronous
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.33 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.34 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.35 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.36Concurrency Utilities forEE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.37Application ServersJava SEWeb/EJB コンテナEJB JSPServlet...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.38Application ServersJava SEWeb/EJB コンテナEJB JSPServlet...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.39 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.40JSR-236 の歴史2003 年冬:エキスパート・グループ結成(BEA, IBM : commonj ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.41JSR-236 の歴史2008 年 6 月:仕様の名前が変更2006 年 4 月 : Version 0...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.42参照実装 :https://cu-javaee.java.net/
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.43 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.44 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.45 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.46 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.47 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.48とてもかんたんEE 環境の非同期タスク実装手順(ManagedExecutorService の利用例)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.49 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.50タスクを実装A implements RunnableB implements Callable非同期タ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.51 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.52 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.53サーバ側の設定例2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.54 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.55非同期タスクのスケジューリング(ManagedScheduledExecutorServiceの利用例)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.56 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.57タスクを実装A implements RunnableB implements Callable非同期タ...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.58サーバ側の設定例2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.59 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.60 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.61 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.62タスクのライフサイクル管理・監視
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.63タスクのライフサイクルtaskSubmitted Submitted taskStartingtaskA...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.64 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.65 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.66内部動作の詳細ContextSerivce
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.67 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.68 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.69 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.70 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.71Dynamic Proxy によるメソッド呼び出しpublic class MyRunnable imp...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.72独自 InvocationHandler の実装public class MyInvocationHan...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.73Dynamic Proxy 経由でタスク実行public class MyDynamicProxy {p...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.74Dynamic Proxy のタスク実行結果オリジナル・メソッド呼び出しの前処理オリジナル・メソッドの呼...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.75 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.76サーバ側の設定例1
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.77 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.78 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.79内部動作の詳細ThreadFactory
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.80 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.81 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.82サーバ側の設定例1
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.83 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.84 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.85 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.86さいごに
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.87 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.88 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.89 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.90 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.91 Copyright © 2013, Oracle and/or its affiliates. All...
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.92
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.93
Upcoming SlideShare
Loading in...5
×

Concurrency utilities for Java EE 7

20,667

Published on

Published in: Technology, News & Politics
0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
20,667
On Slideshare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
97
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide
  • 本日は、エンタープライズ環境における並列処理の実装方法についてと題して、Java EE 環境で利用可能な並列処理の実装方法の歴史的背景から最新のJava EE 7 で含まれる Concurrency Utilities for EE についてもご紹介します。
  • それでは、この Javaの Thread の歴史について簡単におさらいをしてみます。JDK 1.0 が出た当初から、Java はマルチスレッドに対応するプログラミング言語として従来の書き方を用いて、実装する事ができました。しかし、2000年代初頭より始まった、CPU のマルチコア、CPU 内でマルチスレッドで実現するような CPU の登場によって、並列化処理に対する効率的な処理が求められるようになりました。このような中で、作られた仕様が Java SE 5 で導入された、JSR-166の Concurrency Utilities です。Java SE 6 でも、 JSR-166x として拡張が行われています。
  • さらには、Java SE 7 で JSR-166y として Fork/Join フレームワークが導入されより粒度の細かな処理を行う事を目的とした API も提供されました。また、将来的に Java SE 8 でも JSR-166e として拡張が行われる予定です。
  • それでは、JSR-166で提供された機能を簡単におさらいします。タスクという概念の導入、とタスクの非同期実行、並行コレクション、ロック、シンクロナイザ、アトミック処理などがあります。
  • Concurrency utilities for Java EE 7

    1. 1. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.1Yoshio TeradaJava Evangelisthttp://yoshio3.com,Twitter : @yoshioterada【C-4】
    2. 2. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。以下の事項は、マテリアルやコード、機能を提供することをコミットメント(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さい。オラクル製品に関して記載されている機能の開発、リリースおよび時期については、弊社の裁量により決定されます。Oracleは、米国オラクルコーポレーション及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。
    3. 3. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3はじめに
    4. 4. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4Java EE 7 へ含まれる予定の JSR 一覧Connector1.6JPA 2.1 JTA 1.2 JMS 2.0Managed Bean 1.0 EJB 3.2CommonAnnotations 1.1Interceptors 1.1 CDI 1.1PortableExtensionsServlet 3.1JSP 2.2 JSF 2.2JAX-RS2.0EL 3.0BeanValidation1.1ConcurrencyUtilities for EEBatchApplication(JSR-352)Java API forJSON(JSR-353)Java API forWebSocket(JSR-356)新規追加 大幅な更新 通常の更新
    5. 5. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5JSR-236 :Concurrency Utilities forJava™ EE
    6. 6. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6アプリケーション・サーバのデフォルト任せでかんたん実装柔軟にカスタマイズも可能
    7. 7. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7はじめにJava の非同期処理の歴史Java SE 環境
    8. 8. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.819961997JDK1.0ThreadRunnable20042006JDK1.1J2SE1.2J2SE 1.3J2SE 1.4Java SE 5JSR-166Concurrency UtilitiesJava SE 6JSR-166xJava Thread の歴史199820002002
    9. 9. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.92011Java SE 7JSR-166yFork/JoinJava SE 8JSR-166eConcurrency Utilities2013Java SE Thread の歴史
    10. 10. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10class 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);//}};new Thread(r).start();}} }マルチスレッド Web サーバの実装例この方法は SE 環境でも EE 環境でもやめましょう!!
    11. 11. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11new Thread(r).start();Thread-1Thread-2Thread-3Thread-nスレッドを無制限に生成・・・
    12. 12. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.12Thread-1Thread-2Thread-3Thread-n例:スレッド生成時スタック領域も確保スタックThread-1用スタックThread-2用スタックThread-3用スタックThread-n用スタック… …-Xss
    13. 13. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.13Java VM や OS に負荷スレッド生成ごとにメモリを消費スレッド生成数には上限がありスレッドを無制限に生成する欠点
    14. 14. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.14
    15. 15. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.15Java Concurrency Utilities とは• 並列処理の実装をかんたんに !!• 並列処理用の簡易 API を提供• 、保守性、可読性
    16. 16. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.16JSR-166 で提供された機能• タスクの非同期実行• 並行コレクション• ロック、シンクロナイザ• アトミック処理
    17. 17. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.17利用可能なクラスと拡張• Executors, Thread Pool, Futures• コレクション: Queues, Blocking Queues, Concurrent HashMap• ロック・同期: Semaphores, Barriers, Atomic変数• 他の拡張
    18. 18. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18public interface Executor{void execute(Runnable command);}Executor インタフェース非同期呼び出しの標準化
    19. 19. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19public 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)…}ExecutorService インタフェースライフサイクル管理、返り値の取得も可能
    20. 20. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20class MyWebServer{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(){;//} };pool.execute(r);}}} Executor 版 Web サーバ
    21. 21. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.21newFixedThreadPool固定長スレッドプールを利用したExecutorService の実行例LinkedBlockingQueue (FIFO)ThreadFactory…T1 T2 T3 T4 TnExecutorService pool =Executors.newFixedThreadPool(CPU_NUM);pool.execute(r);
    22. 22. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.22効率的にスレッド実行が可能スレッド・ライフサイクル管理も可能Concurrency Utilities の導入効果
    23. 23. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23Java EE の非同期処理の歴史JMS(MDB)Async ServletAsync EJB
    24. 24. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.2419982009JPEJ2EE 1.4Java EE 6非同期 Servlet非同期 EJBJava EE の非同期処理の歴史199920012003Java EE 52006J2EE 1.2JMSJ2EE 1.3MDB
    25. 25. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.25JMS & MDBJava EE 7 からより簡単に
    26. 26. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.26メッセージプロバイダJNDI 名前空間接続ファクトリjms/MyConFactoryアプリケーション・サーバ管理者 宛先jms/MyQueue宛先のキュー名外部のメッセージプロバイダに処理を委譲
    27. 27. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.27JNDI 名前空間接続ファクトリ宛先JMS クライアントリソース・インジェクションjms/MyFactoryjms/MyQueueクライアントはJNDI経由で論理的に接続メッセージプロバイダ宛先のキュー名開発者開発者は JNDI ルックアップで参照
    28. 28. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28@Statelesspublic 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);}}}JMS 2.0 送信 (Java EE 7)
    29. 29. Copyright © 2013, 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;@Overridepublic void onMessage(Message message) {try {TextMessage msg = (TextMessage) message;mailSender.sendMessage(msg.getText());} catch (JMSException jmse) {jmse.printStackTrace();}}} MDB 受信 (Java EE 7)
    30. 30. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.30Servlet 3.0 : Java EE 6 から(asyncSupported = true)
    31. 31. Copyright © 2013, 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 : 非同期サーブレット
    32. 32. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.32EJB 3.1 : Java EE 6 から@Asynchronous
    33. 33. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.33 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.33@Statelesspublic class SyncEmailSenderEJB {@InjectMailSender mailsend;public void syncSendMessage(String email){mailsend.sendMessage(email);}@Asynchronouspublic void asyncSendMessage(String email){mailsend.sendMessage(email);}} EJB 3.1 (Java EE 6)
    34. 34. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.34 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.34
    35. 35. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.35 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.35
    36. 36. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.36Concurrency Utilities forEE
    37. 37. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.37Application ServersJava SEWeb/EJB コンテナEJB JSPServletRunnableCallableJava EE 関連機能 (JAX-RS,JavaMail, CDI など)Java EE 環境で禁止されていた Thread 生成
    38. 38. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.38Application ServersJava SEWeb/EJB コンテナEJB JSPServletRunnableCallableJava EE とConcurrencyのアーキテクチャJava EE 関連機能(JAX-RS,JavaMail, CDI など)ManagedExecutor ServiceManagedScheduledExecutorServiceContextServiceManagedThreadFactoryConcurrency Utilities for EE
    39. 39. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.39 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.39
    40. 40. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.40JSR-236 の歴史2003 年冬:エキスパート・グループ結成(BEA, IBM : commonj を元に)J2EE 1.4, Java EE5 で取り込む予定だったJSR-236 : Timer for Application Servers
    41. 41. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.41JSR-236 の歴史2008 年 6 月:仕様の名前が変更2006 年 4 月 : Version 0.1: Early Draft PreviewJSR-236 : Concurrency Utilities for Java EE2012 年 Q2 : エキスパート・グループ再結成→ Java EE 7 へ !!
    42. 42. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.42参照実装 :https://cu-javaee.java.net/
    43. 43. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.43 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.43
    44. 44. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.44 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.44
    45. 45. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.45 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.45
    46. 46. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.46 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.46
    47. 47. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.47 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.47
    48. 48. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.48とてもかんたんEE 環境の非同期タスク実装手順(ManagedExecutorService の利用例)
    49. 49. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.49 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.49
    50. 50. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.50タスクを実装A implements RunnableB implements Callable非同期タスクを実装リソース・インジェクションでサーバ管理スレッドを利用サーバ側で設定を実施デフォルト設定も利用可1 2EE環境の非同期タスクの実装手順3
    51. 51. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.51 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.51public class MyRunnableTask implements Runnable {@Overridepublic void run() {try {Thread.sleep(10000); //何らかの処理} catch (InterruptedException ex) {logger.log(Level.SEVERE, null, ex);}}}Runnable のタスクを作成1
    52. 52. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.52 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.52Callable のタスクを作成● call() は返り値を持つ事が可能● チェック例外のスローが可能1public class MyCallableTask implementsCallable<String> {@Overridepublic String call() throws Exception {return “Hello World”;}}
    53. 53. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.53サーバ側の設定例2
    54. 54. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.54 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.54@Statelesspublic 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);}非同期タスクを実行する EJB3
    55. 55. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.55非同期タスクのスケジューリング(ManagedScheduledExecutorServiceの利用例)
    56. 56. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.56 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.56
    57. 57. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.57タスクを実装A implements RunnableB implements Callable非同期タスクを実装リソース・インジェクションでサーバ管理スレッドを利用サーバ側で設定を実施デフォルト設定も利用可1 2EE環境の非同期タスクの実装手順3
    58. 58. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.58サーバ側の設定例2
    59. 59. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.59 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.59@Statelesspublic class MyManagedScheduledExecutorService{@Resource(name = "concurrent/DefaultManagedScheduledExecutorService")ManagedScheduledExecutorService managedScheduledExecsvc;public void execScheduledExecutorService() {MyRunnableTask task = new MyRunnableTask();managedScheduledExecsvc.schedule(task, 60L, TimeUnit.SECONDS);}非同期タスクを実行する EJB3
    60. 60. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.60 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.60@Statelesspublic class MyManagedScheduledExecutorService{@Resource(name = "concurrent/DefaultManagedScheduledExecutorService")ManagedScheduledExecutorService managedScheduledExecsvc;public void execScheduledExecutorService() {MyRunnableTask task = new MyRunnableTask();managedScheduledExecsvc.schedule(task, new MyTrigger(new Date(), 10, 1000) }非同期タスクを実行する EJB3
    61. 61. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.61 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.61import javax.enterprise.concurrent.Trigger;public class MyTrigger implements Trigger {@Overridepublic Date getNextRunTime(LastExecution le,Date date){}@Overridepublic boolean skipRun(LastExecution le,Date date) {}}
    62. 62. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.62タスクのライフサイクル管理・監視
    63. 63. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.63タスクのライフサイクルtaskSubmitted Submitted taskStartingtaskAborted StartedtaskDoneDonesubmit()の実行submit成功キャンセル・中止タスクの実行準備タスク実行キャンセル・中止 タスク実行完了
    64. 64. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.64 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.64public class MyManagedTaskListener implementsManagedTaskListener {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){}}
    65. 65. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.65 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.65@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);}
    66. 66. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.66内部動作の詳細ContextSerivce
    67. 67. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.67 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.67
    68. 68. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.68 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.68
    69. 69. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.69 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.69
    70. 70. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.70 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.70
    71. 71. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.71Dynamic Proxy によるメソッド呼び出しpublic class MyRunnable implements Runnable{@Overridepublic void run() {System.out.println(“ ");}}
    72. 72. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.72独自 InvocationHandler の実装public class MyInvocationHandler implements InvocationHandler {private Object underlying;public MyInvocationHandler(Object underlying) {this.underlying = underlying;}@Overridepublic Object invoke(Object proxy, Method method,Object[] args) throwsThrowable {System.out.println(" ");Object ret = method.invoke(underlying, args);System.out.println(" ");return ret;}}
    73. 73. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.73Dynamic Proxy 経由でタスク実行public class MyDynamicProxy {public static void main(String argv[]){MyRunnable task = new MyRunnable();InvocationHandler handler = newMyInvocationHandler(task);Runnable proxy = (Runnable)Proxy.newProxyInstance(MyRunnable.class.getClassLoader(),newClass[]{Runnable.class}, handler);ExecutorService exec = Executors.newSingleThreadExecutor();exec.submit(proxy);}}
    74. 74. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.74Dynamic Proxy のタスク実行結果オリジナル・メソッド呼び出しの前処理オリジナル・メソッドの呼び出しオリジナル・メソッド呼び出しの前処理ExecutorService exec =Executors.newSingleThreadExecutor();exec.submit(proxy);
    75. 75. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.75 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.75
    76. 76. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.76サーバ側の設定例1
    77. 77. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.77 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.77@Statelesspublic 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);}}コンテキスト付タスクの実行2
    78. 78. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.78 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.78オリジナルのタスクDynamic Proxy で生成されたタスク
    79. 79. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.79内部動作の詳細ThreadFactory
    80. 80. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.80 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.80
    81. 81. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.81 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.81
    82. 82. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.82サーバ側の設定例1
    83. 83. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.83 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.83@Resource(name = "concurrent/DefaultManagedThreadFactory")ManagedThreadFactory threadFactory;public void execThreadFactory() {MyRunnableTask task = new MyRunnableTask();Thread taskThread =threadFactory.newThread(task);taskThread.start();}ThreadFactory からスレッド生成(長時間実行タスクに有効)2
    84. 84. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.84 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.84ExecutorService threadPoolExecutor =Executors.newFixedThreadPool(4,threadFac);threadPoolExecutor = new ThreadPoolExecutor(4, 4,0L, TimeUnit.MILLISECONDS,newLinkedBlockingQueue<Runnable>(),threadFac);ThreadPoolExecutor を使用するとカスタム・スレッドプールの生成が可能
    85. 85. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.85 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.85@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);}カスタム・スレッド・プールからスレッド生成
    86. 86. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.86さいごに
    87. 87. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.87 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.87
    88. 88. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.88 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.88
    89. 89. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.89 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.89
    90. 90. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.90 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.90
    91. 91. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.91 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.91
    92. 92. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.92
    93. 93. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.93
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×