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

public class PrintingASCII extends Thread{
    private Object ob;
    
    public PrintingASCII(Object ob) {
        this.ob = ob;
    }
    
    public void run() {
        synchronized(ob) {
            for(int i=65;i<=90;i++) {
                System.out.println(i);
            }
        }
    }
}

Thread 2

public class PrintingCapital extends Thread{
    private Object ob;
    
    public PrintingCapital(Object ob) {
        this.ob = ob;
    }
    
    public void run() {
        synchronized(ob) {
            for(char i='A';i<='Z';i++) {
                System.out.println(i);
            }
        }
    }   
}

Main

public class Main {

    public static void main(String[] args) {
        Object ob = new Object();
        System.out.println("PLAAA");
        PrintingASCII thread1 = new PrintingASCII(ob);
        PrintingCapital thread2 = new PrintingCapital(ob);
        thread1.start();
        thread2.start();
    }
}

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.

class PrintingASCII extends Thread {
    private Object ob1;
    private Object ob2;

    public PrintingASCII(Object ob1) {
        this.ob1 = ob1;
    }

    public void run() {
            try {
                for (int i = 65; i <= 90; i++) {
                    synchronized(ob1) {ob1.wait();} 
                    System.out.println(i);
                    synchronized(String.class) {String.class.notify();}
                }
            }catch (Exception e) {
                System.out.println("Exc : "+e);
            }
    }
}

class PrintingCapital extends Thread {
    private Object ob1;
    private Object ob2;

    public PrintingCapital(Object ob1) {
        this.ob1 = ob1;
    }

    public void run() {
        try {
            for (char i = 'A'; i <= 'Z'; i++) {
                System.out.println(i);
                synchronized(ob1) {ob1.notify();}
                synchronized(String.class) {String.class.wait();}
            }
        }catch (Exception e) {
            System.out.println("Exc : "+e);
        }
    }
}

public class Main {

    public static void main(String[] args) {
        Object ob1 = new Object();
        PrintingASCII thread1 = new PrintingASCII(ob1);
        PrintingCapital thread2 = new PrintingCapital(ob1);
        thread1.start();
        thread2.start();
        
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement