Ciao Ragazzi sto provando a vedere la sincronizazione in Java con i thread; ho creato il seguente codice pero non riesco a capire perchè una volta che il thread viene messo in wait non riparte anche se ho utilizzato notifyall()
Chiedo aiuto a tutti voi, non so piu cosa fare!!!
La prima serie di numeri (da 0 a 9 ) me le stampa invece la seconda serie NO!!
__________________________________________________ ___codice:Classe main() int[] a = new int[1]; // variabilie condivisa a[0]=1; thre xx=new thre(a); xx.start(); thre zz=new thre(a); zz.start();
Classe che eredita da thread
codice:public class thre extends Thread{ int[]a=null; public thre(int[] variabilie) { super(); a = variabilie; } @Override public void run() { // TODO Auto-generated method stub super.run(); try { this.conta(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public synchronized void conta() throws InterruptedException{ int i; while (a[0] == 0) { try { wait(); } catch (InterruptedException e) {} } a[0]=0; for(i=0;i<10;i++){ System.out.print(i + "-") ; Thread.sleep(1000); } a[0]=1; notifyAll(); } }

Rispondi quotando
