// Code of Quiz 6.4 // Filename: Quiz6_4 class Foo{ private int content; private String name; public Foo(int k){ content = k; name = "Anonymous"; } public synchronized void updateName(String s) { name = s; } public synchronized int getContent() { return content; } public synchronized String getName() { return name; } } class MyThread extends Thread{ private Foo f; private String name; public MyThread(Foo f, String s){ this.f = f; this.name = s; } public void run(){ try {sleep((long)(java.lang.Math.random()*6));} catch(InterruptedException e) { return; }; System.out.print(f.getName() + " "); //Print the name of f try {sleep((long)(java.lang.Math.random()*6));} catch(InterruptedException e) { return; }; f.updateName(name); //Update the name of f try {sleep((long)(java.lang.Math.random()*6));} catch(InterruptedException e) { return; }; System.out.print(f.getContent() + " "); //Print the content of f } } public class Quiz6_4{ public static void main(String[] args) { Foo f = new Foo(1); MyThread T1 = new MyThread(f,"Giorgio"); MyThread T2 = new MyThread(f,"Roberto"); T1.start(); T2.start(); } }