Programmazione Concorrente e Distribuita Esame scritto del 27 Agosto 2012 Versione AA Esercizio 1 (5 punti) 1 class A{ 2 S t r i n g s ; 3 4 A( S t r i n g s ){ 5 this. s = s ; 6 System. out. p r i n t l n ( this. s ) ; 7 } 8 } 9 10 class C{ 11 s t a t i c A s1 ; 12 s t a t i c A s2 = new A( pluto ) ; 13 } 14 15 public class E{ 16 public s t a t i c void main ( S t r i n g [ ] a r g s ){ 17 System. out. p r i n t l n ( I n i z i o E. main ( ) ) ; 18 C c = null ; 19 System. out. p r i n t l n ( After C c ; ) ; 20 System. out. p r i n t l n (C. s1 + + C. s2 ) ; 21 System. out. p r i n t l n (C. s1. s ) ; 22 System. out. p r i n t l n (C. s2. s ) ; 23 System. out. p r i n t l n ( E x i t t i n g main ( ) ) ; 24 } 25 } Dare l output causato dall esecuzione del metodo main() della classe E. Pagina 1 di 6 (versione AA)
Esercizio 2 (5 punti) 1 class C{ 2 private int counter = 0 ; 3 4 public synchronized void m( ) { 5 for ( int i = 0 ; i < 1000000; i ++) 6 counter++; 7 for ( int j = 0 ; j < 1000000; j++) 8 counter ; 9 counter++; 10 n o t i f y A l l ( ) ; 12 public synchronized void n ( ) throws I n t e r r u p t e d E x c e p t i o n { 13 i f ( counter < 9) 14 wait ( ) ; 15 System. out. p r i n t ( counter + ) ; 16 } 17 } 18 19 class T1 extends Thread{ 20 private C c ; 21 22 public T1(C c ){ 23 this. c = c ; 24 } 25 26 public void run ( ) { 27 for ( int i = 0 ; i < 1000; i ++) 28 c.m( ) ; 29 } 30 } 31 32 class T2 extends Thread{ 33 private C c ; 34 35 public T2(C c ){ 36 this. c = c ; 37 } 38 39 public void run ( ) { 40 try{ 41 c. n ( ) ; 42 } catch ( I n t e r r u p t e d E x c e p t i o n i e ){} 43 } 44 } 45 46 class Test { 47 public s t a t i c void main ( S t r i n g [ ] argv ){ 48 C c = new C ( ) ; 49 T1 r = new T1( c ) ; 50 T2 s = new T2( c ) ; 51 T2 t = new T2( c ) ; 52 53 t. s t a r t ( ) ; 54 s. s t a r t ( ) ; 55 r. s t a r t ( ) ; 56 } 57 } Pagina 2 di 6 (versione AA)
Ciascuna delle classi definite nel codice precedente compila senza errori. L esecuzione del metodo main() delle classe Test stampa una coppia di interi n, m, con n 0, e m 0. Quali delle seguenti affermazioni sono vere? A Sicuramente n m, n 9 e m 9 B Sicuramente n = m 9 C Può accadere che n o m siano 0 D Sicuramente n 1 e m 1 Esercizio 3 (5 punti) 1 class Node{ 2 private S t r i n g i n f o ; 3 private Node next ; 4 5 public Node ( S t r i n g i n f o ){ 6 this. i n f o = i n f o ; 7 } 8 9 public void setnext ( Node next ){ 10 this. next = next ; 12 13 public s t a t i c void p r i n t I n f o ( Node n ){ 14 synchronized ( n ){ 15 i f ( n!= null ) 16 System. out. p r i n t l n ( n. i n f o + ) ; 17 } 18 } 19 20 public synchronized void printnext ( ) { 21 System. out. p r i n t ( i n f o + ) ; 22 for ( int i = 0 ; i < 9999999; i ++); 23 p r i n t I n f o ( next ) ; 24 System. out. p r i n t ( AFTERPRINTING ) ; 25 } 26 } 27 28 class T extends Thread{ 29 private Node n ; 30 31 public T( Node n ){ 32 this. n = n ; 33 } 34 35 public void run ( ) { 36 n. printnext ( ) ; 37 } 38 } 39 40 class Ex{ 41 public s t a t i c void main ( S t r i n g [ ] argv ){ 42 Node one = new Node ( ONE ) ; 43 Node two = new Node ( TWO ) ; 44 one. setnext ( two ) ; Pagina 3 di 6 (versione AA)
45 two. setnext ( one ) ; 46 T [ ] threadarray = {new T( one ), new T( two ) } ; 47 int k = ( int ) (Math. random ( ) 2 ) ; 48 threadarray [ k ]. s t a r t ( ) ; 49 threadarray [ ( k+1)%2]. s t a r t ( ) ; 50 } 51 } Le classi nel codice precedente compilano senza errori. Si descriva ogni output possibile causato dall esecuzione del metodo main() della classe Ex, considerando le seguenti regole: Se una esecuzione del main() provoca la scrittura di SOMETHING nello standard output e termina normalmente (senza errore), si scriva solo SOMETHING; Se una esecuzione del main() provoca la scrittura di SOMETHING nello standard output e quindi provoca una eccezione, scrivere solo SOMETHING seguito da EXCEP- TION; Se una esecuzione del main() provoca la scrittura di SOMETHING nello standard output e quindi provoca un deadlock, scrivere solo SOMETHING seguito da DEADLOCK. Inoltre, per ciasun output indicato, si motivi (brevemente e chiaramente) lo scenario in cui l output è possibile. Pagina 4 di 6 (versione AA)
Esercizio 4 (5 punti) 1 class Dudes{ 2 s t a t i c long f l a g = 0 ; 3 // i n s e r t code here 4 i f ( f l a g == 0) 5 f l a g = id ; 6 for ( int X = 1 ; X < 3 ; X++){ 7 i f ( f l a g == i d ) 8 System. out. p r i n t ( yo ) ; 9 else 10 System. out. p r i n t ( dude ) ; 12 } 13 } 14 15 public c l a s s Chat implements Runnable{ 16 s t a t i c Dudes d ; 17 18 void go ( ) { 19 d = new Dudes ( ) ; 20 new Thread (new Chat ( ) ). s t a r t ( ) ; 21 new Thread (new Chat ( ) ). s t a r t ( ) ; 22 } 23 24 public void run ( ) { 25 d. chat ( Thread. currentthread ( ). g e t I d ( ) ) ; 26 } 27 28 public s t a t i c void main ( S t r i n g [ ] argv ){ 29 new Chat ( ). go ( ) ; 30 } 31 } Si considerino, inoltre, i seguenti due frammenti di codice: I. synchronized void chat(long id){ II. void chat (long id){ Quando si inserisce il frammento I o II nella linea 3, quali delle seguenti affermazioni è vera? A Sia con il frammento I che con il frammento II, viene lanciata una eccezione a runtime B Con il frammento I, il codice non compila C Con il frammento II, il codice non compila D Con il frammento I, l output potrebbe essere: yo dude dude yo E Con il frammento I, l output potrebbe essere: dude dude yo yo F Con il frammento II, l output potrebbe essere: yo dude dude yo Esercizio 5 (5 punti) 1 public class Tenor extends S i n g e r { 2 public s t a t i c S t r i n g s i n g ( ) { 3 return f a ; Pagina 5 di 6 (versione AA)
4 } 5 6 public s t a t i c void main ( S t r i n g [ ] a r g s ) { 7 Tenor t = new Tenor ( ) ; 8 S i n g e r s = new Tenor ( ) ; 9 System. out. p r i n t l n ( t. s i n g ( ) + + s. s i n g ( ) ) ; 10 } 12 13 class S i n g e r { 14 public s t a t i c S t r i n g s i n g ( ) { 15 return l a ; 16 } 17 } Quale è il risultato? A fa la fa B la fa la C fa fa D fa la E la la F La compilazione fallisce G Viene sollevata un eccezione a runtime Esercizio 6 (5 punti) 1 public class Logger { 2 private S t r i n g B u f f e r c o n t e n t s = new S t r i n g B u f f e r ( ) ; 3 4 public void l o g ( S t r i n g message ) { 5 c o n t e n t s. append ( System. c u r r e n t T i m e M i l l i s ( ) ) ; 6 c o n t e n t s. append ( : ) ; 7 c o n t e n t s. append ( Thread. currentthread ( ). getname ( ) ) ; 8 c o n t e n t s. append ( message ) ; 9 c o n t e n t s. append ( \n ) ; 10 } 11 12 public S t r i n g getcontents ( ) { 13 return c o n t e n t s. t o S t r i n g ( ) ; 14 } 15 } Come possiamo assicurare che le istanze di questa classe non creino problemi di accesso concorrente ai dati in un contesto multithread? A Rendendo synchronyzed il metodo log() B Rendendo synchronyzed il metodo getcontents() C Rendendo synchronyzed sia log() che getcontents() D Non è possibile E Non serve nulla Pagina 6 di 6 (versione AA)