Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TogliStallo {
- public static Object Lock1 = new Object();
- public static Object Lock2 = new Object();
- private static int finito1,finito2;
- public static void main(String args[]) {
- finito1=0;
- Thread1 T1 = new Thread1();
- Thread2 T2 = new Thread2();
- T1.start();
- T2.start();
- while (finito1!=2 && finito2!=2) try { Thread.sleep(10); }
- catch (InterruptedException e) {};
- System.out.println("Fatto!");
- }
- private static class Thread1 extends Thread {
- @Override
- public void run() {
- synchronized (Lock1) {
- System.out.println("Thread 1: In possesso del lock 1...");
- try { Thread.sleep(10); }
- catch (InterruptedException e) {}
- System.out.println("Thread 1: In attesa del lock 2...");
- synchronized (Lock2) {
- System.out.println("Thread 1: In possesso del lock 1 e 2...");
- finito1++;finito2++;
- }
- }
- }
- }
- private static class Thread2 extends Thread {
- @Override
- public void run() {
- synchronized (Lock2) {
- System.out.println("Thread 2: In possesso del lock 2...");
- try { Thread.sleep(10); }
- catch (InterruptedException e) {}
- System.out.println("Thread 2: In attesa del lock 1...");
- synchronized (Lock1) {
- System.out.println("Thread 2: In possesso del lock 1 e 2...");
- finito1++;finito2++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement