Spring 2001, CSE 428: Quiz 6.1 - 1 March 2001


Please write your Name and Student ID,at the top of the page.
By default, this quiz will be returned in Section 2 (morning section). If you want to get it back in the afternoon section, please write "Section 1" at the top of the page.
Consider the following declaration in Java, where myRandom() is a function which returns an arbitrary number. Ignore the problem of InterruptedException in the instruction sleep.
   class Foo{
      private int content;
      private String name;

      public Foo(int k, String s){ content = k; name = s; }
      public void updateContent(int k) { content = content + k; }
      public int getContent() { return content; }
      public String getName() { return name; }
   }

   class MyThread extends Thread{
      private Foo f;
           
      public MyThread(Foo f){ this.f = f; }
      public void run(){ 
         sleep(myRandom());                   //Sleep an arbitrary amount of time. 
         f.updateContent(2);                  //Update the content of f
         sleep(myRandom());                   //Sleep an arbitrary amount of time. 
         System.out.print(f.getName());       //Print the name of f
         sleep(myRandom());                   //Sleep an arbitrary amount of time. 
         System.out.print(f.getContent());    //Print the content of f
       }
   }

      ...
      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();
      }
  1. [Pts 4]  For each of the following methods, specify whether it needs to be synchronized or not

  2. [Pts 2]  How many times will the string "Giorgio" be printed?  
     
    2  

  3. [Pts 4]  Suppose you have added the synchronization keyword on all the methods of Foo except the constructor. For each of the following integer values, say whether it can be printed or not during the execution of the program