mercredi 24 septembre 2014

Locking Objects with Threads in Java

Here is a quick example of Locking Objects with Threads in Java.

 public Friend(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }


creating a main for the locking of two objects 

 public static void main(String[] args) {
        final Friend alphonse =
            new Friend("Alphonse");
        final Friend gaston =
            new Friend("Gaston");
        new Thread(new BowLoop(alphonse, gaston)).start();
        new Thread(new BowLoop(gaston, alphonse)).start();
    }

calling the BowLoop who locks the two objects in the order they have been entered.