Fenomeno85
08-05-2006, 23:58
class Colori {
private int red;
private int blue;
private int white;
//getter
public int red(){
return this.red;
}
public int blue (){
return this.blue;
}
public int white(){
return this.white;
}
public synchronized void setColori (int r, int b, int w, String nomeThread){
this.red = r;
this.blue = b;
this.white = w;
try {
Thread.sleep(344);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(">>" + nomeThread + " " + this.blue + " "+ this.white + " " + this.red);
}
}
//thread
class CambiaColori extends Thread{
private Colori color;
private String nomeThread;
private int b, w, r;
//costruttore
public CambiaColori(Colori color, String nomeThread, int b, int w, int r){
this.color = color;
this.nomeThread = nomeThread;
this.b = b;
this.w = w;
this.r = r;
}
public void run (){
synchronized (color){
color.setColori(this.b,this.w,this.r,this.nomeThread);
}
}
}
public class Test {
public static void main(String[] args) {
Colori color = new Colori();
CambiaColori d = new CambiaColori (color,"PRIMO", 3,3,3);
CambiaColori d1 = new CambiaColori (color,"SECONDO", 93,3,3);
d.start();
d1.start();
}
}
ma il synchro si mette in attesa se richiamo un metodo sincronizizzato?no perchè ho fatto questo codice per prova e i due thread lavorano correttamente
~§~ Sempre E Solo Lei ~§~
private int red;
private int blue;
private int white;
//getter
public int red(){
return this.red;
}
public int blue (){
return this.blue;
}
public int white(){
return this.white;
}
public synchronized void setColori (int r, int b, int w, String nomeThread){
this.red = r;
this.blue = b;
this.white = w;
try {
Thread.sleep(344);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(">>" + nomeThread + " " + this.blue + " "+ this.white + " " + this.red);
}
}
//thread
class CambiaColori extends Thread{
private Colori color;
private String nomeThread;
private int b, w, r;
//costruttore
public CambiaColori(Colori color, String nomeThread, int b, int w, int r){
this.color = color;
this.nomeThread = nomeThread;
this.b = b;
this.w = w;
this.r = r;
}
public void run (){
synchronized (color){
color.setColori(this.b,this.w,this.r,this.nomeThread);
}
}
}
public class Test {
public static void main(String[] args) {
Colori color = new Colori();
CambiaColori d = new CambiaColori (color,"PRIMO", 3,3,3);
CambiaColori d1 = new CambiaColori (color,"SECONDO", 93,3,3);
d.start();
d1.start();
}
}
ma il synchro si mette in attesa se richiamo un metodo sincronizizzato?no perchè ho fatto questo codice per prova e i due thread lavorano correttamente
~§~ Sempre E Solo Lei ~§~