Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class IncDec
- {
- public static void main (String [] args)
- {
- Alpha alpha = new Alpha (5);
- Incrementor i = new Incrementor (alpha);
- Decrementor d = new Decrementor (alpha);
- Thread t1 = new Thread (i);
- Thread t2 = new Thread (d);
- t1.start ();
- t2.start ();
- }
- }
- class Alpha
- {
- private int alpha;
- public Alpha (int beta)
- {
- alpha = beta;
- }
- public void decrement ()
- {
- -- this.alpha;
- System.out.println (this);
- }
- public void increment ()
- {
- ++ this.alpha ;
- System.out.println (this);
- }
- public String toString ()
- {
- return "Now alpha = " + alpha;
- }
- }
- class Incrementor implements Runnable
- {
- private Alpha x;
- public Incrementor (Alpha y)
- {
- x = y;
- }
- public void run ()
- {
- for (int i = 0; i < 10; i ++)
- {
- x.increment ();
- }
- }
- }
- class Decrementor implements Runnable
- {
- private Alpha x;
- public Decrementor (Alpha y)
- {
- x = y;
- }
- public void run ()
- {
- for (int i = 0; i < 10; i ++)
- {
- x.decrement ();
- }
- }
- }
- Q1) The output of this program can begin with 4 or 5 or 6.
- Q2) The output of this program will always end with 5.
- Q3) There is a scenario in which the output of
- the program can begin with 5 5.
- Q4) A ReentrantLock was declared as a field of the class
- Alpha, and defined as a member of its created objects.
- The code contained within the body of Alpha's methods
- decrement () and increment () was enclosed within
- a pair of lock () and unlock () method calls.
- The output of the modified program cannot begin with 5.
- The program was further modified by placing a
- call to sleep (100) after the statement
- x.increment () in the run () method of
- Incrementor.
- Q5) There is no scenario in which the output can begin
- with 4.
- Q6) There is no scenario in which the output can begin
- with 5.
- Q7) There is a scenario in which the output can begin
- with 6.
- Q8) There is a scenario in which the output can end
- with 5.
- Q9) There is not a scenario in which the output can end
- with 4?
- Q10) There is not a scenario in which the output can end
- with 6?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement