Java5ããã¹ã¬ãããã¼ã«ãå©ç¨ã§ããããã«ãªãã¾ããã
ã¹ã¬ããä½æã®æ¬ ç¹
ããã¾ã§ã®ã¹ã¬ããã¯ãThreadã¯ã©ã¹ãRunnableã¯ã©ã¹ãçµã¿åãã使ã£ã¦ãã¾ãããããããã¹ã¬ããããã åã«ä½¿ãã ãã ã¨ä»¥ä¸ã®ãããªæ¬ ç¹ãããã¾ãã
- ã¹ã¬ãããçæããç ´æ£ã¾ã§ã®ã©ã¤ããµã¤ã¯ã«ã§çºçããã³ã¹ã
ä¸è¬çã«ã¯ã¹ã¬ããä½æã¯OSãJVMã«è² è·ãçºçãã¾ãããªã¯ã¨ã¹ããé »ç¹ã§è»½ãå ´åã¯ç¸å½éã®ãªã½ã¼ã¹ãæ¶è²»ãã¦ãã¾ãã¾ãã
- ãªã½ã¼ã¹ã®æ¶è²»
ã¢ã¯ãã£ããªã¹ã¬ããã¯ã¡ã¢ãªãæ¶è²»ãã¾ããå®è¡ç¶æ ã®ã¹ã¬ãããå©ç¨å¯è½ãªCPUã®æ°ããå¤ãã¨ã¹ã¬ãããåæ»ãã¾ããããã«ã¢ã¤ãã«ç¶æ ã®ã¹ã¬ãããå¤ããªãã¨ã¡ã¢ãªæ¶è²»éãå¢ãã¦ãã¬ãã¼ã¸ã³ã¬ã¯ã¿ã®è² æ ãå¢å¤§ãå¤ãã®ã¹ã¬ãããCPUã®æéã奪ãåãã¬ã¼ã¹ã³ã³ãã£ã·ã§ã³ã«ãªãæ§è½ãä½ä¸ãã¾ãããã®ããã«CPUã®æ°ä»¥ä¸ã¹ã¬ãããåå¨ãã¦ãæå³ãããã¾ããã
- å®å®æ§ãèæ ®ããã¦ããªã
ä½æã§ããã¹ã¬ããã®ä¸éã®æ°ã¯ããããã決ãããã¦ãã¾ãããããããããç¡è¦ãã¦ä½æããå ´åOutOfMemoryErrorãçºçãããã¨ã«ãªãã¾ããä¸éãè¶ ããªããããªä»çµã¿ãå¿ è¦ã«ãªãã¾ãã
ã¨ããããã§ããããã®è§£ã¨ã㦠Executor ãã¬ã¼ã ã¯ã¼ã¯ã§ã
java.util.concurrentããã±ã¼ã¸ã§æä¾ãããExecutorãã¬ã¼ã ã¯ã¼ã¯ã使ãã°ã¢ããªã±ã¼ã·ã§ã³ãéè² è·ã§ã¡ã¢ãªä¸è¶³ã«é¥ããªãããã«ãªãã¾ãã
åºæ¬çã«ã¯ã¹ã¬ãããã¼ã«ãä½æããã®ç¯å²å
ã§ã¹ã¬ããã管çãããã¨ã§ãæé©ãªã¹ã«ã¼ãããã¨è³æºç®¡çãå®ç¾ãã¾ãã
以ä¸ã®ã³ã¼ããã覧ãã ããã
final ExecutorService e = Executors.newFixedThreadPool(2);ã®é¨åã§ã¹ã¬ãããã¼ã«ãµã¤ãº2ã§ä½æãã¦ãã¾ãããã®å ´ååæ2åã®ã¿ã¹ã¯ããå¦çãã¾ãããã¿ã¹ã¯ã¯e.executeã§ä¸ãã¾ããæåã«10åç»é²ãã¦ãã¾ããããã¼ã«ã2åã¾ã§ã§ãã®ã§æåã®2åããé çªã«å¦çããã¾ãããã¼ã«ãµã¤ãºã10åã«ããå ´åã¯ãã¹ã¦ã®ã¿ã¹ã¯ã並è¡å¦çããã¾ãã
ã¿ã¹ã¯ã®ãã£ã³ã»ã«ã§ãããe.shutdownãããã¯e.shutdownNowãããã¾ããe.shutdownã¯ãå®è¡ä¸ã®ã¿ã¹ã¯ãæ¢ããã«æ®ãã®å®è¡ããã¦ããªãã¿ã¹ã¯ããã£ã³ã»ã«ãã¾ãããã®å ´åã§ãå®è¡ä¸ã®ã¿ã¹ã¯ãåæ¢ããããå ´åã¯ãe.isShutdownãåç
§ããã¨ããã§ããããããããã³ã°ããAPIã使ã£ã¦ããå ´åã¯e.shutdownNowã使ããªãã¨ãã£ã³ã»ã«ã§ãã¾ããããã®å ´åã¯ã¤ã³ã¿ã©ãããçºçãããã®ã§ãã¿ã¹ã¯å´ã§InterruptedExceptionããã³ããªã³ã°ã§ããããã«ããå¿
è¦ãããã¾ããInterruptedExceptionãçºçãããã«ã¼ããæãããªã©ã®ã³ã¼ããå®è£
ããã ãã§ãã£ã³ã»ã«ã«å¯¾å¿ã§ããããã«ãªãã¾ãã
TimeUnit.SECONDS.sleep(5);ã§ãããã¯ãã¦ãã¦ããe.shutdownNowãå¼ã°ããã¨InterruptedExceptionãçºçãã¿ã¹ã¯ã¯ãã£ã³ã»ã«ãã¾ãã
public class Test { public static void main(String arg[]) { final ExecutorService e = Executors.newFixedThreadPool(2); for (int i = 0; i < 10; i++) { e.execute(new Runnable() { public void run() { handleRequest(e); } }); } try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e1) { } e.shutdownNow(); try { e.awaitTermination(10, TimeUnit.SECONDS); } catch (InterruptedException e1) { } } private static void handleRequest(ExecutorService executorService) { int coutner = 0; try { while (!executorService.isShutdown()) { TimeUnit.SECONDS.sleep(5); System.out.println(Thread.currentThread().getId() + " : counter = " + coutner++); } } catch (InterruptedException e) { e.printStackTrace(); } } }
S2Chronosã®ã¸ã§ãã¯ã©ã¹ã§ãããã®ã¤ã³ã¿ã©ãã·ã§ã³ãµãã¼ããã¾ãã®ã§ãã®ä»çµã¿ããã®ã¾ã¾å©ç¨ã§ãã¾ãã
ã¤ã³ã¿ã©ãã·ã§ã³ãçºçãããã©ããã®ãã©ã°ã調ã¹ããå ´åã¯ãThread.currentThread().isInterrupted()ãå¼ã³åºãã¦ç¢ºèªã§ãã¾ãã
public class ExampleJob { <snip> @Group("groupA") @Next("jobB") @Join(JoinType.NoWait) public void doJobA() { try { for (int i = 1; i < 5; i++) { TimeUnit.SECONDS.sleep(1); log.info("doJobA"); } } catch (InterruptedException e) { } } <snip> }
åèæ¸ç±ã¯
Java並è¡å¦çããã°ã©ãã³ã° âãã®ãåºç¤ãã¨ãææ°APIãã究ããâ
- ä½è : Brian Goetz,Joshua Bloch,Doug Lea
- åºç社/ã¡ã¼ã«ã¼: ã½ãããã³ã¯ã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2006/11/22
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 24人 ã¯ãªãã¯: 419å
- ãã®ååãå«ãããã° (163件) ãè¦ã