简体   繁体   English

如何使用线程同步方法进行如下打印?

[英]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.我已经使用线程同步方法来打印 ASCII 代码及其值,如下例所示。
Ex:-前任:-
A一个
65 65
B
66 66
C C
67 67
. .
. .
. .
. .
Z Z
90 90

But the output is this.但是output就是这个。

在此处输入图像描述

Following are the two threads.以下是两个线程。

Thread 1线程 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线程 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?在不改变主要方法的情况下我能做些什么?

This can be solved using two Object monitor.这可以使用两个 Object 监视器来解决。

  1. One object monitor is to notify PrintingCapital after printing ASCII code.一台 object 监视器用于在打印 ASCII 码后通知 PrintingCapital。
  2. Second object monitor is to notify PrintingASCII after printing particular ASCII value.第二个 object 监视器是在打印特定的 ASCII 值后通知 PrintingASCII。

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.我使用 String.class 作为第二个 object 监视器,因为不允许更改主监视器。 You can use any other object as monitor if you can access the main method.如果您可以访问 main 方法,您可以使用任何其他 object 作为监视器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 7 中实现“像承诺一样”的线程同步系统? - How can I implement a "promise like" thread-synchronization system in Java 7? 如何使用如下线程获取 output? - How to get output using thread like below? 我该如何重写此主线程-工作线程同步 - How can I rewrite this main thread - worker threads synchronization 如何在 java 中打印以下结果? - How can I print below result in java? 如何使方法线程安全,即使我忘记在方法或块级别添加同步而不更改文件? - How to make method thread safe, even I forget to add synchronization on method or block level without changing file? 我如何打印线程状态? - How i can print thread status? 如何使用 Swing 在按钮内打印 void 方法? - How can I print a void method inside a button using Swing? 我应该使用哪种同步方法来暂停使用者队列线程? - What synchronization method should I use for pausing a consumer queue thread? 如何为有意义的相等对象创建线程锁/同步并防止各个线程的并行执行? - How can I create a thread lock/synchronization for meaningfully equal objects and prevent parallel execution of the respective threads? 如何安排GUI和GUI组件状态读取的线程同步? - how can i arrange gui and thread synchronization of gui component's state reading?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM