public class Thread2 implements Runnable{
@Override
public void run() {
this.m.divisione();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("FINITO THREAD");
}
public Thread2 (MetodoSincronizzato c) {
this.m = c;
}
public MetodoSincronizzato m;
}
------------------------------------
public class MetodoSincronizzato {
public synchronized void divisione() {
System.out.println (this.a + " diviso " + this.b + " è uguale a " + this.a/this.b + " con resto " + this.a%this.b + ".");
}
public MetodoSincronizzato(int a, int b){
this.a = a;
this.b = b;
}
private int a, b;
}
------------------------------------
public class MaineRoad {
public static void main (String[] args){
MetodoSincronizzato c = new MetodoSincronizzato(11, 4);
Runnable a = new Thread2 (c);
Runnable b = new Thread2 (c);
Thread t1 = new Thread (a, "CICCIO");
Thread t2 = new Thread (b, "PASTICCIO");
t1.start();
t2.start();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}