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 51 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
Great answer, keep it coming :)


T4T

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

 

Did you know?

Acute bronchitis is an inflammation of the breathing tubes (bronchi), which causes increased mucus production and other changes. It is usually caused by bacteria or viruses, can be serious in people who have pulmonary or cardiac diseases, and can lead to pneumonia.

Did you know?

Elderly adults are living longer, and causes of death are shifting. At the same time, autopsy rates are at or near their lowest in history.

Did you know?

The horizontal fraction bar was introduced by the Arabs.

Did you know?

About 100 new prescription or over-the-counter drugs come into the U.S. market every year.

Did you know?

The lipid bilayer is made of phospholipids. They are arranged in a double layer because one of their ends is attracted to water while the other is repelled by water.

For a complete list of videos, visit our video library