Skip to content
Advertisement

How Can I print like below using thread synchronization method?

I have used thread Synchronization method to print ASCII code and its value like below example.
Ex:-
A
65
B
66
C
67
.
.
.
.
Z
90

But the output is this.

enter image description here

Following are the two threads.

Thread 1

JavaScript

Thread 2

JavaScript

Main

JavaScript

What can I do for it without changing main method?

Advertisement

Answer

This can be solved using two Object monitor.

  1. One object monitor is to notify PrintingCapital after printing ASCII code.
  2. Second object monitor is to notify PrintingASCII after printing particular ASCII value.

So the two threads prints their values alternatively to achieve the required result.

I used String.class as second object monitor since the main is not allowed to be changed. You can use any other object as monitor if you can access the main method.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement