{"id":187854,"date":"2013-07-26T16:23:03","date_gmt":"2013-07-26T12:23:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=187854"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=187854","title":{"rendered":"<span class=\"post_title\">JDK concurrent package<\/span>"},"content":{"rendered":"<div class=\"content html_format\">       \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432 Java, \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u0433\u043e\u043d\u043e\u043a \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0418 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 \u043e\u0442 \u0433\u043e\u043d\u043e\u043a, \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043d\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438.<\/p>\n<p>  \u041f\u0430\u043a\u0435\u0442 <code>java.util.concurrent<\/code>, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432 HotSpot JDK, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430:  <\/p>\n<ul>\n<li>Atomic<\/li>\n<li>Locks<\/li>\n<li>Collections<\/li>\n<li>Synchronization points <\/li>\n<li>Executors<\/li>\n<li>Accumulators _jdk 1.8_<\/li>\n<\/ul>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h5>Atomic<\/h5>\n<p>  \u0412 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u043c \u043f\u0430\u043a\u0435\u0442\u0435 <code>java.util.concurrent.atomic<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0430\u0431\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438. \u041a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 <code>compare-and-set<\/code> \u0437\u0430 \u00ab1 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438\u00bb. \u041f\u0440\u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0435 \u0435\u0435 \u0441\u0442\u0430\u0440\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (\u043f\u043e\u0434\u0445\u043e\u0434 \u043e\u043f\u0442\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438). \u0415\u0441\u043b\u0438 \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e \u2014 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 <code>false<\/code>.<\/p>\n<p>  \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <code>long<\/code> \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <code>[1,2,3,4,5]<\/code> \u0438 <code>[-1,-2,-3,-4,-5]<\/code>. \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0435\u0434\u0438\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e. \u041a\u043e\u0434 (<i>groovy<\/i>) \u0441 \u043f\u0435\u0441\u0441\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"java\">class Sum {     static monitor = new Object()     static volatile long sum = 0 }  class Summer implements Callable {     long[] data     Object call() throws Exception {         data.each {             synchronized (Sum.monitor) {                 println(&quot;${Thread.currentThread().name}: add ${it} to ${Sum.sum}&quot;)                 Sum.sum += it             }         }     } }  Executors.newFixedThreadPool(2).invokeAll([         new Summer(data: [1,2,3,4,5]),         new Summer(data: [-1,-2,-3,-4,-5]) ])  print(&quot;Sum: ${Sum.sum}&quot;) <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c:<\/p>\n<pre><code>pool-1-thread-1: add 1 to 0 pool-1-thread-2: add -1 to 1 pool-1-thread-1: add 2 to 0 pool-1-thread-2: add -2 to 2 pool-1-thread-1: add 3 to 0 pool-1-thread-2: add -3 to 3 pool-1-thread-1: add 4 to 0 pool-1-thread-1: add 5 to 4 pool-1-thread-2: add -4 to 9 pool-1-thread-2: add -5 to 5 Sum: 0 <\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438\u043c\u0435\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430 \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0434\u043b\u044f \u043d\u0430\u0441 \u0440\u0430\u0431\u043e\u0442\u0443, \u0443\u0445\u043e\u0434\u0438\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0447\u0435\u043c \u043d\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e:  <\/p>\n<ul>\n<li>\u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0430<\/li>\n<li>\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u0430<\/li>\n<li>\u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0430<\/li>\n<li>\u0440\u0430\u0437\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0442\u043e\u043a\u0430<\/li>\n<\/ul>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>AtomicLong<\/code> \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0440\u0438 \u0440\u0430\u0441\u0447\u0435\u0442\u0435 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0441\u0443\u043c\u043c\u044b:<\/p>\n<pre><code class=\"java\">class Sum {     static volatile AtomicLong sum = new AtomicLong(0) } class Summer implements Callable {     long[] data     Object call() throws Exception {         data.each {                 while(true) {                     long localSum = Sum.sum.get()                     if (Sum.sum.compareAndSet(localSum, localSum + it)) {                         println(&quot;${Thread.currentThread().name}: add ${it} to ${Sum.sum}&quot;)                         break;                     } else {                         println(&quot;[MISS!] ${Thread.currentThread().name}: add ${it} to ${Sum.sum}&quot;)                     }                 }         }     } }  Executors.newFixedThreadPool(2).invokeAll([         new Summer(data: [1,2,3,4,5]),         new Summer(data: [-1,-2,-3,-4,-5]) ])  print(&quot;Sum: ${Sum.sum}&quot;) <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u00ab\u043e\u0448\u0438\u0431\u043e\u0447\u043d\u044b\u0445\u00bb \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u0431\u044b\u043b\u043e \u043d\u0435 \u0442\u0430\u043a \u0443\u0436 \u0438 \u043c\u043d\u043e\u0433\u043e:<\/p>\n<pre><code>[MISS!] pool-1-thread-1: add 1 to -1 pool-1-thread-2: add -1 to -1 pool-1-thread-2: add -2 to -3 [MISS!] pool-1-thread-1: add 1 to -3 pool-1-thread-2: add -3 to -6 pool-1-thread-1: add 1 to -5 [MISS!] pool-1-thread-2: add -4 to -5 pool-1-thread-1: add 2 to -7 pool-1-thread-2: add -4 to -7 pool-1-thread-1: add 3 to -9 pool-1-thread-2: add -5 to -9 pool-1-thread-1: add 4 to -5 pool-1-thread-1: add 5 to 0 Sum: 0 <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u0443\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u0430\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0441 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u043b\u043e \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0427\u0435\u043c \u0434\u043e\u043b\u044c\u0448\u0435 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u2014 \u0442\u0435\u043c \u0447\u0430\u0449\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u044b\u0435 <code>compare-and-set<\/code>, \u0438 \u0442\u0435\u043c \u0447\u0430\u0449\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e.<\/p>\n<p>  \u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <code>compare-and-set<\/code> \u043c\u043e\u0436\u0435\u0442 \u0442\u0430\u043a\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0430\u044f read \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432 atomic \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u043e\u043b\u0443\u0447\u0438\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u0434\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0432\u0435\u0440\u0438\u0442\u044c \u0435\u0435 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f. \u041e\u0431\u044b\u0447\u043d\u044b\u0435 <code>read-write<\/code> \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u0432 \u0441\u0438\u043b\u0443, \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0432\u0435\u0440\u0441\u0438\u0438 \u043f\u0440\u043e\u0432\u0430\u043b\u0438\u043b\u0430\u0441\u044c.<\/p>\n<pre><code class=\"java\">class Transaction {     long debit }  class Account {     AtomicLong version = new AtomicLong()     ReadWriteLock readWriteLock = new ReentrantReadWriteLock()     List&lt;Transaction&gt; transactions = new ArrayList&lt;Transaction&gt;() }  long  balance(Account account) {     ReentrantReadWriteLock.ReadLock locked     while(true) {         long balance = 0         long version = account.version.get()         account.transactions.each {balance += it.debit}         \/\/volatile write for JMM         if (account.version.compareAndSet(version, version)) {             if (locked) {locked.unlock()}             return balance         } else {             locked = account.readWriteLock.readLock()         }     } }  void modifyTransaction(Account account, int position, long newDebit) {     def writeLock = account.readWriteLock.writeLock()     account.version.incrementAndGet()     account.transactions[position].debit = newDebit     writeLock.unlock() } <\/code><\/pre>\n<h5>Locks<\/h5>\n<p>  <\/p>\n<h6>ReentrantLock<\/h6>\n<p>  \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 syncronized \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a, <code>ReentrantLock <\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0441\u043d\u044f\u0442\u0438\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0442.\u043a. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0435 Java \u0432\u044b\u0437\u043e\u0432\u044b. \u0422\u0430\u043a\u0436\u0435 <code>ReentrantLock<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u00ab\u043e\u0436\u0438\u0434\u0430\u0442\u044c\u00bb \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u0447\u0435\u0441\u0442\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 (\u0441\u043e\u0431\u043b\u044e\u0434\u0430\u044e\u0449\u0438\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u0435 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0430) \u2014 <code>ReentrantLock<\/code> \u0442\u0430\u043a\u0436\u0435 \u0441\u043d\u0430\u0431\u0436\u0435\u043d \u044d\u0442\u0438\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c.<\/p>\n<p>  \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e <code>syncronized<\/code> \u0438 <code>ReentrantLock<\/code> \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438 \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 JVM \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e. <br \/>  \u041d\u0435 \u0432\u0434\u0430\u0432\u0430\u044f\u0441\u044c \u0432 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 JMM: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>ReentrantLock<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0439 JVM syncronized \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u0442\u043e\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u0438\u0442\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0437\u0430 \u043c\u043e\u043d\u0438\u0442\u043e\u0440. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0432 syncronized \u043c\u0435\u0442\u043e\u0434 _\u043e\u0431\u044b\u0447\u043d\u043e_ \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u2014 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c <code>ReentrantLock<\/code> \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0443 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a JVM.<\/p>\n<h6>ReentrantReadWriteLock<\/h6>\n<p>  \u0414\u043e\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>ReentrantLock<\/code> \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c. \u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u00ab\u043e\u043f\u0443\u0449\u0435\u043d\u0430\u00bb \u0434\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e.<\/p>\n<h6>StampedLock _jdk 1.8_<\/h6>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0435 \u0438 \u043f\u0435\u0441\u0441\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435-\u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0438\u043b\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f. \u041e\u043f\u0442\u0438\u043c\u0438\u0441\u0442\u0438\u0447\u043d\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u00ab\u0448\u0442\u0430\u043c\u043f\u00bb \u043b\u043e\u043a\u0430 (<i>javadoc<\/i>):<\/p>\n<pre><code class=\"java\">double distanceFromOriginV1() { \/\/ A read-only method  long stamp;  if ((stamp = sl.tryOptimisticRead()) != 0L) { \/\/ optimistic    double currentX = x;    double currentY = y;    if (sl.validate(stamp))      return Math.sqrt(currentX * currentX + currentY * currentY);  }  stamp = sl.readLock(); \/\/ fall back to read lock  try {    double currentX = x;    double currentY = y;      return Math.sqrt(currentX * currentX + currentY * currentY);  } finally {    sl.unlockRead(stamp);  } } <\/code><\/pre>\n<h5>Collections<\/h5>\n<p>  <\/p>\n<h6>ArrayBlockingQueue<\/h6>\n<p>  \u0427\u0435\u0441\u0442\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 (<code>put()<\/code> <code>take()<\/code>) \u0438 \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 (<code>offer()<\/code> <code>pool()<\/code>) \u043c\u0435\u0442\u043e\u0434\u044b. \u0417\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 null \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0415\u043c\u043a\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u0430 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438.<\/p>\n<h6>ConcurrentHashMap<\/h6>\n<p>  \u041a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 <code>hash<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435. \u041f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u043a\u0430\u0440\u0442\u044b (\u0441\u0435\u0433\u043c\u0435\u043d\u0442). \u041a\u043e\u043b-\u0432\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u0439 \u043a <code>concurrencyLevel<\/code> \u0441\u0442\u0435\u043f\u0435\u043d\u0438 2.<\/p>\n<h6>ConcurrentSkipListMap<\/h6>\n<p>  \u0421\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 (O(log n)). \u041f\u043e\u0438\u0441\u043a \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u043c\u0438. \u041a\u0430\u0440\u0442\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0438.<\/p>\n<h6>ConcurrentSkipListSet<\/h6>\n<p>  <code>ConcurrentSkipListMap<\/code> \u0431\u0435\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n<h6>CopyOnWriteArrayList<\/h6>\n<p>  \u0411\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c, \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043e\u043a. \u041b\u044e\u0431\u0430\u044f \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h6>CopyOnWriteArraySet<\/h6>\n<p>  <code>CopyOnWriteArrayList<\/code> \u0431\u0435\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n<h6>DelayQueue<\/h6>\n<p>  <code>PriorityBlockingQueue<\/code> \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0449\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438 (\u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>Delayed<\/code> \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430). <code>DelayQueue<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0430 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0430. \u0415\u043c\u043a\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430.<\/p>\n<h6>LinkedBlockingDeque<\/h6>\n<p>  \u0414\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f <code>BlockingQueue<\/code>, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438 (cache-miss &#038; cache coherence overhead). \u0415\u043c\u043a\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430.<\/p>\n<h6>LinkedBlockingQueue<\/h6>\n<p>  \u041e\u0434\u043d\u043e\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f <code>BlockingQueue<\/code>, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438 (cache-miss &#038; cache coherence overhead). \u0415\u043c\u043a\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430.<\/p>\n<h6>LinkedTransferQueue<\/h6>\n<p>  \u041e\u0434\u043d\u043e\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f `BlockingQueue`, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438 (cache-miss &#038; cache coherence overhead). \u0415\u043c\u043a\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430. \u0414\u0430\u043d\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u00ab\u0437\u0430\u0431\u0435\u0440\u0435\u0442\u00bb \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a.<\/p>\n<h6>PriorityBlockingQueue<\/h6>\n<p>  \u041e\u0434\u043d\u043e\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f `BlockingQueue`, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0449\u0430\u044f \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f (\u0447\u0435\u0440\u0435\u0437 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432). \u0417\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 null \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<h6>SynchronousQueue<\/h6>\n<p>  \u041e\u0434\u043d\u043e\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f `BlockingQueue`, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0430\u044f <code>transfer()<\/code> \u043b\u043e\u0433\u0438\u043a\u0443 \u0434\u043b\u044f <code>put()<\/code> \u043c\u0435\u0442\u043e\u0434\u043e\u0432.<\/p>\n<h5>Synchronization points<\/h5>\n<p>  <\/p>\n<h6>CountDownLatch<\/h6>\n<p>  \u0411\u0430\u0440\u044c\u0435\u0440 (<code>await()<\/code>), \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e (\u0438\u043b\u0438 \u0431\u043e\u043b\u044c\u0448\u0435) \u043a\u043e\u043b-\u0432\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <code>countDown()<\/code>. \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u0430\u0440\u044c\u0435\u0440\u0430 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0431\u0440\u043e\u0448\u0435\u043d\u043e.<\/p>\n<h6>CyclicBarrier<\/h6>\n<p>  \u0411\u0430\u0440\u044c\u0435\u0440 (<code>await()<\/code>), \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u043b-\u0432\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <code>await()<\/code> \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438. \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u043b-\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 callback \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043d\u0438\u043c\u0435\u0442\u0441\u044f. \u0411\u0430\u0440\u044c\u0435\u0440 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u0441\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e.<\/p>\n<h6>Exchanger<\/h6>\n<p>  \u0411\u0430\u0440\u044c\u0435\u0440 (`exchange()`) \u0434\u043b\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0432\u0443\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 volatile \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438.<\/p>\n<h6>Phaser<\/h6>\n<p>  \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 `CyclicBarrier`, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0446\u0438\u043a\u043b \u0431\u0430\u0440\u044c\u0435\u0440\u0430.<\/p>\n<h6>Semaphore<\/h6>\n<p>  \u0411\u0430\u0440\u044c\u0435\u0440, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0449\u0438\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u043a\u043e\u043b-\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440. \u041f\u043e \u0441\u0443\u0442\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b `Lock` \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0431\u043b\u043e\u043a\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c.<\/p>\n<h5>Executors<\/h5>\n<p>  <code>ExecutorService<\/code> \u043f\u0440\u0438\u0448\u0435\u043b \u043d\u0430 \u0437\u0430\u043c\u0435\u043d\u0443 <code>new Thread(runnable)<\/code> \u0447\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438. <code>ExecutorService<\/code> \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0432\u0448\u0438\u0435\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0438, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438\u0437 \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f \u043f\u0443\u043b\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438. \u0412\u043c\u0435\u0441\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 <code>Runnable<\/code> \u043f\u0443\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>Callable<\/code> (\u0443\u043c\u0435\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0438 \u043a\u0438\u0434\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438).<\/p>\n<pre><code class=\"java\">ExecutorService pool = Executors.newFixedThreadPool(4) Future future = pool.submit(new Callable() {     Object call() throws Exception {         println(&quot;In thread&quot;)         return &quot;From thread&quot;     } }) println(&quot;From main&quot;) println(future.get())  try {     pool.submit(new Callable() {         Object call() throws Exception {             throw new IllegalStateException()         }     }).get() } catch (ExecutionException e) {println(&quot;Got it: ${e.cause}&quot;)}  pool.shutdown() <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>invokeAll<\/code> \u043e\u0442\u0434\u0430\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u0437\u0432\u0430\u0432\u0448\u0435\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447. \u041c\u0435\u0442\u043e\u0434 <code>invokeAny<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0435\u0440\u0432\u043e\u0439 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043e\u0442\u043c\u0435\u043d\u044f\u044f \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435.<\/p>\n<h6>ThreadPoolExecutor<\/h6>\n<p>  \u041f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0447\u0435\u0435 \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b-\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043f\u0443\u043b\u0435, \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447.<\/p>\n<h6>ScheduledThreadPoolExecutor<\/h6>\n<p>  \u0420\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b <code>ThreadPoolExecutor<\/code> \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u043e \u0438\u043b\u0438 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e.<\/p>\n<h6>ThreadPoolExecutor<\/h6>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u043b\u0435\u0433\u043a\u0438\u0439 \u043f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u00ab\u0441\u0430\u043c\u043e\u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0449\u0438\u0445\u00bb \u0437\u0430\u0434\u0430\u0447. \u041f\u0443\u043b \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 `fork()` \u0438 `join()` \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0443 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439.<\/p>\n<pre><code class=\"java\">class LNode {     List&lt;LNode&gt; childs = []     def object }  class Finder extends RecursiveTask&lt;LNode&gt; {     LNode  node     Object expect      protected LNode compute() {         if (node?.object?.equals(expect)) {             return node         }         node?.childs?.collect {             new Finder(node: it, expect: expect).fork()         }?.collect {             it.join()         }?.find {             it != null         }     } }  ForkJoinPool es = new ForkJoinPool() def invoke = es.invoke(new Finder(         node: new LNode(                 childs: [                         new LNode(object: &quot;ivalid&quot;),                         new LNode(                                 object: &quot;ivalid&quot;,                                 childs: [new LNode(object: &quot;test&quot;)]                         )                 ]         ),         expect: &quot;test&quot; ))  print(&quot;${invoke?.object}&quot;) <\/code><\/pre>\n<h6>Accumulators _jdk 1.8_<\/h6>\n<p>  \u0410\u043a\u043a\u0443\u043c\u0443\u043b\u044f\u0442\u043e\u0440\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 (\u0441\u0443\u043c\u043c\u0430\/\u043f\u043e\u0438\u0441\u043a \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f) \u043d\u0430\u0434 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435 \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f CAS.   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/187854\/\"> http:\/\/habrahabr.ru\/post\/187854\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">       \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432 Java, \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u0433\u043e\u043d\u043e\u043a \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0418 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 \u043e\u0442 \u0433\u043e\u043d\u043e\u043a, \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043d\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438.<\/p>\n<p>  \u041f\u0430\u043a\u0435\u0442 <code>java.util.concurrent<\/code>, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432 HotSpot JDK, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430:  <\/p>\n<ul>\n<li>Atomic<\/li>\n<li>Locks<\/li>\n<li>Collections<\/li>\n<li>Synchronization points <\/li>\n<li>Executors<\/li>\n<li>Accumulators _jdk 1.8_<\/li>\n<\/ul>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-187854","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/187854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=187854"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/187854\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}