{"id":362335,"date":"2024-05-21T01:33:02","date_gmt":"2024-05-21T01:33:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=362335"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T01:33:02","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=362335","title":{"rendered":"<span>\u041f\u043e\u043b\u044c\u0437\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f?<br \/>\u0417\u0430\u0447\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u0438 \u0437\u0430\u0447\u0435\u043c \u0437\u043d\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b?<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c 10 \u0437\u0430\u0434\u0430\u0447. \u041a\u0430\u0436\u0434\u0430\u044f \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u0430 \u043e\u0442 1 \u0434\u043e 10, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0435\u043a\u0443\u043d\u0434, \u0440\u0430\u0432\u043d\u044b\u043c \u043d\u043e\u043c\u0435\u0440\u0443 \u0437\u0430\u0434\u0430\u0447\u0438. 1\u044f \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; 1 \u0441\u0435\u043a\u0443\u043d\u0434\u0430, 10\u044f \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; 10 \u0441\u0435\u043a\u0443\u043d\u0434 &#8212; \u043e\u043d\u0430 \u0441\u0430\u043c\u0430\u044f &#171;\u0442\u044f\u0436\u0435\u043b\u0430\u044f&#187;.<\/p>\n<p>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0437\u0430 55 \u0441\u0435\u043a\u0443\u043d\u0434. \u041a\u0430\u043a \u043c\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e? \u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432.<\/p>\n<ol>\n<li>\n<p>\u0421\u043b\u043e\u0436\u0438\u0432 \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435( 1+2+3&#8230;+10). \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u043a\u043b fori. \u0415\u0449\u0435 \u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f &#171;\u0421\u0443\u043c\u043c\u0430 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438&#187;.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0443\u043b\u0443 (n * (n+1)) \/ 2 (\u042d\u0442\u043e \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443, &#171;\u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430?&#187;). \u041e\u043d\u0430 \u0436\u0435 \u0441 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u044b\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c (n * (n+1)) >> 1<\/p>\n<\/li>\n<li>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0441\u043b\u043e\u0436\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0435 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0447\u0438\u0441\u043b\u0430, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c 11. 1 + 10 = 11. \u0414\u0430\u043b\u0435\u0435 2+9 \u0442\u043e\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 11, \u0438 \u0442\u0430\u043a \u0434\u043e 5+6=11. \u0422\u043e \u0435\u0441\u0442\u044c 11+11+11+11+11 = 11*(10\/2) = 11*5 = 55.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 &#8212; \u044d\u0442\u043e \u043e\u0434\u043d\u043e \u0438\u0437 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430 Leetcode \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u0433\u0434\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c &#171;Two pointers&#187; \u0438\u043b\u0438 &#171;\u0414\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f&#187;.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 5 \u044f\u0434\u0435\u0440 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439. \u041a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 \u0438 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0442\u0440\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u0434\u0435\u043b\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044f\u0434\u0435\u0440, \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0441\u0435\u0439\u0447\u0430\u0441. \u0422\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u043e 2 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u044f\u0434\u0440\u043e, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u043f\u043e\u0442\u043e\u043a\u0438. \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0432\u0448\u0438\u0435\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u0431\u0440\u0430\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0442\u0430\u043a \u0436\u0435 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443(\u0410\u043d\u0430\u043b\u043e\u0433 \u043f\u0443\u043b\u0430).<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435, \u043f\u043e\u0434\u0435\u043b\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u0435\u0436\u0434\u0443 \u044f\u0434\u0440\u0430\u043c\u0438, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044f\u0434\u0440\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c &#171;Two pointers&#187;(\u042d\u0442\u043e \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 &#171;\u0417\u0430\u0447\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u0437\u043d\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b?&#187;).<\/p>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u0430\u043a, \u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c!<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0434 \u0442\u0435\u043c\u0438 \u0436\u0435 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438, \u0447\u0442\u043e \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u041c\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u0435\u0436\u0434\u0443 \u044f\u0434\u0440\u0430\u043c\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u044f\u0434\u0440\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e 9 \u0438 10 \u0441\u0435\u043a\u0443\u043d\u0434, \u0447\u0442\u043e \u0432 \u0441\u0443\u043c\u043c\u0435 19. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u044f\u0434\u0440\u0430 \u0443\u0436\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 \u0441\u0432\u043e\u0438 \u0437\u0430\u0434\u0430\u0447\u0438, \u0438 \u043a\u0430\u043a \u0438\u0442\u043e\u0433 &#8212; \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u0435 \u0441\u0430\u043c\u044b\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u044f\u0434\u0440\u043e &#8212; 19 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0434\u0435\u0441\u044c 5\u0435 \u044f\u0434\u0440\u043e, \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0432 5 \u0441\u0435\u043a\u0443\u043d\u0434, \u0437\u0430\u0431\u0435\u0440\u0435\u0442 \u0441\u0435\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 10 \u0441\u0435\u043a\u0443\u043d\u0434, \u0438\u0442\u043e\u0433\u043e, \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0430\u0439\u043c\u0435\u0442 15 \u0441\u0435\u043a\u0443\u043d\u0434. \u041d\u0435\u043f\u043b\u043e\u0445\u043e.<\/p>\n<\/li>\n<li>\n<p>5 \u044f\u0434\u0435\u0440 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e 11 \u0441\u0435\u043a\u0443\u043d\u0434 (10 + 1, 9 + 2, 8 + 3, 7 + 4, 6 + 5). \u041a\u0430\u043a \u0438\u0442\u043e\u0433 &#8212; \u0432\u0441\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0439\u043c\u0435\u0442 11 \u0441\u0435\u043a\u0443\u043d\u0434. \u041b\u0443\u0447\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442!<\/p>\n<\/li>\n<\/ol>\n<blockquote>\n<p>\u041e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u043c\u044b\u0441\u043b\u044c \u0438\u0437 \u043a\u043d\u0438\u0433\u0438 &#171;<strong>Java concurrency in action<\/strong>&#171;!<br \/><strong><em>&#171;\u0420\u0435\u0430\u043b\u044c\u043d\u0430\u044f \u043e\u0442\u0434\u0430\u0447\u0430 \u043e\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445, \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e.&#187;<\/em><\/strong><\/p>\n<\/blockquote>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u043d\u0435 \u0438\u043c\u0435\u0435\u043c \u0434\u0435\u043b \u0441 &#171;\u0421\u0443\u043c\u043c\u043e\u0439 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438&#187;, \u0433\u0434\u0435 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u043c\u0435\u044e\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0430, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u0435\u0435, \u0438\u043c\u0435\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0435\u0439\u0441\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0443\u0442 \u0442\u0430\u043a \u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>[1, 3, 4, 4, 6, 7, 7, 8, 9, 10]<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0444\u043e\u0440\u043c\u0443\u043b\u044b, \u043d\u043e \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u044b \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0436\u0435\u043c. 59 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>19 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>16 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>13 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u043f\u044f\u0442\u044c <strong>\u043f\u043e\u0431\u0435\u0436\u0434\u0430\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u0434\u0430\u0447 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/strong><\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u044b\u0440\u0430\u0437\u0438\u043c \u044d\u0442\u043e \u0432 \u043a\u043e\u0434\u0435. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u044f \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Lombok.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435. \u041c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u043b\u0430\u0441\u044c. \u041d\u0430\u043c \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u044b \u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043d\u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 10 \u043b\u0435\u0442.<\/p>\n<p>\u041d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0441\u043e \u0441\u0432\u043e\u0435\u0439 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439. \u0421\u0435\u0439\u0447\u0430\u0441, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u044f\u0441\u043d\u043e.<\/p>\n<p>\u041e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0433\u0438\u0442\u0445\u0430\u0431 \u0441\u043e \u0432\u0441\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c. \u0422\u0430\u043a \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0441\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c <a href=\"https:\/\/github.com\/Borlok\/ForkJoinAndInvestors.git\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0440\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. Investor, Investment, ExecutionMethod. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043d\u0443\u0436\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430.<\/p>\n<pre><code class=\"java\">@Data public class ExecutionMethod {    private String name;    private long time;    private String description;   public ExecutionMethod(String name, String description) {       this.name = name;       this.description = description;   }      public void represent() {       System.out.println(name + \". \" + description); \/\/ TODO Delete   }      public void showExecutionTime() {       System.out.println(name + \" \" + time + \" ms.\"); \/\/ TODO Delete   } }  @Data @NoArgsConstructor @AllArgsConstructor public class Investor {    private String name;    private List investments;  }  @Data @NoArgsConstructor @AllArgsConstructor public class Investment {    private String from;    private String to;    private Long price;  }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0430. BusinessLogic (\u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443) \u0438 MyPhaser (\u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u0441 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0435\u0439).<\/p>\n<pre><code class=\"java\">public class BusinessLogic {   public static void handle(List&lt;Investor> investors) {       try {           System.out.println(\"\u0418\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432: \" + investors.size() + \" \u0441 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439: \" + investors.stream().map(x -> x.getInvestments().size()).collect(Collectors.toList())); \/\/ TODO Delete           for (Investor investor : investors)               for (int j = 0; j &lt; investor.getInvestments().size(); j++)                   Thread.sleep(1);       } catch (InterruptedException e) {           e.printStackTrace();           throw new RuntimeException(e);       }   }      public static List&lt;Investor> getUnhandledInvestors(int investorsNum, int investmentNum) {       List&lt;Investor> investors = new ArrayList&lt;>();       for (int i = 0; i &lt; investorsNum; i++) {           List&lt;Investment> investments = new ArrayList&lt;>();           Investor investor = new Investor(\"Some Investor\", investments);           int investmentsNumber = (int) ((Math.random()) * investmentNum);           for (int j = 0; j &lt; investmentsNumber; j++)               investments.add(new Investment(\"Good company\", \"somebody\", 1000L));           investors.add(investor);       }       return investors;   } }<\/code><\/pre>\n<pre><code class=\"java\">public class MyPhaser {    private static Phaser phaser = new Phaser();      public static void awaitAdvance() {       phaser.arriveAndAwaitAdvance();   }      public static void deregister() {       phaser.arriveAndDeregister();   }      public static void register() {       if (phaser.isTerminated())           phaser = new Phaser(0);       phaser.register();   } }<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u043c getUnhandledInvestors(&#8230;), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432 \u043c\u0435\u0442\u043e\u0434 handle(&#8230;).<br \/>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 handle(&#8230;) \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0443 1\u043c\u0441 \u0434\u043b\u044f \u0441\u0438\u043c\u0443\u043b\u044f\u0446\u0438\u0438 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0434\u0435\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u0423 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 1024 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u043b\u0438 \u043e\u0442 0 \u0434\u043e 120 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0437\u0430 10 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u043b\u0435\u0442. (120 &#8212; \u044d\u0442\u043e 12 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u043d\u0430 10 \u043b\u0435\u0442. \u041f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043b\u0430\u0442\u0435\u0436\u0443 \u0432 \u043c\u0435\u0441\u044f\u0446.)<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0437\u0435\u0440\u0430, \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e. \u041e\u043d \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043d\u0430\u043c \u043f\u043e\u0437\u0436\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0440\u0430\u0437\u0443.<\/p>\n<pre><code class=\"java\">public static void main(String[] args) {     List&lt;Investor> investors = BusinessLogic.getUnhandledInvestors(1024, 120);     MyPhaser.register();     ExecutionMethod syncHandle = new ExecutionMethod(\"\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");          syncHandle.represent();     syncHandle.setTime(syncHandle(investors));     MyPhaser.deregister(); }  private static long syncHandle(List&lt;Investor> investors) {     long start = System.currentTimeMillis(); \/\/ TODO Delete     BusinessLogic.handle(investors);     long end = System.currentTimeMillis(); \/\/ TODO Delete     System.out.println(\"Result: \" + (end - start) + \" ms\\n\"); \/\/ TODO Delete     return (end - start); } <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ebe\/67a\/513\/ebe67a5130eb3d993eefa44a9e1e2b80.png\" alt=\"\" title=\"\" width=\"2056\" height=\"330\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ebe\/67a\/513\/ebe67a5130eb3d993eefa44a9e1e2b80.png\"\/><\/figure>\n<p>\u041d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0438\u043b\u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439, \u0436\u0434\u0430\u0442\u044c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u0434\u043e\u043b\u044c\u0448\u0435.<\/p>\n<p>\u041c\u044b \u0437\u043d\u0430\u0435\u043c \u0447\u0442\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u044b \u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0438 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439. \u041e\u043d\u0438 \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0438 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442\u0441\u044f.<\/p>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c ForkJoin Framework \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u043d\u0430\u0448\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445. <strong>\u0413\u043b\u0430\u0432\u043d\u044b\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435\u043c ForkJoin \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u043e\u043d \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u044f\u0434\u0440\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430, \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0432\u0430\u043d\u0438\u044f \u0438 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043f\u043e \u044f\u0434\u0440\u0430\u043c, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0430 \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435\u0432 \u043e\u0434\u043d\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u0440\u0443\u0433\u0438\u043c\u0438.<\/strong><\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430\u0448\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u041d\u0430 \u043c\u043e\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 5 \u044f\u0434\u0435\u0440, \u043f\u043e\u0442\u043e\u043c\u0443 \u044f \u0432\u044b\u0431\u0440\u0430\u043b 5. \u0423\u0437\u043d\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u0432 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043c Runtime.getRuntime().availableProcessors().<\/p>\n<pre><code class=\"java\">public class TaskManager {    private final List&lt;Investor> tasks;    private final int CORES = 5;    private final ForkJoinPool pool = new ForkJoinPool(CORES);      public TaskManager(List&lt;Investor> tasks) {       this.tasks = new ArrayList&lt;>(tasks);   }   public void orderedExec(int thresholdSize, int parts) {       MyPhaser.register();       pool.submit(new AsyncOrderedTask(tasks, Math.max(100, thresholdSize), parts));   } }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 orderedExec(&#8230;) \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 thresholdSize &#8212; \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 parts &#8212; \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u0435\u0439 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0442\u044c \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u043d\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b, \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0430\u0448\u0443 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443.<\/p>\n<pre><code class=\"java\">public class AsyncOrderedTask extends RecursiveAction {    private final List&lt;Investor> investors;    private final int threshold;    private final int parts;    public AsyncOrderedTask(List&lt;Investor> investors, int threshold, int parts) {       this.investors = investors;       this.threshold = threshold;       this.parts = parts;   }      @Override   protected void compute() {       try {           if (investors.size() &lt; threshold)               someBusinessLogic();           else               divide();       } finally {           MyPhaser.deregister();       }   }    private void someBusinessLogic() {       BusinessLogic.handle(investors);   }    private void divide() {       int size = investors.size(), remains = size % parts, step = size \/ parts + remains;       System.out.printf(\"All size = %d | Step = %d | Parts = %d\\n\", size, step, parts); \/\/ TODO Delete       for (int i = 0; i &lt; size; i += step) {           MyPhaser.register();           int max = Math.min(size, i + step);           new AsyncOrderedTask(investors.subList(i, max), threshold, parts).fork();       }   } }<\/code><\/pre>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043c\u0435\u0442\u043e\u0434 divide(). \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0448\u0430\u0433 \u0446\u0438\u043a\u043b\u0430 (step). \u0412 \u0446\u0438\u043a\u043b\u0435 fori \u043c\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0430 \u0444\u0430\u0437\u0435\u0440\u0430 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>remains &#8212; \u044d\u0442\u043e \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043e\u0442 \u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u0434\u043b\u044f \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u044f\u0434\u0440\u0430\u043c. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 1000 \u043d\u0430 5 \u043c\u044b \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0435\u043b\u0438\u043c, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u043e 200 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u043d\u0430 \u044f\u0434\u0440\u043e.<\/p>\n<p>\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 1024 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u043c\u044b \u0431\u0435\u0437 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043d\u0435 \u0443\u0447\u0442\u0435\u043c \u043e\u0441\u0442\u0430\u0442\u043e\u043a, \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u043c \u044f\u0434\u0440\u043e\u043c \u0441\u0432\u043e\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 204 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430, \u043e\u043d\u043e \u0432\u043e\u0437\u044c\u043c\u0435\u0442 \u0435\u0449\u0435 4\u0445 \u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u044b\u0445 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0420\u0430\u0437\u0431\u0438\u0432\u043a\u0430 \u0431\u0435\u0437 \u0443\u0447\u0435\u0442\u0430 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<br \/> 1 \u044f\u0434\u0440\u043e. 204 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 2 \u044f\u0434\u0440\u043e. 204 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 3 \u044f\u0434\u0440\u043e. 204 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 4 \u044f\u0434\u0440\u043e. 204 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 5 \u044f\u0434\u0440\u043e. 204 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 1 \u044f\u0434\u0440\u043e. 4 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<\/p>\n<p>\u0421 \u0443\u0447\u0435\u0442\u043e\u043c \u0436\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430, \u044f\u0434\u0440\u0430 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<br \/> 1 \u044f\u0434\u0440\u043e. 208 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 2 \u044f\u0434\u0440\u043e. 208 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 3 \u044f\u0434\u0440\u043e. 208 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 4 \u044f\u0434\u0440\u043e. 208 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<br \/> 5 \u044f\u0434\u0440\u043e. 192 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430<\/p>\n<p>\u0413\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0443\u0447\u0448\u0435! \u0422\u0430\u043a \u0436\u0435 \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 &#171;int max = Math.min(size, i + step);&#187;. \u042d\u0442\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u044b \u043d\u0435 \u0432\u044b\u0439\u0442\u0438 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. &#171;5 \u044f\u0434\u0440\u043e. 192 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430&#187; \u043a\u0430\u043a \u0440\u0430\u0437 \u043d\u0430\u043c \u044d\u0442\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442.<\/p>\n<p>\u0421 \u0440\u0430\u0437\u0431\u043e\u0440\u043e\u043c \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0442\u0435\u0441\u0442\u0430\u043c!<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043f\u043e\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0433\u043e \u0443\u0431\u0435\u0440\u0435\u043c.<\/p>\n<pre><code class=\"java\">public static void main(String[] args) {     List&lt;Investor> investors = BusinessLogic.getUnhandledInvestors(1024, 120);     MyPhaser.register();     ExecutionMethod commonMethodRandom = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");      commonMethodRandom.represent();     commonMethodRandom.setTime(commonMethod(investors, 5));     MyPhaser.deregister(); }   private static long commonMethod(List&lt;Investor> investors, int parts) {     TaskManager manager = new TaskManager(investors);     long start = System.currentTimeMillis();     manager.orderedExec(investors.size()  \/ 4, parts);     MyPhaser.awaitAdvance();     long end = System.currentTimeMillis();     System.out.println(\"Result: \" + (end - start) + \" ms\\n\"); \/\/ TODO Delete     return (end - start); } <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5e8\/cc7\/d7b\/5e8cc7d7b3cf8b7093540dbf0db0dd88.png\" width=\"2056\" height=\"586\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5e8\/cc7\/d7b\/5e8cc7d7b3cf8b7093540dbf0db0dd88.png\"\/><\/figure>\n<p>\u0421\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043f\u043b\u043e\u0445\u043e! \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e-\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043c\u0435\u0442\u043e\u0434 main \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<pre><code class=\"java\">public static void main(String[] args) {     List&lt;Investor> investors = BusinessLogic.getUnhandledInvestors(1024, 120);     MyPhaser.register();     ExecutionMethod commonMethodRandom = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod commonMethodOrdered = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u0425\u0443\u0434\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");      commonMethodRandom.represent();     commonMethodRandom.setTime(commonMethod(investors, 5));      investors.sort(Comparator.comparingInt(dividedStep -> dividedStep.getInvestments().size()));      commonMethodOrdered.represent();     commonMethodOrdered.setTime(commonMethod(investors, 5));      MyPhaser.deregister(); }  <\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/37f\/3e2\/0ed\/37f3e20edd9ccd31d36f0af2c611ffb2.png\" width=\"2056\" height=\"910\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/37f\/3e2\/0ed\/37f3e20edd9ccd31d36f0af2c611ffb2.png\"\/><\/figure>\n<p>\u0423\u0436\u0435 \u043d\u0435 \u0442\u0430\u043a \u0432\u0435\u0441\u0435\u043b\u043e, \u043d\u0435 \u043f\u0440\u0430\u0432\u0434\u0430 \u043b\u0438?<\/p>\n<p>\u0412\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u043d\u0430\u043c \u0438 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432. \u0412\u00a0\u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u00abTwo pointers\u00bb.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0431\u0438\u0442\u0438\u044f \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<pre><code class=\"java\">public class AsyncTwoPointerTask extends RecursiveAction {   private final List&lt;Investor> investors;   private final int threshold;    private final int parts;    private final boolean asPool;      public AsyncTwoPointerTask(List&lt;Investor> investors, int threshold, int parts, boolean asPool) {         this.investors = investors;         this.threshold = threshold;         this.parts = parts;         this.asPool = asPool;   }    @Override   protected void compute() {         try {             if (investors.size() &lt; threshold)                 someBusinessLogic();             else                 divide();         } finally {             MyPhaser.deregister();         }     }      private void someBusinessLogic() {         BusinessLogic.handle(investors);     }      private void divide() {         int size = investors.size(), remains = size % parts, step = size \/ parts, dividedStep = step \/ 2, leftEnd = 0, leftStart, rightStart = size, rightEnd;         System.out.printf(\"All size = %d | Step = %d | Parts = %d\\n\", size, asPool ? step \/ 2 : step, asPool ? parts * 2 : parts); \/\/ TODO Delete         while (leftEnd &lt; rightStart) {             MyPhaser.register();             leftStart = leftEnd;             rightEnd = rightStart;             leftEnd += Math.min(rightEnd - leftStart - dividedStep, dividedStep + remains);             rightStart -= dividedStep;             if (asPool) {                 MyPhaser.register();                 new AsyncTwoPointerTask(investors.subList(leftStart, leftEnd), threshold, parts, asPool).fork();                 new AsyncTwoPointerTask(investors.subList(rightStart, rightEnd), threshold, parts, asPool).fork();             } else                 new AsyncTwoPointerTask(                         Stream.of(investors.subList(leftStart, leftEnd), investors.subList(rightStart, rightEnd))                                 .flatMap(Collection::stream).collect(Collectors.toList()), threshold, parts, asPool).fork();         }     } }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 divide() \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u043a \u0446\u0435\u043d\u0442\u0440\u0443. \u0421\u043b\u0435\u0432\u0430 \u043e\u043d \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0441 \u0441\u0430\u043c\u044b\u043c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439, \u0441\u043f\u0440\u0430\u0432\u0430 &#8212; \u0441 \u0441\u0430\u043c\u044b\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043c.<\/p>\n<p>\u041f\u043e\u0442\u043e\u043c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0438\u0445 \u0438 \u043e\u0442\u0434\u0430\u0435\u0442 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u043c \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u044f\u0434\u0440\u043e.<\/p>\n<p>\u0412\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0432\u00a0\u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441 \u00ab\u0421\u0443\u043c\u043c\u043e\u0439 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438\u00bb, \u043e \u0447\u0435\u043c \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u0440\u0430\u043d\u0435\u0435. 11+11+11+11+11=55.<\/p>\n<blockquote>\n<p><em>\u0422\u0430\u043a \u0436\u0435 \u043f\u0440\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f asPool, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0438\u0437 \u0432\u043d\u0435. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u043f\u0443\u043b\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0433\u0434\u0435 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 &#8212; \u0431\u0435\u0440\u0435\u0442 \u0434\u0440\u0443\u0433\u0443\u044e (\u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u043f\u0443\u043b\u0435). \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0438\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0443\u043b \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 AsyncOrderedTask, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 main \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0447\u0430\u0441\u0442\u0435\u0439, \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432. \u042d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u043f\u043e\u043b\u043d\u043e\u043c \u043a\u043e\u0434\u0435, \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438, \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043c\u044b \u0438\u0445 \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/em><\/p>\n<\/blockquote>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0432 TaskManager.<\/p>\n<pre><code class=\"java\">public void twoPointersExec(int thresholdSize, boolean asPool) {     MyPhaser.register();     pool.submit(new AsyncTwoPointerTask(tasks, Math.max(100, thresholdSize), CORES, asPool)); }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0432 \u043c\u0435\u0442\u043e\u0434 main \u0438 \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"java\">public static void main(String[] args) {     List&lt;Investor> investors = BusinessLogic.getUnhandledInvestors(1024, 120);     MyPhaser.register();     ExecutionMethod commonMethodRandom = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod commonMethodOrdered = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u0425\u0443\u0434\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");     ExecutionMethod twoPointerExecutionRandom = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 Two Pointers\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod twoPointerExecutionOrdered = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 Two Pointers\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");      commonMethodRandom.represent();     commonMethodRandom.setTime(commonMethod(investors, 5));      twoPointerExecutionRandom.represent();     twoPointerExecutionRandom.setTime(twoPointerExecution(investors, false));      investors.sort(Comparator.comparingInt(dividedStep -> dividedStep.getInvestments().size()));      commonMethodOrdered.represent();     commonMethodOrdered.setTime(commonMethod(investors, 5));      twoPointerExecutionOrdered.represent();     twoPointerExecutionOrdered.setTime(twoPointerExecution(investors, false));      MyPhaser.deregister(); }   private static long twoPointerExecution(List&lt;Investor> investors, boolean asPool) {     TaskManager manager = new TaskManager(investors);     long start = System.currentTimeMillis();     manager.twoPointersExec(investors.size() \/ 4, asPool);     MyPhaser.awaitAdvance();     long end = System.currentTimeMillis();     System.out.println(\"Result: \" + (end - start) + \" ms\\n\"); \/\/ TODO Delete     return (end - start); }<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bd8\/a2e\/e98\/bd8a2ee98afa94fde4a5fdb8385e4d07.png\" width=\"2056\" height=\"1632\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bd8\/a2e\/e98\/bd8a2ee98afa94fde4a5fdb8385e4d07.png\"\/><\/figure>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0442\u043e\u00a0\u043f\u0440\u0438\u00a0\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439, \u043f\u0440\u0438\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u00abTwo pointers\u00bb, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u0430 \u043f\u043e\u00a0\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a\u00a0\u043e\u0431\u044b\u0447\u043d\u043e\u043c\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044e.<\/p>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0442\u044c \u0432\u00a0\u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438, \u043d\u043e\u00a0\u043d\u0435\u00a0\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e. \u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447, \u043f\u0440\u0438\u00a0\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439, \u043c\u044b \u0442\u0430\u043a\u00a0\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e. \u041f\u043e\u0442\u043e\u043c\u0443 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u043c\u0438 \u0438\u0445 \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438\u00a0\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u043c\u044b \u0432\u0438\u0434\u0438\u043c\u00a0\u043b\u0438\u0431\u043e, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u00a0\u043b\u0438\u0431\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e <strong>\u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c, \u043d\u043e\u00a0\u0438 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442\u00a0\u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0438 \u00ab\u0442\u044f\u0436\u0435\u0441\u0442\u0438\u00bb \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/strong><\/p>\n<p>\u041c\u044b \u0440\u0430\u0441\u043a\u0440\u044b\u043b\u0438 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0443 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b!<\/p>\n<p>\u0422\u0430\u043a\u00a0\u0436\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u0431\u0438\u044f \u043f\u0443\u043b\u0430. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u043c \u043c\u0435\u0442\u043e\u0434 main \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<pre><code class=\"java\">public static void main(String[] args) {     List&lt;Investor> investors = BusinessLogic.getUnhandledInvestors(1024, 120);     MyPhaser.register();     ExecutionMethod syncHandle = new ExecutionMethod(\"\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod commonMethodRandom = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod commonMethodRandomPool = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u043f\u043e \u0442\u0438\u043f\u0443 \u043f\u0443\u043b\u0430 (\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod twoPointerExecutionRandom = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 Two Pointers\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod twoPointerExecutionRandomPool = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 Two Pointers \u043f\u043e \u0442\u0438\u043f\u0443 \u043f\u0443\u043b\u0430\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");     ExecutionMethod commonMethodOrdered = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 (\u0425\u0443\u0434\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");     ExecutionMethod commonMethodOrderedPool = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u043f\u043e \u0442\u0438\u043f\u0443 \u043f\u0443\u043b\u0430 (\u0425\u0443\u0434\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043f\u0443\u043b\u043e\u043c)\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");     ExecutionMethod twoPointerExecutionOrdered = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 Two Pointers\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");     ExecutionMethod twoPointerExecutionOrderedPool = new ExecutionMethod(\"\u041c\u0435\u0442\u043e\u0434 \u0441 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 Two Pointers \u043f\u043e \u0442\u0438\u043f\u0443 \u043f\u0443\u043b\u0430\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [1,2,3,4,5,6,7,8,9,10]\");      syncHandle.represent();     syncHandle.setTime(syncHandle(investors));      commonMethodRandom.represent();     commonMethodRandom.setTime(commonMethod(investors, 5));      commonMethodRandomPool.represent();     commonMethodRandomPool.setTime(commonMethod(investors, 10));      twoPointerExecutionRandom.represent();     twoPointerExecutionRandom.setTime(twoPointerExecution(investors, false));      twoPointerExecutionRandomPool.represent();     twoPointerExecutionRandomPool.setTime(twoPointerExecution(investors, true));      investors.sort(Comparator.comparingInt(dividedStep -> dividedStep.getInvestments().size()));      commonMethodOrdered.represent();     commonMethodOrdered.setTime(commonMethod(investors, 5));      commonMethodOrderedPool.represent();     commonMethodOrderedPool.setTime(commonMethod(investors, 10));      twoPointerExecutionOrdered.represent();     twoPointerExecutionOrdered.setTime(twoPointerExecution(investors, false));      twoPointerExecutionOrderedPool.represent();     twoPointerExecutionOrderedPool.setTime(twoPointerExecution(investors, true));      System.out.println(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:\"); \/\/ TODO Delete     commonMethodRandom.showExecutionTime();     twoPointerExecutionRandom.showExecutionTime();     twoPointerExecutionRandomPool.showExecutionTime();     commonMethodOrdered.showExecutionTime();     commonMethodOrderedPool.showExecutionTime();     twoPointerExecutionOrdered.showExecutionTime();     twoPointerExecutionOrderedPool.showExecutionTime();      System.out.println(\"\\n\u041d\u0430\u0448 \u043f\u043e\u0431\u0435\u0434\u0438\u0442\u0435\u043b\u044c:\"); \/\/ TODO Delete     ExecutionMethod method = Stream.of(commonMethodRandom,             twoPointerExecutionRandom,             twoPointerExecutionRandomPool,             commonMethodOrdered,             commonMethodOrderedPool,             twoPointerExecutionOrdered,             twoPointerExecutionOrderedPool).min(Comparator.comparingLong(ExecutionMethod::getTime))             .orElse(new ExecutionMethod(\"\u041f\u0443\u0441\u0442\u043e\u0439\", \"\u041f\u0443\u0441\u0442\u043e\u0439\"));     method.showExecutionTime();     MyPhaser.deregister(); } <\/code><\/pre>\n<p>\u0410 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b \u0443\u0436\u0435 \u0441\u0430\u043c\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \ud83d\ude42<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/779188\/\"> https:\/\/habr.com\/ru\/articles\/779188\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f?<br \/>\u0417\u0430\u0447\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u0438 \u0437\u0430\u0447\u0435\u043c \u0437\u043d\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b?<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c 10 \u0437\u0430\u0434\u0430\u0447. \u041a\u0430\u0436\u0434\u0430\u044f \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u0430 \u043e\u0442 1 \u0434\u043e 10, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0435\u043a\u0443\u043d\u0434, \u0440\u0430\u0432\u043d\u044b\u043c \u043d\u043e\u043c\u0435\u0440\u0443 \u0437\u0430\u0434\u0430\u0447\u0438. 1\u044f \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; 1 \u0441\u0435\u043a\u0443\u043d\u0434\u0430, 10\u044f \u0437\u0430\u0434\u0430\u0447\u0430 &#8212; 10 \u0441\u0435\u043a\u0443\u043d\u0434 &#8212; \u043e\u043d\u0430 \u0441\u0430\u043c\u0430\u044f &#171;\u0442\u044f\u0436\u0435\u043b\u0430\u044f&#187;.<\/p>\n<p>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0437\u0430 55 \u0441\u0435\u043a\u0443\u043d\u0434. \u041a\u0430\u043a \u043c\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e? \u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432.<\/p>\n<ol>\n<li>\n<p>\u0421\u043b\u043e\u0436\u0438\u0432 \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435( 1+2+3&#8230;+10). \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u043a\u043b fori. \u0415\u0449\u0435 \u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f &#171;\u0421\u0443\u043c\u043c\u0430 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438&#187;.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0443\u043b\u0443 (n * (n+1)) \/ 2 (\u042d\u0442\u043e \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443, &#171;\u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430?&#187;). \u041e\u043d\u0430 \u0436\u0435 \u0441 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u044b\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c (n * (n+1)) >> 1<\/p>\n<\/li>\n<li>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0441\u043b\u043e\u0436\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0435 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0447\u0438\u0441\u043b\u0430, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c 11. 1 + 10 = 11. \u0414\u0430\u043b\u0435\u0435 2+9 \u0442\u043e\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 11, \u0438 \u0442\u0430\u043a \u0434\u043e 5+6=11. \u0422\u043e \u0435\u0441\u0442\u044c 11+11+11+11+11 = 11*(10\/2) = 11*5 = 55.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 &#8212; \u044d\u0442\u043e \u043e\u0434\u043d\u043e \u0438\u0437 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430 Leetcode \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u0433\u0434\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c &#171;Two pointers&#187; \u0438\u043b\u0438 &#171;\u0414\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f&#187;.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c 5 \u044f\u0434\u0435\u0440 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439. \u041a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 \u0438 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u0442\u0440\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u0434\u0435\u043b\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044f\u0434\u0435\u0440, \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0441\u0435\u0439\u0447\u0430\u0441. \u0422\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u043e 2 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u044f\u0434\u0440\u043e, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u043f\u043e\u0442\u043e\u043a\u0438. \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0432\u0448\u0438\u0435\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u0431\u0440\u0430\u0442\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0442\u0430\u043a \u0436\u0435 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443(\u0410\u043d\u0430\u043b\u043e\u0433 \u043f\u0443\u043b\u0430).<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435, \u043f\u043e\u0434\u0435\u043b\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u0435\u0436\u0434\u0443 \u044f\u0434\u0440\u0430\u043c\u0438, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044f\u0434\u0440\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c &#171;Two pointers&#187;(\u042d\u0442\u043e \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 &#171;\u0417\u0430\u0447\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u0437\u043d\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b?&#187;).<\/p>\n<\/li>\n<\/ol>\n<p>\u0418\u0442\u0430\u043a, \u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c!<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0434 \u0442\u0435\u043c\u0438 \u0436\u0435 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438, \u0447\u0442\u043e \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u041c\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u0435\u0436\u0434\u0443 \u044f\u0434\u0440\u0430\u043c\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u044f\u0434\u0440\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e 9 \u0438 10 \u0441\u0435\u043a\u0443\u043d\u0434, \u0447\u0442\u043e \u0432 \u0441\u0443\u043c\u043c\u0435 19. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u044f\u0434\u0440\u0430 \u0443\u0436\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 \u0441\u0432\u043e\u0438 \u0437\u0430\u0434\u0430\u0447\u0438, \u0438 \u043a\u0430\u043a \u0438\u0442\u043e\u0433 &#8212; \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u0435 \u0441\u0430\u043c\u044b\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043d\u0430 \u044f\u0434\u0440\u043e &#8212; 19 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0434\u0435\u0441\u044c 5\u0435 \u044f\u0434\u0440\u043e, \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0432 5 \u0441\u0435\u043a\u0443\u043d\u0434, \u0437\u0430\u0431\u0435\u0440\u0435\u0442 \u0441\u0435\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 10 \u0441\u0435\u043a\u0443\u043d\u0434, \u0438\u0442\u043e\u0433\u043e, \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0430\u0439\u043c\u0435\u0442 15 \u0441\u0435\u043a\u0443\u043d\u0434. \u041d\u0435\u043f\u043b\u043e\u0445\u043e.<\/p>\n<\/li>\n<li>\n<p>5 \u044f\u0434\u0435\u0440 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e 11 \u0441\u0435\u043a\u0443\u043d\u0434 (10 + 1, 9 + 2, 8 + 3, 7 + 4, 6 + 5). \u041a\u0430\u043a \u0438\u0442\u043e\u0433 &#8212; \u0432\u0441\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0439\u043c\u0435\u0442 11 \u0441\u0435\u043a\u0443\u043d\u0434. \u041b\u0443\u0447\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442!<\/p>\n<\/li>\n<\/ol>\n<blockquote>\n<p>\u041e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u043c\u044b\u0441\u043b\u044c \u0438\u0437 \u043a\u043d\u0438\u0433\u0438 &#171;<strong>Java concurrency in action<\/strong>&#171;!<br \/><strong><em>&#171;\u0420\u0435\u0430\u043b\u044c\u043d\u0430\u044f \u043e\u0442\u0434\u0430\u0447\u0430 \u043e\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445, \u043e\u0434\u043d\u043e\u0440\u043e\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e.&#187;<\/em><\/strong><\/p>\n<\/blockquote>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u043d\u0435 \u0438\u043c\u0435\u0435\u043c \u0434\u0435\u043b \u0441 &#171;\u0421\u0443\u043c\u043c\u043e\u0439 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438&#187;, \u0433\u0434\u0435 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u043c\u0435\u044e\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0430, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u0435\u0435, \u0438\u043c\u0435\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0435\u0439\u0441\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0443\u0442 \u0442\u0430\u043a \u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>[1, 3, 4, 4, 6, 7, 7, 8, 9, 10]<\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0444\u043e\u0440\u043c\u0443\u043b\u044b, \u043d\u043e \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u044b \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0436\u0435\u043c. 59 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>19 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>16 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<li>\n<p>13 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u043f\u044f\u0442\u044c <strong>\u043f\u043e\u0431\u0435\u0436\u0434\u0430\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u0434\u0430\u0447 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/strong><\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u044b\u0440\u0430\u0437\u0438\u043c \u044d\u0442\u043e \u0432 \u043a\u043e\u0434\u0435. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u044f \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Lombok.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435. \u041c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043e\u0442\u043a\u0440\u044b\u043b\u0430\u0441\u044c. \u041d\u0430\u043c \u043f\u0440\u0438\u0441\u044b\u043b\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u044b \u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043d\u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 10 \u043b\u0435\u0442.<\/p>\n<p>\u041d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0441\u043e \u0441\u0432\u043e\u0435\u0439 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439. \u0421\u0435\u0439\u0447\u0430\u0441, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u044f\u0441\u043d\u043e.<\/p>\n<p>\u041e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0433\u0438\u0442\u0445\u0430\u0431 \u0441\u043e \u0432\u0441\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c. \u0422\u0430\u043a \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0441\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c <a href=\"https:\/\/github.com\/Borlok\/ForkJoinAndInvestors.git\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0440\u0438 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. Investor, Investment, ExecutionMethod. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u043d\u0443\u0436\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430.<\/p>\n<pre><code class=\"java\">@Data public class ExecutionMethod {    private String name;    private long time;    private String description;   public ExecutionMethod(String name, String description) {       this.name = name;       this.description = description;   }      public void represent() {       System.out.println(name + \". \" + description); \/\/ TODO Delete   }      public void showExecutionTime() {       System.out.println(name + \" \" + time + \" ms.\"); \/\/ TODO Delete   } }  @Data @NoArgsConstructor @AllArgsConstructor public class Investor {    private String name;    private List investments;  }  @Data @NoArgsConstructor @AllArgsConstructor public class Investment {    private String from;    private String to;    private Long price;  }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0430. BusinessLogic (\u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443) \u0438 MyPhaser (\u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u0441 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0435\u0439).<\/p>\n<pre><code class=\"java\">public class BusinessLogic {   public static void handle(List&lt;Investor> investors) {       try {           System.out.println(\"\u0418\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432: \" + investors.size() + \" \u0441 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439: \" + investors.stream().map(x -> x.getInvestments().size()).collect(Collectors.toList())); \/\/ TODO Delete           for (Investor investor : investors)               for (int j = 0; j &lt; investor.getInvestments().size(); j++)                   Thread.sleep(1);       } catch (InterruptedException e) {           e.printStackTrace();           throw new RuntimeException(e);       }   }      public static List&lt;Investor> getUnhandledInvestors(int investorsNum, int investmentNum) {       List&lt;Investor> investors = new ArrayList&lt;>();       for (int i = 0; i &lt; investorsNum; i++) {           List&lt;Investment> investments = new ArrayList&lt;>();           Investor investor = new Investor(\"Some Investor\", investments);           int investmentsNumber = (int) ((Math.random()) * investmentNum);           for (int j = 0; j &lt; investmentsNumber; j++)               investments.add(new Investment(\"Good company\", \"somebody\", 1000L));           investors.add(investor);       }       return investors;   } }<\/code><\/pre>\n<pre><code class=\"java\">public class MyPhaser {    private static Phaser phaser = new Phaser();      public static void awaitAdvance() {       phaser.arriveAndAwaitAdvance();   }      public static void deregister() {       phaser.arriveAndDeregister();   }      public static void register() {       if (phaser.isTerminated())           phaser = new Phaser(0);       phaser.register();   } }<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u043c getUnhandledInvestors(&#8230;), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432 \u043c\u0435\u0442\u043e\u0434 handle(&#8230;).<br \/>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 handle(&#8230;) \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0443 1\u043c\u0441 \u0434\u043b\u044f \u0441\u0438\u043c\u0443\u043b\u044f\u0446\u0438\u0438 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0434\u0435\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u0423 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 1024 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u043b\u0438 \u043e\u0442 0 \u0434\u043e 120 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439 \u0437\u0430 10 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u043b\u0435\u0442. (120 &#8212; \u044d\u0442\u043e 12 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u043d\u0430 10 \u043b\u0435\u0442. \u041f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043b\u0430\u0442\u0435\u0436\u0443 \u0432 \u043c\u0435\u0441\u044f\u0446.)<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0437\u0435\u0440\u0430, \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e. \u041e\u043d \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043d\u0430\u043c \u043f\u043e\u0437\u0436\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0440\u0430\u0437\u0443.<\/p>\n<pre><code class=\"java\">public static void main(String[] args) {     List&lt;Investor> investors = BusinessLogic.getUnhandledInvestors(1024, 120);     MyPhaser.register();     ExecutionMethod syncHandle = new ExecutionMethod(\"\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439\", \"\u041f\u0440\u0438\u043c\u0435\u0440: [4,1,10,6,2,9,3,8,7,5]\");          syncHandle.represent();     syncHandle.setTime(syncHandle(investors));     MyPhaser.deregister(); }  private static long syncHandle(List&lt;Investor> investors) {     long start = System.currentTimeMillis(); \/\/ TODO Delete     BusinessLogic.handle(investors);     long end = System.currentTimeMillis(); \/\/ TODO Delete     System.out.println(\"Result: \" + (end - start) + \" ms\\n\"); \/\/ TODO Delete     return (end - start); } <\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0438\u043b\u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439, \u0436\u0434\u0430\u0442\u044c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u0434\u043e\u043b\u044c\u0448\u0435.<\/p>\n<p>\u041c\u044b \u0437\u043d\u0430\u0435\u043c \u0447\u0442\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u044b \u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0438 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0438 \u0438\u043d\u0432\u0435\u0441\u0442\u0438\u0446\u0438\u0439. \u041e\u043d\u0438 \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0438 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442\u0441\u044f.<\/p>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c ForkJoin Framework \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u043d\u0430\u0448\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445. <strong>\u0413\u043b\u0430\u0432\u043d\u044b\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435\u043c ForkJoin \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u043e\u043d \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u044f\u0434\u0440\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430, \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0432\u0430\u043d\u0438\u044f \u0438 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043f\u043e \u044f\u0434\u0440\u0430\u043c, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0430 \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435\u0432 \u043e\u0434\u043d\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u0440\u0443\u0433\u0438\u043c\u0438.<\/strong><\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430\u0448\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u041d\u0430 \u043c\u043e\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 5 \u044f\u0434\u0435\u0440, \u043f\u043e\u0442\u043e\u043c\u0443 \u044f \u0432\u044b\u0431\u0440\u0430\u043b 5. \u0423\u0437\u043d\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u0432 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043c Runtime.getRuntime().availableProcessors().<\/p>\n<pre><code class=\"java\">public class TaskManager {    private final List&lt;Investor> tasks;    private final int CORES = 5;    private final ForkJoinPool pool = new ForkJoinPool(CORES);      public TaskManager(List&lt;Investor> tasks) {       this.tasks = new ArrayList&lt;>(tasks);   }   public void orderedExec(int thresholdSize, int parts) {       MyPhaser.register();       pool.submit(new AsyncOrderedTask(tasks, Math.max(100, thresholdSize), parts));   } }<\/code><\/pre>\n<p>\u0412 \u043c\u0435\u0442\u043e\u0434\u0435 orderedExec(&#8230;) \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 thresholdSize &#8212; \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 parts &#8212; \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u0435\u0439 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0442\u044c \u0438\u043d\u0432\u0435\u0441\u0442\u043e\u0440\u043e\u0432 \u043d\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b, \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043d\u0430\u0448\u0443 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443.<\/p>\n<pre><code class=\"java\">public class AsyncOrderedTask extends RecursiveAction {    private final List&lt;Investor> investors;    private final int threshold;    private final int parts;    public AsyncOrderedTask(List&lt;Investor> investors, int threshold, int parts) {       this.investors = investors;       this.threshold = threshold;       this.parts = parts;   }      @Override   protected void compute() {       try {           if (investors.size() &lt; threshold)               someBusinessLogic();           else               divide();       } finally {           MyPhaser.deregister();       }   }    private void someBusinessLogic() {       BusinessLogic.handle(investors);   }    private void<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-362335","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/362335","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=362335"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/362335\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}