Recall that the Counter class initializes itself to 0 and allows a client to increment it, and the Increase class is initialized with a Counter c and an int amount and when run, invokes increment on the Counter object c amount times. Identify the possible output of the following code sequence: Counter c = new Counter(); Runnable r = new Increase(c, 2); Thread t = new Thread(r); c.increment(); t.start(); System.out.println(c);

Respuesta :

Answer:

Output can be 0 or 1 or 2.

Explanation:

As we are initializing the value with 2 so at most it will increment to 2 times means value will be 2.

when main thread is printing the c , Increase thread might be started or might not be started

so output can be 0 or 1 or 2

ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE