{"id":3138,"date":"2022-06-15T05:27:58","date_gmt":"2022-06-15T05:27:58","guid":{"rendered":"http:\/\/the-codest.localhost\/blog\/concurrency-in-java-part-1-introduction\/"},"modified":"2026-03-11T05:59:41","modified_gmt":"2026-03-11T05:59:41","slug":"soubeznost-v-jave-cast-1-uvod","status":"publish","type":"post","link":"https:\/\/thecodest.co\/cs\/blog\/concurrency-in-java-part-1-introduction\/","title":{"rendered":"Soub\u011b\u017enost v jazyce Java - \u010d\u00e1st 1 - \u00favod"},"content":{"rendered":"<p>Obecn\u011b plat\u00ed, \u017ee konven\u010dn\u00ed programov\u00e1n\u00ed je sekven\u010dn\u00ed. V\u0161e v programu se d\u011bje postupn\u011b.<br>Ve skute\u010dnosti v\u0161ak paraleln\u011b b\u011b\u017e\u00ed cel\u00fd sv\u011bt - je to schopnost vykon\u00e1vat v\u00edce \u00faloh sou\u010dasn\u011b.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Vl\u00e1kno vs. proces<\/h2>\n\n\n\n<p>Diskutovat o pokro\u010dil\u00fdch t\u00e9matech, jako jsou <strong>soub\u011b\u017enost v <a href=\"https:\/\/thecodest.co\/cs\/blog\/top-programming-languages-to-build-e-commerce\/\">Java<\/a><\/strong> nebo multithreadingu, mus\u00edme se dohodnout na n\u011bkter\u00fdch spole\u010dn\u00fdch definic\u00edch, abychom si byli jisti, \u017ee jsme na stejn\u00e9 vln\u011b.<\/p>\n\n\n\n<p>Za\u010dn\u011bme od z\u00e1klad\u016f. V nesekven\u010dn\u00edm sv\u011bt\u011b m\u00e1me dva druhy reprezentant\u016f soub\u011bhu: procesy a soub\u011bhy.<br>z\u00e1vity. Proces je instance b\u011b\u017e\u00edc\u00edho programu. Obvykle je izolov\u00e1n od ostatn\u00edch proces\u016f.<br>Za p\u0159id\u011blov\u00e1n\u00ed prost\u0159edk\u016f jednotliv\u00fdm proces\u016fm je zodpov\u011bdn\u00fd opera\u010dn\u00ed syst\u00e9m. Krom\u011b toho funguje jako vodi\u010d, kter\u00fd<br>pl\u00e1nuje a kontroluje.<\/p>\n\n\n\n<p>Nit je druh procesu, ale na ni\u017e\u0161\u00ed \u00farovni, proto je tak\u00e9 zn\u00e1m\u00e1 jako lehk\u00e1 nit. V jednom vl\u00e1kn\u011b m\u016f\u017ee b\u011b\u017eet v\u00edce vl\u00e1ken<br>proces. Program zde funguje jako pl\u00e1nova\u010d a \u0159adi\u010d vl\u00e1ken. T\u00edmto zp\u016fsobem se jednotliv\u00e9 programy jev\u00ed jako<br>v\u00edce \u00fakol\u016f najednou.<\/p>\n\n\n\n<p>Z\u00e1kladn\u00edm rozd\u00edlem mezi vl\u00e1kny a procesy je \u00farove\u0148 izolace. Proces m\u00e1 svou vlastn\u00ed sadu<br>zat\u00edmco vl\u00e1kno sd\u00edl\u00ed <a href=\"https:\/\/thecodest.co\/cs\/blog\/app-data-collection-security-risks-value-and-types-explored\/\">data<\/a> s dal\u0161\u00edmi vl\u00e1kny. M\u016f\u017ee se to zd\u00e1t jako p\u0159\u00edstup n\u00e1chyln\u00fd k chyb\u00e1m a skute\u010dn\u011b tomu tak je. Pro<br>nyn\u00ed se na to nezam\u011b\u0159ujme, proto\u017ee to p\u0159esahuje r\u00e1mec tohoto \u010dl\u00e1nku.<\/p>\n\n\n\n<p>Procesy, vl\u00e1kna - dob\u0159e... Ale co p\u0159esn\u011b je soub\u011b\u017enost? Soub\u011b\u017enost znamen\u00e1, \u017ee m\u016f\u017eete prov\u00e1d\u011bt v\u00edce \u00faloh najednou.<br>\u010das. Neznamen\u00e1 to, \u017ee tyto \u00falohy mus\u00ed b\u011b\u017eet sou\u010dasn\u011b - k tomu slou\u017e\u00ed paralelismus. <strong>Concurrenc v Javay<\/strong> tak\u00e9 ne<br>vy\u017eadovat v\u00edce procesor\u016f nebo dokonce v\u00edce jader. Toho lze dos\u00e1hnout v prost\u0159ed\u00ed s jedn\u00edm j\u00e1drem s vyu\u017eit\u00edm<br>p\u0159ep\u00edn\u00e1n\u00ed kontextu.<\/p>\n\n\n\n<p>Term\u00ednem souvisej\u00edc\u00edm se soub\u011b\u017enost\u00ed je multithreading. Jedn\u00e1 se o vlastnost program\u016f, kter\u00e1 jim umo\u017e\u0148uje vykon\u00e1vat n\u011bkolik \u00faloh najednou. Ne ka\u017ed\u00fd program tento p\u0159\u00edstup pou\u017e\u00edv\u00e1, ale ty, kter\u00e9 jej pou\u017e\u00edvaj\u00ed, lze ozna\u010dit za v\u00edcevl\u00e1knov\u00e9.<\/p>\n\n\n\n<p>Jsme t\u00e9m\u011b\u0159 p\u0159ipraveni, u\u017e jen jedna definice. Asynchronnost znamen\u00e1, \u017ee program prov\u00e1d\u00ed neblokuj\u00edc\u00ed operace.<br>Iniciuje \u00fakol a pak se v\u011bnuje dal\u0161\u00edm \u010dinnostem, zat\u00edmco \u010dek\u00e1 na odpov\u011b\u010f. Kdy\u017e odpov\u011b\u010f dostane, m\u016f\u017ee <a href=\"https:\/\/thecodest.co\/cs\/blog\/react-development-all-you-have-to-know\/\">reagovat<\/a> k n\u011bmu.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">V\u0161echen ten jazz<\/h2>\n\n\n\n<p>Ve v\u00fdchoz\u00edm nastaven\u00ed je ka\u017ed\u00fd <strong>Aplikace Java<\/strong> b\u011b\u017e\u00ed v jednom procesu. V tomto procesu je jedno vl\u00e1kno souvisej\u00edc\u00ed s procesem <code>main()<\/code> metoda<br>aplikace. Jak v\u0161ak bylo uvedeno, je mo\u017en\u00e9 vyu\u017e\u00edt mechanismy v\u00edce vl\u00e1ken v r\u00e1mci jednoho vl\u00e1kna.<br>program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Spustiteln\u00e9<\/h3>\n\n\n\n<p><code>Vl\u00e1kno<\/code> je <strong>Java<\/strong> t\u0159\u00edda, ve kter\u00e9 se odehr\u00e1v\u00e1 kouzlo. Jedn\u00e1 se o objektovou reprezentaci v\u00fd\u0161e zm\u00edn\u011bn\u00e9ho vl\u00e1kna. Na adresu<br>vytvo\u0159it vlastn\u00ed vl\u00e1kno, m\u016f\u017eete roz\u0161\u00ed\u0159it <code>Vl\u00e1kno<\/code> t\u0159\u00edda. Nen\u00ed to v\u0161ak doporu\u010den\u00fd p\u0159\u00edstup. <code>Vl\u00e1kna<\/code> by m\u011bl b\u00fdt pou\u017eit jako mechanismus pro spu\u0161t\u011bn\u00ed \u00falohy. \u00dalohy jsou \u010d\u00e1sti <a href=\"https:\/\/thecodest.co\/cs\/dictionary\/what-is-code-refactoring\/\">k\u00f3d<\/a> kter\u00e9 chceme spustit v soub\u011b\u017en\u00e9m re\u017eimu. M\u016f\u017eeme je definovat pomoc\u00ed p\u0159\u00edkazu <code>Spustiteln\u00e9<\/code> rozhran\u00ed.<\/p>\n\n\n\n<p>Ale dost teorie, poj\u010fme se pod\u00edvat na n\u00e1\u0161 k\u00f3d.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Probl\u00e9m<\/h3>\n\n\n\n<p>P\u0159edpokl\u00e1dejme, \u017ee m\u00e1me n\u011bkolik pol\u00ed \u010d\u00edsel. Pro ka\u017ed\u00e9 pole chceme zn\u00e1t sou\u010det \u010d\u00edsel v poli. Nech\u0165<br>p\u0159edst\u00edrat, \u017ee takov\u00fdch pol\u00ed je mnoho a ka\u017ed\u00e9 z nich je pom\u011brn\u011b velk\u00e9. Za takov\u00fdch podm\u00ednek chceme vyu\u017e\u00edt soub\u011bhu a ka\u017ed\u00e9 pole se\u010d\u00edst jako samostatnou \u00falohu.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">int[] a1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\nint[] a2 = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};\nint[] a3 = {3, 4, 3, 4, 3, 4, 2, 1, 3, 7};\n\nRunnable task1 = () -&gt; {\n    int sum = Arrays.stream(a1).sum();\n    System.out.println(\"1. Sou\u010det je: \" + sum);\n};\n\nRunnable task2 = () -&gt; {\n    int sum = Arrays.stream(a2).sum();\n    System.out.println(\"2. Sou\u010det je: \" + sum);\n};\n\nRunnable task3 = () -&gt; {\n    int sum = Arrays.stream(a3).sum();\n    System.out.println(\"3. Sou\u010det je: \" + sum);\n};\n\nnew Thread(task1).start();\nnew Thread(task2).start();\nnew Thread(task3).start();<\/code><\/pre>\n\n\n\n<p>Jak vid\u00edte z v\u00fd\u0161e uveden\u00e9ho k\u00f3du <code>Spustiteln\u00e9<\/code> je funk\u010dn\u00ed rozhran\u00ed. Obsahuje jedinou abstraktn\u00ed metodu <code>run()<\/code><br>bez argument\u016f. Adresa <code>Spustiteln\u00e9<\/code> rozhran\u00ed by m\u011blo b\u00fdt implementov\u00e1no ka\u017edou t\u0159\u00eddou, jej\u00ed\u017e instance maj\u00ed b\u00fdt<br>prov\u00e1d\u011bn\u00e9 vl\u00e1knem.<\/p>\n\n\n\n<p>Po definov\u00e1n\u00ed \u00falohy m\u016f\u017eete vytvo\u0159it vl\u00e1kno pro jej\u00ed spu\u0161t\u011bn\u00ed. Toho lze dos\u00e1hnout pomoc\u00ed <code>new Thread()<\/code> konstruktor, kter\u00fd<br>bere <code>Spustiteln\u00e9<\/code> jako argument.<\/p>\n\n\n\n<p>Posledn\u00edm krokem je <code>start()<\/code> nov\u011b vytvo\u0159en\u00e9 vl\u00e1kno. V <a href=\"https:\/\/thecodest.co\/cs\/blog\/compare-staff-augmentation-firms-that-excel-in-api-team-staffing-for-financial-technology-projects\/\">API<\/a> existuj\u00ed tak\u00e9 <code>run()<\/code> metody v <code>Spustiteln\u00e9<\/code> a v<br><code>Vl\u00e1kno<\/code>. To v\u0161ak nen\u00ed zp\u016fsob, jak v Jav\u011b vyu\u017e\u00edt soub\u011b\u017enost. P\u0159\u00edm\u00e9 vol\u00e1n\u00ed ka\u017ed\u00e9 z t\u011bchto metod vede k tomu, \u017ee se<br>prov\u00e1d\u011bn\u00ed \u00falohy ve stejn\u00e9m vl\u00e1kn\u011b <code>main()<\/code> metoda se spust\u00ed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fondy vl\u00e1ken a exekuto\u0159i<\/h3>\n\n\n\n<p>P\u0159i velk\u00e9m po\u010dtu \u00faloh nen\u00ed dobr\u00e9 vytv\u00e1\u0159et pro ka\u017edou z nich samostatn\u00e9 vl\u00e1kno. Vytvo\u0159en\u00ed <code>Vl\u00e1kno<\/code> je<br>t\u011b\u017ekou operaci a je mnohem lep\u0161\u00ed znovu pou\u017e\u00edt st\u00e1vaj\u00edc\u00ed vl\u00e1kna ne\u017e vytv\u00e1\u0159et nov\u00e1.<\/p>\n\n\n\n<p>Pokud program vytv\u00e1\u0159\u00ed mnoho kr\u00e1tkodob\u00fdch vl\u00e1ken, je lep\u0161\u00ed pou\u017e\u00edt fond vl\u00e1ken. Fond vl\u00e1ken obsahuje \u0159adu<br>vl\u00e1kna p\u0159ipraven\u00e1 ke spu\u0161t\u011bn\u00ed, ale v sou\u010dasn\u00e9 dob\u011b neaktivn\u00ed. Poskytnut\u00ed a <code>Spustiteln\u00e9<\/code> do fondu zp\u016fsob\u00ed, \u017ee jedno z vl\u00e1ken zavol\u00e1 p\u0159\u00edkaz<br><code>run()<\/code> metoda dan\u00e9ho <code>Spustiteln\u00e9<\/code>. Po dokon\u010den\u00ed \u00falohy vl\u00e1kno st\u00e1le existuje a je v ne\u010dinn\u00e9m stavu.<\/p>\n\n\n\n<p>Dob\u0159e, ch\u00e1pete to - m\u00edsto ru\u010dn\u00edho vytv\u00e1\u0159en\u00ed rad\u011bji fond vl\u00e1ken. Ale jak m\u016f\u017eete vyu\u017e\u00edt pooly vl\u00e1ken? Na adrese <code>Exekuto\u0159i<\/code><br>t\u0159\u00edda m\u00e1 \u0159adu statick\u00fdch tov\u00e1rn\u00edch metod pro konstrukci poolu vl\u00e1ken. Nap\u0159\u00edklad <code>newCachedThredPool()<\/code> vytv\u00e1\u0159\u00ed<br>fond, ve kter\u00e9m se podle pot\u0159eby vytv\u00e1\u0159ej\u00ed nov\u00e1 vl\u00e1kna a ne\u010dinn\u00e1 vl\u00e1kna se uchov\u00e1vaj\u00ed po dobu 60 sekund. Naproti tomu,<br><code>newFixedThreadPool()<\/code> obsahuje pevnou mno\u017einu vl\u00e1ken, v n\u00ed\u017e jsou ne\u010dinn\u00e1 vl\u00e1kna uchov\u00e1v\u00e1na po neomezenou dobu.<\/p>\n\n\n\n<p>Pod\u00edvejme se, jak by to mohlo fungovat v na\u0161em p\u0159\u00edkladu. Nyn\u00ed ji\u017e nemus\u00edme vytv\u00e1\u0159et vl\u00e1kna ru\u010dn\u011b. M\u00edsto toho mus\u00edme vytvo\u0159it<br><code>ExecutorService<\/code> kter\u00fd poskytuje fond vl\u00e1ken. Pak mu m\u016f\u017eeme p\u0159i\u0159adit \u00falohy. Posledn\u00edm krokem je uzav\u0159en\u00ed vl\u00e1kna<br>pool, aby nedoch\u00e1zelo k \u00fanik\u016fm pam\u011bti. Zbytek p\u0159edchoz\u00edho k\u00f3du z\u016fst\u00e1v\u00e1 stejn\u00fd.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">ExecutorService executor = Executors.newCachedThreadPool();\n\nexecutor.submit(task1);\nexecutor.submit(task2);\nexecutor.submit(task3);\n\nexecutor.shutdown();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Volateln\u00fd<\/h3>\n\n\n\n<p><code>Spustiteln\u00e9<\/code> se zd\u00e1 b\u00fdt \u0161ikovn\u00fdm zp\u016fsobem vytv\u00e1\u0159en\u00ed soub\u011b\u017en\u00fdch \u00faloh, ale m\u00e1 jeden z\u00e1sadn\u00ed nedostatek. Nem\u016f\u017ee vr\u00e1tit \u017e\u00e1dnou<br>hodnotu. Nav\u00edc nem\u016f\u017eeme ur\u010dit, zda je \u00fakol dokon\u010den, nebo ne. Nev\u00edme tak\u00e9, zda byl dokon\u010den.<br>norm\u00e1ln\u011b nebo v\u00fdjime\u010dn\u011b. \u0158e\u0161en\u00edm t\u011bchto neduh\u016f je <code>Volateln\u00fd<\/code>.<\/p>\n\n\n\n<p><code>Volateln\u00fd<\/code> je podobn\u00fd jako <code>Spustiteln\u00e9<\/code> v jist\u00e9m smyslu tak\u00e9 obaluje asynchronn\u00ed \u00falohy. Hlavn\u00edm rozd\u00edlem je, \u017ee je schopen<br>vr\u00e1tit hodnotu. N\u00e1vratov\u00e1 hodnota m\u016f\u017ee b\u00fdt libovoln\u00e9ho (neprimitivn\u00edho) typu, proto\u017ee typ <code>Volateln\u00fd<\/code> rozhran\u00ed je parametrizovan\u00fd typ.<br><code>Volateln\u00fd<\/code> je funk\u010dn\u00ed rozhran\u00ed, kter\u00e9 m\u00e1 <code>call()<\/code> metoda, kter\u00e1 m\u016f\u017ee vyhodit <code>V\u00fdjimka<\/code>.<\/p>\n\n\n\n<p>Nyn\u00ed se pod\u00edv\u00e1me, jak m\u016f\u017eeme vyu\u017e\u00edt <code>Volateln\u00fd<\/code> v na\u0161em probl\u00e9mu s poli.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">int[] a1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\nint[] a2 = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};\nint[] a3 = {3, 4, 3, 4, 3, 4, 2, 1, 3, 7};\n\nCallable task1 = () -&gt; Arrays.stream(a1).sum();\nCallable task2 = () -&gt; Arrays.stream(a2).sum();\nCallable task3 = () -&gt; Arrays.stream(a3).sum();\n\nExecutorService executor = Executors.newCachedThreadPool();\nFuture future1 = executor.submit(task1);\nFuture future2 = executor.submit(task2);\nFuture future3 = executor.submit(task3);\n\nSystem.out.println(\"1. Sou\u010det je: \" + future1.get());\nSystem.out.println(\"2. Sou\u010det je: \" + future2.get());\nSystem.out.println(\"3. Sou\u010det je: \" + future3.get());\n\nexecutor.shutdown();<\/code><\/pre>\n\n\n\n<p>Dob\u0159e, vid\u00edme, jak <code>Volateln\u00fd<\/code> je vytvo\u0159en a pot\u00e9 odesl\u00e1n do <code>ExecutorService<\/code>. Ale co to sakra je <code>Budoucnost<\/code>?<br><code>Budoucnost<\/code> funguje jako most mezi vl\u00e1kny. Sou\u010det ka\u017ed\u00e9ho pole se vytv\u00e1\u0159\u00ed v samostatn\u00e9m vl\u00e1kn\u011b a my pot\u0159ebujeme zp\u016fsob, jak ho<br>z\u00edskat tyto v\u00fdsledky zp\u011bt do <code>main()<\/code>.<\/p>\n\n\n\n<p>Z\u00edsk\u00e1n\u00ed v\u00fdsledku z <code>Budoucnost<\/code> mus\u00edme zavolat <code>get()<\/code> metoda. Zde se m\u016f\u017ee st\u00e1t jedna ze dvou v\u011bc\u00ed. Zaprv\u00e9,<br>v\u00fdsledek v\u00fdpo\u010dtu proveden\u00e9ho pomoc\u00ed <code>Volateln\u00fd<\/code> je k dispozici. Pak ji okam\u017eit\u011b dostaneme. Za druh\u00e9, v\u00fdsledek nen\u00ed<br>ji\u017e p\u0159ipravena. V tom p\u0159\u00edpad\u011b <code>get()<\/code> se zablokuje, dokud nebude k dispozici v\u00fdsledek.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ComputableFuture<\/h3>\n\n\n\n<p>Probl\u00e9m s <code>Budoucnost<\/code> je, \u017ee funguje v paradigmatu \"push\". P\u0159i pou\u017eit\u00ed <code>Budoucnost<\/code> mus\u00edte b\u00fdt jako \u0161\u00e9f, kter\u00fd<br>se neust\u00e1le pt\u00e1: \"Je tv\u016fj \u00fakol spln\u011bn? Je hotov\u00fd?\", dokud nep\u0159inese v\u00fdsledek. Jedn\u00e1n\u00ed pod neust\u00e1l\u00fdm tlakem je<br>drah\u00e9. Mnohem lep\u0161\u00ed by bylo objednat si <code>Budoucnost<\/code> co m\u00e1 ud\u011blat, a\u017e bude p\u0159ipraven ke sv\u00e9mu \u00fakolu. Bohu\u017eel,<br><code>Budoucnost<\/code> to nem\u016f\u017ee ud\u011blat, ale <code>ComputableFuture<\/code> m\u016f\u017ee.<\/p>\n\n\n\n<p><code>ComputableFuture<\/code> funguje v \"pull paradigmatu\". M\u016f\u017eeme mu \u0159\u00edci, co m\u00e1 ud\u011blat s v\u00fdsledkem, kdy\u017e spln\u00ed sv\u00e9 \u00fakoly. To<br>je p\u0159\u00edkladem asynchronn\u00edho p\u0159\u00edstupu.<\/p>\n\n\n\n<p><code>ComputableFuture<\/code> funguje perfektn\u011b s <code>Spustiteln\u00e9<\/code> ale ne s <code>Volateln\u00fd<\/code>. M\u00edsto toho je mo\u017en\u00e9 zadat \u00falohu do<br><code>ComputableFuture<\/code> ve form\u011b <code>Dodavatel<\/code>.<\/p>\n\n\n\n<p>Pod\u00edvejme se, jak v\u00fd\u0161e uveden\u00e9 souvis\u00ed s na\u0161\u00edm probl\u00e9mem.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">int[] a1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\nint[] a2 = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};\nint[] a3 = {3, 4, 3, 4, 3, 4, 2, 1, 3, 7};\n\nCompletableFuture.supplyAsync(() -&gt; Arrays.stream(a1).sum())\n                .thenAccept(System.out::println);\n\nCompletableFuture.supplyAsync(() -&gt; Arrays.stream(a2).sum())\n                .thenAccept(System.out::println);\n\nCompletableFuture.supplyAsync(() -&gt; Arrays.stream(a3).sum())\n                .thenAccept(System.out::println);<\/code><\/pre>\n\n\n\n<p>Prvn\u00ed, co v\u00e1s zaraz\u00ed, je, o kolik je toto \u0159e\u0161en\u00ed krat\u0161\u00ed. Krom\u011b toho tak\u00e9 vypad\u00e1 elegantn\u011b a \u00fahledn\u011b.<\/p>\n\n\n\n<p>\u00dakol pro <code>Dokon\u010diteln\u00e9Budoucnost<\/code> m\u016f\u017ee poskytnout <code>supplyAsync()<\/code> metoda, kter\u00e1 p\u0159eb\u00edr\u00e1 <code>Dodavatel<\/code> nebo <code>runAsync()<\/code> \u017ee<br>bere <code>Spustiteln\u00e9<\/code>. Zp\u011btn\u00e9 vol\u00e1n\u00ed - \u010d\u00e1st k\u00f3du, kter\u00e1 m\u00e1 b\u00fdt spu\u0161t\u011bna p\u0159i dokon\u010den\u00ed \u00falohy - je definov\u00e1no p\u0159\u00edkazem <code>thenAccept()<\/code><br>metoda.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Z\u00e1v\u011bry<\/h2>\n\n\n\n<p><strong>Java<\/strong> nab\u00edz\u00ed mnoho r\u016fzn\u00fdch p\u0159\u00edstup\u016f k soub\u011bhu. V tomto \u010dl\u00e1nku jsme se tohoto t\u00e9matu sotva dotkli.<\/p>\n\n\n\n<p>Nicm\u00e9n\u011b jsme se zab\u00fdvali z\u00e1klady <code>Vl\u00e1kno<\/code>, <code>Spustiteln\u00e9<\/code>, <code>Volateln\u00fd<\/code>a <code>CallableFuture<\/code> co\u017e je dobr\u00fd post\u0159eh<br>pro dal\u0161\u00ed zkoum\u00e1n\u00ed t\u00e9matu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/thecodest.co\/contact\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1283\" height=\"460\" src=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_.png\" alt=\"\" class=\"wp-image-4927\" srcset=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_.png 1283w, https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_-300x108.png 300w, https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_-1024x367.png 1024w, https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_-768x275.png 768w, https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_-18x6.png 18w, https:\/\/thecodest.co\/app\/uploads\/2024\/05\/interested_in_cooperation_-67x24.png 67w\" sizes=\"auto, (max-width: 1283px) 100vw, 1283px\" \/><\/a><\/figure>","protected":false},"excerpt":{"rendered":"<p>P\u0159e\u010dt\u011bte si prvn\u00ed \u010d\u00e1st na\u0161eho seri\u00e1lu blog\u016f v\u011bnovan\u00e9ho soub\u011bhu v jazyce Java. V n\u00e1sleduj\u00edc\u00edm \u010dl\u00e1nku se bl\u00ed\u017ee pod\u00edv\u00e1me na rozd\u00edly mezi vl\u00e1kny a procesy, pooly vl\u00e1ken, exekutory a mnoho dal\u0161\u00edho!<\/p>","protected":false},"author":2,"featured_media":3139,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Concurrency in Java Part 1 - Introduction - The Codest<\/title>\n<meta name=\"description\" content=\"Read the first part of our blog series devoted to concurrency in Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/cs\/blog\/soubeznost-v-jave-cast-1-uvod\/\" \/>\n<meta property=\"og:locale\" content=\"cs_CZ\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Concurrency in Java Part 1 - Introduction\" \/>\n<meta property=\"og:description\" content=\"Read the first part of our blog series devoted to concurrency in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/cs\/blog\/soubeznost-v-jave-cast-1-uvod\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-15T05:27:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-11T05:59:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/concurrency_in_java_part_1_-_introduction.png\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"thecodest\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"thecodest\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/\"},\"author\":{\"name\":\"thecodest\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\"},\"headline\":\"Concurrency in Java Part 1 &#8211; Introduction\",\"datePublished\":\"2022-06-15T05:27:58+00:00\",\"dateModified\":\"2026-03-11T05:59:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/\"},\"wordCount\":1295,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/concurrency_in_java_part_1_-_introduction.png\",\"articleSection\":[\"Software Development\"],\"inLanguage\":\"cs-CZ\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/\",\"name\":\"Concurrency in Java Part 1 - Introduction - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/concurrency_in_java_part_1_-_introduction.png\",\"datePublished\":\"2022-06-15T05:27:58+00:00\",\"dateModified\":\"2026-03-11T05:59:41+00:00\",\"description\":\"Read the first part of our blog series devoted to concurrency in Java.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#breadcrumb\"},\"inLanguage\":\"cs-CZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"cs-CZ\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#primaryimage\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/concurrency_in_java_part_1_-_introduction.png\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/concurrency_in_java_part_1_-_introduction.png\",\"width\":960,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/concurrency-in-java-part-1-introduction\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Concurrency in Java Part 1 &#8211; Introduction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"cs-CZ\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#organization\",\"name\":\"The Codest\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs-CZ\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/03\\\/thecodest-logo.svg\",\"width\":144,\"height\":36,\"caption\":\"The Codest\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/pl.linkedin.com\\\/company\\\/codest\",\"https:\\\/\\\/clutch.co\\\/profile\\\/codest\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\",\"name\":\"thecodest\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"cs-CZ\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"caption\":\"thecodest\"},\"url\":\"https:\\\/\\\/thecodest.co\\\/cs\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Soub\u011b\u017enost v Jav\u011b - 1. \u010d\u00e1st - \u00davod - The Codest","description":"P\u0159e\u010dt\u011bte si prvn\u00ed \u010d\u00e1st na\u0161eho seri\u00e1lu blog\u016f v\u011bnovan\u00e9ho soub\u011bhu v jazyce Java.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thecodest.co\/cs\/blog\/soubeznost-v-jave-cast-1-uvod\/","og_locale":"cs_CZ","og_type":"article","og_title":"Concurrency in Java Part 1 - Introduction","og_description":"Read the first part of our blog series devoted to concurrency in Java.","og_url":"https:\/\/thecodest.co\/cs\/blog\/soubeznost-v-jave-cast-1-uvod\/","og_site_name":"The Codest","article_published_time":"2022-06-15T05:27:58+00:00","article_modified_time":"2026-03-11T05:59:41+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/concurrency_in_java_part_1_-_introduction.png","type":"image\/png"}],"author":"thecodest","twitter_card":"summary_large_image","twitter_misc":{"Written by":"thecodest","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#article","isPartOf":{"@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/"},"author":{"name":"thecodest","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76"},"headline":"Concurrency in Java Part 1 &#8211; Introduction","datePublished":"2022-06-15T05:27:58+00:00","dateModified":"2026-03-11T05:59:41+00:00","mainEntityOfPage":{"@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/"},"wordCount":1295,"commentCount":0,"publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"image":{"@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/concurrency_in_java_part_1_-_introduction.png","articleSection":["Software Development"],"inLanguage":"cs-CZ","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/","url":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/","name":"Soub\u011b\u017enost v Jav\u011b - 1. \u010d\u00e1st - \u00davod - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#primaryimage"},"image":{"@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/concurrency_in_java_part_1_-_introduction.png","datePublished":"2022-06-15T05:27:58+00:00","dateModified":"2026-03-11T05:59:41+00:00","description":"P\u0159e\u010dt\u011bte si prvn\u00ed \u010d\u00e1st na\u0161eho seri\u00e1lu blog\u016f v\u011bnovan\u00e9ho soub\u011bhu v jazyce Java.","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#breadcrumb"},"inLanguage":"cs-CZ","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/"]}]},{"@type":"ImageObject","inLanguage":"cs-CZ","@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#primaryimage","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/concurrency_in_java_part_1_-_introduction.png","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/concurrency_in_java_part_1_-_introduction.png","width":960,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/blog\/concurrency-in-java-part-1-introduction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"Concurrency in Java Part 1 &#8211; Introduction"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"The Codest","description":"","publisher":{"@id":"https:\/\/thecodest.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"cs-CZ"},{"@type":"Organization","@id":"https:\/\/thecodest.co\/#organization","name":"The Codest","url":"https:\/\/thecodest.co\/","logo":{"@type":"ImageObject","inLanguage":"cs-CZ","@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/03\/thecodest-logo.svg","width":144,"height":36,"caption":"The Codest"},"image":{"@id":"https:\/\/thecodest.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/pl.linkedin.com\/company\/codest","https:\/\/clutch.co\/profile\/codest"]},{"@type":"Person","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76","name":"thecodest","image":{"@type":"ImageObject","inLanguage":"cs-CZ","@id":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","caption":"thecodest"},"url":"https:\/\/thecodest.co\/cs\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/comments?post=3138"}],"version-history":[{"count":8,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3138\/revisions"}],"predecessor-version":[{"id":8551,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/posts\/3138\/revisions\/8551"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media\/3139"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/media?parent=3138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/categories?post=3138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/cs\/wp-json\/wp\/v2\/tags?post=3138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}