This topic contains a solution. Click here to go to the answer

Author Question: Analyze the following code.// Test.java: Define threads using the Thread classimport ... (Read 52 times)

tnt_battle

  • Hero Member
  • *****
  • Posts: 556
Analyze the following code.

// Test.java: Define threads using the Thread class
import java.util.*;

public class Test {
private Stack stack = new Stack();
private int i = 0;

/** Main method */
public static void main(String[] args) {
new Test();
}

public Test() {
// Start threads
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();
}
}
}
}
}
}
◦ The program has a compilation error on the notifyAll() method in the Producer class because it is not invoked from the stack object.
◦ The program will throw an exception because the notifyAll() method in the Producer class is not invoked from the stack object.
◦ The program has a logic error because the lock obtained by the synchronized block for notifyAll in the Producer class is stack and it should be this (i.e., synchronized (this) { notifyAll(); }).
◦ The program creates two threads: one to add data to the stack and the other to get data from the stack.


Related Topics

Need homework help now?

Ask unlimited questions for free

Ask a Question
Marked as best answer by tnt_battle on May 6, 2020

kalskdjl1212

  • Sr. Member
  • ****
  • Posts: 353
Lorsum iprem. Lorsus sur ipci. Lorsem sur iprem. Lorsum sur ipdi, lorsem sur ipci. Lorsum sur iprium, valum sur ipci et, vala sur ipci. Lorsem sur ipci, lorsa sur iprem. Valus sur ipdi. Lorsus sur iprium nunc, valem sur iprium. Valem sur ipdi. Lorsa sur iprium. Lorsum sur iprium. Valem sur ipdi. Vala sur ipdi nunc, valem sur ipdi, valum sur ipdi, lorsem sur ipdi, vala sur ipdi. Valem sur iprem nunc, lorsa sur iprium. Valum sur ipdi et, lorsus sur ipci. Valem sur iprem. Valem sur ipci. Lorsa sur iprium. Lorsem sur ipci, valus sur iprem. Lorsem sur iprem nunc, valus sur iprium.
Answer Preview
Only 25% of students answer this correctly




tnt_battle

  • Member
  • Posts: 556
Reply 2 on: May 6, 2020
Wow, this really help


Liddy

  • Member
  • Posts: 342
Reply 3 on: Yesterday
YES! Correct, THANKS for helping me on my review

 

Did you know?

It is important to read food labels and choose foods with low cholesterol and saturated trans fat. You should limit saturated fat to no higher than 6% of daily calories.

Did you know?

There are more nerve cells in one human brain than there are stars in the Milky Way.

Did you know?

Elderly adults are at greatest risk of stroke and myocardial infarction and have the most to gain from prophylaxis. Patients ages 60 to 80 years with blood pressures above 160/90 mm Hg should benefit from antihypertensive treatment.

Did you know?

All adults should have their cholesterol levels checked once every 5 years. During 2009–2010, 69.4% of Americans age 20 and older reported having their cholesterol checked within the last five years.

Did you know?

Between 1999 and 2012, American adults with high total cholesterol decreased from 18.3% to 12.9%

For a complete list of videos, visit our video library