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 37 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


epscape

  • Member
  • Posts: 335
Reply 3 on: Yesterday
Thanks for the timely response, appreciate it

 

Did you know?

The Romans did not use numerals to indicate fractions but instead used words to indicate parts of a whole.

Did you know?

Only 12 hours after an egg cell is fertilized by a sperm cell, the egg cell starts to divide. As it continues to divide, it moves along the fallopian tube toward the uterus at about 1 inch per day.

Did you know?

Your skin wrinkles if you stay in the bathtub a long time because the outermost layer of skin (which consists of dead keratin) swells when it absorbs water. It is tightly attached to the skin below it, so it compensates for the increased area by wrinkling. This happens to the hands and feet because they have the thickest layer of dead keratin cells.

Did you know?

Asthma cases in Americans are about 75% higher today than they were in 1980.

Did you know?

According to the American College of Allergy, Asthma & Immunology, more than 50 million Americans have some kind of food allergy. Food allergies affect between 4 and 6% of children, and 4% of adults, according to the CDC. The most common food allergies include shellfish, peanuts, walnuts, fish, eggs, milk, and soy.

For a complete list of videos, visit our video library