|
| 1 | +--- |
| 2 | +title: Active Object |
| 3 | +category: Concurrency |
| 4 | +language: it |
| 5 | +tag: |
| 6 | + - Performance |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +## Intento |
| 11 | +Il design pattern active object disaccoppia l'esecuzione del metodo dall'invocazione del metodo per gli oggetti che risiedono ciascuno nel proprio thread di controllo. L'obiettivo è introdurre la concorrenza utilizzando l'invocazione asincrona dei metodi e uno scheduler per gestire le richieste. |
| 12 | + |
| 13 | +## Spiegazione |
| 14 | + |
| 15 | +La classe che implementa il pattern active object conterrà un meccanismo di autosincronizzazione senza utilizzare metodi 'synchronized'. |
| 16 | + |
| 17 | +Esempio del mondo reale |
| 18 | + |
| 19 | +>Gli Orchi sono noti per la loro natura selvaggia e la loro anima indomabile. Sembra che abbiano il loro proprio thread di controllo basato su comportamenti precedenti. |
| 20 | +
|
| 21 | +Per implementare una creatura che ha il suo meccanismo di thread di controllo e che esponga solo la sua API e non l'esecuzione stessa, possiamo utilizzare il pattern Active Object. |
| 22 | + |
| 23 | + |
| 24 | +**Esempio di codice** |
| 25 | + |
| 26 | +```java |
| 27 | +public abstract class ActiveCreature{ |
| 28 | + private final Logger logger = LoggerFactory.getLogger(ActiveCreature.class.getName()); |
| 29 | + |
| 30 | + private BlockingQueue<Runnable> requests; |
| 31 | + |
| 32 | + private String name; |
| 33 | + |
| 34 | + private Thread thread; |
| 35 | + |
| 36 | + public ActiveCreature(String name) { |
| 37 | + this.name = name; |
| 38 | + this.requests = new LinkedBlockingQueue<Runnable>(); |
| 39 | + thread = new Thread(new Runnable() { |
| 40 | + @Override |
| 41 | + public void run() { |
| 42 | + while (true) { |
| 43 | + try { |
| 44 | + requests.take().run(); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + logger.error(e.getMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + ); |
| 52 | + thread.start(); |
| 53 | + } |
| 54 | + |
| 55 | + public void eat() throws InterruptedException { |
| 56 | + requests.put(new Runnable() { |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + logger.info("{} is eating!",name()); |
| 60 | + logger.info("{} has finished eating!",name()); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public void roam() throws InterruptedException { |
| 67 | + requests.put(new Runnable() { |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + logger.info("{} has started to roam the wastelands.",name()); |
| 71 | + } |
| 72 | + } |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public String name() { |
| 77 | + return this.name; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +Possiamo vedere che qualsiasi classe che estenderà la classe ActiveCreature avrà il proprio thread di controllo per invocare ed eseguire i metodi. |
| 83 | + |
| 84 | +Ad esempio, la classe Orc: |
| 85 | + |
| 86 | +```java |
| 87 | +public class Orc extends ActiveCreature { |
| 88 | + |
| 89 | + public Orc(String name) { |
| 90 | + super(name); |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +Ora possiamo creare diverse creature come gli Orchi, dir loro di mangiare e vagabondare, e loro eseguiranno queste azioni nel proprio thread di controllo: |
| 97 | + |
| 98 | +```java |
| 99 | + public static void main(String[] args) { |
| 100 | + var app = new App(); |
| 101 | + app.run(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void run() { |
| 106 | + ActiveCreature creature; |
| 107 | + try { |
| 108 | + for (int i = 0;i < creatures;i++) { |
| 109 | + creature = new Orc(Orc.class.getSimpleName().toString() + i); |
| 110 | + creature.eat(); |
| 111 | + creature.roam(); |
| 112 | + } |
| 113 | + Thread.sleep(1000); |
| 114 | + } catch (InterruptedException e) { |
| 115 | + logger.error(e.getMessage()); |
| 116 | + } |
| 117 | + Runtime.getRuntime().exit(1); |
| 118 | + } |
| 119 | +``` |
| 120 | + |
| 121 | +## Diagramma delle classi |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +## Tutorial |
| 126 | + |
| 127 | +* [Android and Java Concurrency: The Active Object Pattern](https://www.youtube.com/watch?v=Cd8t2u5Qmvc) |
0 commit comments