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 39 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
Excellent


miss_1456@hotmail.com

  • Member
  • Posts: 289
Reply 3 on: Yesterday
Great answer, keep it coming :)

 

Did you know?

Stevens-Johnson syndrome and Toxic Epidermal Necrolysis syndrome are life-threatening reactions that can result in death. Complications include permanent blindness, dry-eye syndrome, lung damage, photophobia, asthma, chronic obstructive pulmonary disease, permanent loss of nail beds, scarring of mucous membranes, arthritis, and chronic fatigue syndrome. Many patients' pores scar shut, causing them to retain heat.

Did you know?

People with alcoholism are at a much greater risk of malnutrition than are other people and usually exhibit low levels of most vitamins (especially folic acid). This is because alcohol often takes the place of 50% of their daily intake of calories, with little nutritional value contained in it.

Did you know?

More than 2,500 barbiturates have been synthesized. At the height of their popularity, about 50 were marketed for human use.

Did you know?

The U.S. Preventive Services Task Force recommends that all women age 65 years of age or older should be screened with bone densitometry.

Did you know?

The most dangerous mercury compound, dimethyl mercury, is so toxic that even a few microliters spilled on the skin can cause death. Mercury has been shown to accumulate in higher amounts in the following types of fish than other types: swordfish, shark, mackerel, tilefish, crab, and tuna.

For a complete list of videos, visit our video library