// Code of Quiz 6.1 // Filename: Quiz6_1 class Foo{ private int content; private String name; public Foo(int k, String s){ content = k; name = s; } public synchronized void updateContent(int k) { content = content + k; } public synchronized int getContent() { return content; } public synchronized String getName() { return name; } } class MyThread extends Thread{ private Foo f; public MyThread(Foo f){ this.f = f; } public void run(){ try {sleep((long)(java.lang.Math.random()*6));} catch(InterruptedException e) { return; }; f.updateContent(2); //Update the content of f 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; }; System.out.print(f.getContent() + " "); //Print the content of f } } public class Quiz6_1{ public static void main(String[] args) { Foo f = new Foo(1,"Giorgio"); MyThread T1 = new MyThread(f); MyThread T2 = new MyThread(f); T1.start(); T2.start(); } }