Which statement is true?

Given:

1. class PingPong2 {
2. synchronized void hit(long n) {
3. for(int i = 1; i < 3; i++)
4. System.out.print(n + “-” + i + ” “);
5. }
6. }

1. public class Tester implements Runnable {
2. static PingPong2 pp2 = new PingPong2();
3. public static void main(String[] args) {
4. new Thread(new Tester()).start();
5. new Thread(new Tester()).start();
6. }
7. public void run() { pp2.hit(Thread.currentThread().getId()); }
8. }

Which statement is true?

Given:

1. class PingPong2 {
2. synchronized void hit(long n) {
3. for(int i = 1; i < 3; i++)
4. System.out.print(n + “-” + i + ” “);
5. }
6. }

1. public class Tester implements Runnable {
2. static PingPong2 pp2 = new PingPong2();
3. public static void main(String[] args) {
4. new Thread(new Tester()).start();
5. new Thread(new Tester()).start();
6. }
7. public void run() { pp2.hit(Thread.currentThread().getId()); }
8. }

Which statement is true?

A.
The output could be 5-1 6-1 6-2 5-2

B.
The output could be 6-1 6-2 5-1 5-2

C.
The output could be 6-1 5-2 6-2 5-1

D.
The output could be 6-1 6-2 5-1 7-1

Explanation:
Since hit method is synchronized the answer for this question will be
n-1 n-2 m-1 m-2



Leave a Reply 0

Your email address will not be published. Required fields are marked *