Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.80 KB | None | 0 0
  1. public class IncDec
  2.       {
  3.          public static void main (String [] args)
  4.                {
  5.                    Alpha alpha = new Alpha (5);
  6.                    Incrementor i = new Incrementor (alpha);
  7.                    Decrementor d = new Decrementor (alpha);
  8.                    Thread t1 = new Thread (i);
  9.                    Thread t2 = new Thread (d);
  10.                    t1.start ();
  11.                    t2.start ();
  12.               }
  13.       }
  14.  
  15. class Alpha
  16.      {
  17.          private int alpha;
  18.          public Alpha (int beta)
  19.                {
  20.                    alpha = beta;
  21.                }
  22.          public void decrement ()
  23.                {
  24.                   -- this.alpha;
  25.                   System.out.println (this);
  26.                }
  27.          public void increment ()
  28.                {
  29.                   ++ this.alpha ;
  30.                   System.out.println (this);
  31.                }
  32.          public String toString ()
  33.                {
  34.                    return "Now alpha = " + alpha;
  35.                }
  36.       }
  37.  
  38. class Incrementor implements Runnable
  39.      {
  40.          private Alpha x;
  41.          public Incrementor (Alpha y)
  42.                {
  43.                   x = y;
  44.                }
  45.          public void run ()
  46.                {
  47.                   for (int i = 0; i < 10; i ++)
  48.                      {
  49.                         x.increment ();
  50.                      }
  51.                }
  52.      }
  53.  
  54. class Decrementor implements Runnable
  55.      {
  56.         private Alpha x;
  57.         public Decrementor (Alpha y)
  58.               {
  59.                  x = y;
  60.               }
  61.         public void run ()
  62.               {
  63.                 for (int i = 0; i < 10; i ++)
  64.                {
  65.                   x.decrement ();
  66.                }
  67.               }
  68.       }
  69.      
  70. Q1) The output of this program can begin with 4 or 5 or 6.
  71.  
  72. Q2) The output of this program will always end with 5.
  73.  
  74. Q3) There is a scenario in which the output of
  75. the program can begin with 5 5.
  76.  
  77. Q4) A ReentrantLock was declared as a field of the class
  78. Alpha, and defined as a member of its created objects.
  79. The code contained within the body of Alpha's methods
  80. decrement () and increment () was enclosed within
  81. a pair of lock () and unlock () method calls.
  82. The output of the modified program cannot begin with 5.
  83.  
  84.  
  85. The program was further modified by placing a
  86. call to sleep (100) after the statement
  87. x.increment () in the run () method of
  88. Incrementor.
  89.  
  90. Q5) There is no scenario in which the output can begin
  91. with 4.
  92.  
  93. Q6) There is no scenario in which the output can begin
  94. with 5.
  95.  
  96. Q7) There is a scenario in which the output can begin
  97. with 6.
  98.  
  99. Q8) There is a scenario in which the output can end
  100. with 5.
  101.  
  102. Q9) There is not a scenario in which the output can end
  103. with 4?
  104.  
  105. Q10) There is not a scenario in which the output can end
  106. with 6?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement