lang:java:thread
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
lang:java:thread [2019/09/29 19:01] – Création root | lang:java:thread [2023/06/12 15:07] (Version actuelle) – Ajout de "Nommage des threads" root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | =====Threads===== | + | =====Différentes classes===== |
====Runnable==== | ====Runnable==== | ||
Ligne 28: | Ligne 28: | ||
</ | </ | ||
- | ====Méthodes courantes==== | + | ====FutureTask==== |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
+ | Une '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | Quand une exception est générée, elle n'est pas remontée au thread principale. Elle est transformée en une '' | ||
+ | |||
+ | <code java> | ||
+ | import java.util.concurrent.FutureTask; | ||
+ | import java.io.IOException; | ||
+ | import java.util.concurrent.ExecutorService; | ||
+ | import java.util.concurrent.Executors; | ||
+ | import java.util.concurrent.ExecutionException; | ||
+ | |||
+ | public class MyClass { | ||
+ | public static void main(String args[]) { | ||
+ | FutureTask< | ||
+ | throw new IOException(" | ||
+ | }); | ||
+ | ExecutorService executor = Executors.newFixedThreadPool(2); | ||
+ | executor.execute(futureTask); | ||
+ | try { | ||
+ | futureTask.get(); | ||
+ | } catch (InterruptedException e) { | ||
+ | } catch (ExecutionException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | executor.shutdown(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Sortie standard : | ||
+ | |||
+ | < | ||
+ | OK | ||
+ | OK2 | ||
+ | |||
+ | java.util.concurrent.ExecutionException: | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at MyClass.main(MyClass.java: | ||
+ | Caused by: java.io.IOException: | ||
+ | at MyClass.lambda$main$0(MyClass.java: | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | </ | ||
+ | |||
+ | Si l' | ||
+ | |||
+ | <code java> | ||
+ | import java.io.IOException; | ||
+ | import java.util.concurrent.Callable; | ||
+ | import java.util.concurrent.ExecutionException; | ||
+ | import java.util.concurrent.ExecutorService; | ||
+ | import java.util.concurrent.Executors; | ||
+ | import java.util.concurrent.FutureTask; | ||
+ | |||
+ | public class MyClass { | ||
+ | public static class SecuredFutureTask< | ||
+ | public SecuredFutureTask(Callable< | ||
+ | super(callable); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void done() { | ||
+ | try { | ||
+ | get(); | ||
+ | } catch (ExecutionException | InterruptedException e) { | ||
+ | throw new AssertionError(e); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | public static void main(String args[]) { | ||
+ | FutureTask< | ||
+ | new SecuredFutureTask< | ||
+ | ExecutorService executor = Executors.newFixedThreadPool(2); | ||
+ | executor.execute(futureTask); | ||
+ | System.out.println(" | ||
+ | executor.shutdown(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Sortie standard : | ||
+ | |||
+ | < | ||
+ | OK | ||
+ | |||
+ | Exception in thread " | ||
+ | at MyClass$SecuredFutureTask.done(MyClass.java: | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | Caused by: java.util.concurrent.ExecutionException: | ||
+ | at java.base/ | ||
+ | at java.base/ | ||
+ | at MyClass$SecuredFutureTask.done(MyClass.java: | ||
+ | ... 6 more | ||
+ | Caused by: java.io.IOException: | ||
+ | at MyClass.lambda$main$0(MyClass.java: | ||
+ | at java.base/ | ||
+ | ... 3 more | ||
+ | </ | ||
=====Accès concurrent===== | =====Accès concurrent===== | ||
<code java> | <code java> | ||
Ligne 56: | Ligne 159: | ||
Le concept consiste à poser un lock sur une instance ou sur une classe. | Le concept consiste à poser un lock sur une instance ou sur une classe. | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ======Barrières===== | ||
+ | |||
+ | ====Semaphore==== | ||
+ | |||
+ | Barrière en attente d'un signal. | ||
+ | |||
+ | <code java> | ||
+ | import java.util.concurrent.Semaphore; | ||
+ | |||
+ | public class MyClass { | ||
+ | public static void main(String args[]) { | ||
+ | Semaphore semaphoreAcquisitionReady = new Semaphore(0); | ||
+ | |||
+ | new Thread(() -> { | ||
+ | try { | ||
+ | Thread.sleep(50); | ||
+ | } catch (InterruptedException e) {} | ||
+ | System.out.println(" | ||
+ | semaphoreAcquisitionReady.release(); | ||
+ | System.out.println(" | ||
+ | }).start(); | ||
+ | |||
+ | System.out.println(" | ||
+ | try { | ||
+ | semaphoreAcquisitionReady.acquire(); | ||
+ | } catch (InterruptedException e) {} | ||
+ | System.out.println(" | ||
+ | |||
+ | semaphoreAcquisitionReady.release(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Sortie standard. | ||
+ | |||
+ | < | ||
+ | BEFORE acquire | ||
+ | BEFORE release | ||
+ | AFTER release | ||
+ | AFTER acquire | ||
+ | </ | ||
+ | |||
+ | =====Debug===== | ||
+ | ====Nommage des threads==== | ||
+ | |||
+ | Par défaut, le nom d'un thread est '' | ||
+ | |||
+ | Dans le cas des threads, cela se fait directement depuis le constructeur. | ||
+ | |||
+ | <code java> | ||
+ | new Thread(() -> {}, "Nom du thread" | ||
+ | </ | ||
+ | |||
+ | Dans le cas de '' | ||
+ | |||
+ | <code java> | ||
+ | new FutureTask<> | ||
+ | Thread.currentThread().setName(" | ||
+ | }); | ||
+ | </ |
lang/java/thread.1569776485.txt.gz · Dernière modification : 2019/09/29 19:01 de root