【问题标题】:How Can I print like below using thread synchronization method?如何使用线程同步方法进行如下打印?
【发布时间】:2021-04-26 12:52:54
【问题描述】:

我使用线程同步方法打印 ASCII 码及其值,如下例所示。
例如:-
一个
65

66
C
67
.
.
.
.
Z
90

但输出是这样的。

以下是两个线程。

线程 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);
            }
        }
    }
}

线程 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);
            }
        }
    }   
}

主要

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

在不改变main方法的情况下我能做什么?

【问题讨论】:

  • System.out.printlnob.notify()之一之前添加ob.wait()
  • 并将syncronized 放在等待/通知调用周围的循环中,例如for(char i='A';i&lt;='Z';i++) {synchronized(ob) {ob.wait();} System.out.println(i);}

标签: java multithreading oop synchronization synchronized


【解决方案1】:

这可以使用两个对象监视器来解决。

  1. 一个对象监视器是在打印 ASCII 码后通知 PrintingCapital。
  2. 第二个对象监视器是在打印特定的 ASCII 值后通知 PrintingASCII。

所以两个线程交替打印它们的值以达到所需的结果。

我使用 String.class 作为第二个对象监视器,因为不允许更改 main。如果您可以访问 main 方法,则可以使用任何其他对象作为监视器。

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

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多