Gientxk 2017-11-24 11:13 采纳率: 50%
浏览 4323
已采纳

多线程问题:使用两个线程交替打印A和B,使用最大打印次数控制,详见代码。

多线程问题:使用两个线程交替打印A和B,使用最大打印次数控制,详见代码。
出现的问题是:当运行线程B中的run()方法时,程序没有先while()判断,而是直接进入了while循环体。

 /**
 * 共享资源:打印机
 * @author Tangxkun
 *
 */
public class Printer {

    //最大打印次数
    private final int MAX_COUNT = 7;
    //false表示该打印机未打印A,true表示该打印机正在打印A
    private boolean printingA = false;
    private int count = 0;

    public synchronized void printA(){
        System.out.print(count++);
        printingA = true;
        System.out.print("A-");
        notifyAll();
    }
    public synchronized void printB(){
        System.out.print(count++);
        printingA = false;
        System.out.print("B-");
        notifyAll();
    }
    public synchronized void aWaiting() throws InterruptedException{
        while(printingA == true){
            wait();
        }
    }
    public synchronized void bWaiting() throws InterruptedException{
        while(printingA == false){
            wait();
        }
    }

    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int getMAX_COUNT() {
        return MAX_COUNT;
    }
}
 /**
 * 打印A的线程
 * @author Tangxkun
 *
 */
public class PrintA implements Runnable{

    private Printer printer;

    public PrintA(Printer printer) {
        super();
        this.printer = printer;
    }

    @Override
    public void run() {
        while(printer.getCount() < printer.getMAX_COUNT()){

            printer.printA();

            try {
                printer.aWaiting();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

}
 /**
 * 打印B的线程
 * @author Tangxkun
 *
 */
public class PrintB implements Runnable{

    private Printer printer;

    public PrintB(Printer printer) {
        super();
        this.printer = printer;
    }

    @Override
    public void run() {
        while(printer.getCount() < printer.getMAX_COUNT()){

            try {
                printer.bWaiting();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            printer.printB();

        }
    }

}
 /**
 * 测试代码
 * @author Tangxkun
 *
 */
public class Test {

    public static void main(String[] args) {

        Printer printer = new Printer();
        PrintA printA = new PrintA(printer);
        PrintB printB = new PrintB(printer);

        Thread threadA = new Thread(printA,"A");
        Thread threadB = new Thread(printB, "B");

        threadA.start();
        threadB.start();

    }
}

当设置MAX_COUNT=5时,输出结果为:图片说明

当设置MAX_COUNT=6时,输出结果为:图片说明

当设置MAX_COUNT=7时,输出结果为:图片说明

烧脑了半天还是没有找出端倪,所以来请教一下各位大神!

  • 写回答

3条回答 默认 最新

  • Gientxk 2017-11-25 04:00
    关注

    原因终于找到了,是自己没有理解清楚线程挂起的概念。第一次执行线程B的时候,会while条件判断进入,然后挂起,并没有执行printer.printB(),当线程A唤醒线程B时,线程B从挂起时刻的代码处继续往后执行(执行printer.printB(),完成之前被挂起的任务),而不是重新开始执行run(),也就是说,不会再进行while条件判断,最后再次进入while循环。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 2月8日

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条