题海让大学四年没有难题
白天模式登陆

题目详情

单选题 分析代码.下面陈述正确的是 ?import java.util.*;public class Test { private Stack stack = new Stack(); private int i = 0; public static void main(String[] args) { new Test(); } public Test() { new Producer().start(); new Consumer().start(); } class Producer extends Thread { public void run() { while (true) { System.out.println("Producer: put " + i); stack.push(new Integer(i++)); synchronized (stack) { notifyAll(); } } } } class Consumer extends Thread { public void run() { while (true) { synchronized (stack) { try { while (stack.isEmpty()) stack.wait(); System.out.println("Consumer: get " + stack.pop()); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } }}

A. 程序创建了两个线程: 一个向stack中添加数据,一个从stack中获取数据

B. 程序有一个编译错误,因为Producer类中的notifyAll()方法没有通过stack对象来调用

C. 程序抛出一个异常,因为Producer类中的notifyAll()方法没有通过stack对象来调用

D. 程序有一个逻辑错误

学科:高级语言程序设计

时间:2023-05-17 02:47:02

Copyright © 2022 津ICP备2021001502号