Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

java - 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?

question from:https://stackoverflow.com/questions/65838660/how-can-i-print-like-below-using-thread-synchronization-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
        
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...