Exercise 6   Name ____________________ Points  __/15

Document last modified: 

1. (4 pts) What is one possible sequence of instructions executed for the complete program?

3, 4, 5, 6, 16-18, 6, 20-26,
        5, 6, 16-18, 6, 20-26,
        5, 6, 16-18, 6, 20-26,
8, 9, 10
 

  1. public class sequencedThread
  2.  {
  3.     public static void main(String args[]) throws Exception
  4.     {
  5.          for (int i=1; i<=3; i++)
  6.                  new NumberedThread(i).start();
  7.  
  8.          System.out.println("main completed");
  9.     }
  10.  }
  11.  
  12.  class NumberedThread extends Thread
  13.  {
  14.     int n;
  15.  
  16.     public NumberedThread(int n)
  17.     {    this.n = n;
  18.     }
  19.  
  20.     public void run()
  21.     { 
  22.          System.out.println("  [Start Thread " + n + "]");
  23.          for(char ch='a'; ch<'d'; ch++)
  24.                  System.out.println("{Working Thread " + n + " <" + ch+ ">}");
  25.          System.out.println("  [Completed Thread " + n + "]");
  26.     }
  27.  }

2. (4 pts) What is reasonable expected output?

  [Start Thread 1]
{Working Thread 1 <a>}
{Working Thread 1 <b>}
  [Start Thread 3]
main completed
{Working Thread 1 <c>}
  [Start Thread 2]
{Working Thread 3 <a>}
  [Completed Thread 1]
{Working Thread 2 <a>}
{Working Thread 3 <b>}
{Working Thread 2 <b>}
{Working Thread 3 <c>}
{Working Thread 2 <c>}
  [Completed Thread 3]
  [Completed Thread 2]
3. (1 pts) Are other outputs possible?  Yes 

4. (4 pts) What is the sequence of instructions executed for the complete program?

3, 4, 5, 6, 17-20, 6,
        5, 6, 17-20, 6,
        5, 6, 17-20, 6, 7-9
        22-26,
        22-25,
        22-25,
                27, 26, 27, 26, 27, 28-30
          26, 27, 26, 27, 26, 27, 28-30
          26, 27, 26, 27, 26, 27, 28-30
10
 

  1. public class ex7
  2.  {
  3.     public static void main(String args[]) throws Exception
  4.     {  String string = new String("Go IUS");
  5.          for (int i=1; i<=3; i++)
  6.                  new NumberedThread(i, string).start();
  7.  
  8.          System.out.println("main completed");
  9.     }
  10.  }
  11.  
  12.  class NumberedThread extends Thread
  13.  {
  14.      int n;
  15.      String string;
  16.  
  17.     public NumberedThread(int n, String string)
  18.     {    this.n = n;
  19.           this.string = string;
  20.     }
  21.  
  22.     public void run()
  23.     { 
  24.          System.out.println("  [Start Thread " + n + "]");
  25.          synchronized(string) {
  26.                 for(char i='a'; i<'d'; i++)
  27.                     System.out.println("{Working Thread " + n + " <" + i + ">}");
  28.          }
  29.          System.out.println("  [Completed Thread " + n + "]");
  30.     }
  31.  }

5. (2 pts) Rearrange the following output into proper order for the above program. There are two mistakes.

main completed
  [Start Thread 1]
{Working Thread 1 <b>}
{Working Thread 1 <a>}
{Working Thread 1 <c>}
  [Start Thread 2]
  [Start Thread 3]
  [Completed Thread 1]
{Working Thread 2 <a>}
{Working Thread 2 <b>}
{Working Thread 3 <a>}
{Working Thread 2 <c>}
  [Completed Thread 2]
{Working Thread 3 <b>}
{Working Thread 3 <c>}
  [Completed Thread 3]
main completed
  [Start Thread 1]
{Working Thread 1 <a>}
{Working Thread 1 <b>}
{Working Thread 1 <c>}
  [Start Thread 2]
  [Start Thread 3]
  [Completed Thread 1]
{Working Thread 2 <a>}
{Working Thread 2 <b>}
{Working Thread 2 <c>}
  [Completed Thread 2]
{Working Thread 3 <a>}
{Working Thread 3 <b>}
{Working Thread 3 <c>}
  [Completed Thread 3]