Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Subtle bug in Deitel & Deitel "Java How to Program" book

843807Dec 31 2009
Merry x mas and happy new year, guys.

I have this applet (which is at the same time runnable and listener) its printed in Deitel & Deitel "Java How to Program" book 3rd ed.

The program works but as you turn on and off the "suspend" checkboxes some of the threads are not notified, so they go to infinite wait state, deadlock. I couldn't figure out what is wrong or how to fix the problem.. please read the code below, or copy and paste this code to your eclipse, its just one file, then play with the check boxes, some of the threads don't wake up after wait... technically they don't shift from WAITING to TIMED_WAITING state.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author 
 */
// Fig. 15.7: RandomCharacters.java
// Demonstrating the Runnableinterface
public class RandomCharacters extends JApplet implements Runnable, ActionListener {

    private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private JLabel outputs[];
    private JCheckBox checkboxes[];
    private final static int SIZE = 3;
    private Thread threads[];
    private boolean suspended[];

    public void init() {
        outputs = new JLabel[SIZE];
        checkboxes = new JCheckBox[SIZE];
        threads = new Thread[SIZE];
        suspended = new boolean[SIZE];

        Container c = getContentPane();
        c.setLayout(new GridLayout(SIZE, 2, 5, 5));

        for (int i = 0; i < SIZE; i++) {
            outputs[i] = new JLabel();
            outputs.setBackground(Color.green);
outputs[i].setOpaque(true);
c.add(outputs[i]);

checkboxes[i] = new JCheckBox("Suspended");
checkboxes[i].addActionListener(this);
c.add(checkboxes[i]);
}
}

public void start() {
// create threads and start every time start is called
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(this, "Thread " + (i + 1));
threads[i].start();
}
}

public void run() {
Thread currentThread = Thread.currentThread();
int index = getIndex(currentThread);
char displayChar;

while (threads[index] == currentThread) {
// sleep from 0 to 1 second
try {
Thread.sleep((int) (Math.random() * 1000));

synchronized (this) {
while (suspended[index]
&& threads[index] == currentThread) {
wait();
}
}
} catch (InterruptedException e) {
System.err.println("sleep interrupted");
}

displayChar = alphabet.charAt(
(int) (Math.random() * 26));
outputs[index].setText(currentThread.getName() + ": " + displayChar);
}

System.err.println(currentThread.getName() + " terminating");
}

private int getIndex(Thread current) {
for (int i = 0; i < threads.length; i++) {
if (current == threads[i]) {
return i;
}
}

return -1;
}

public synchronized void stop() {
// stop threads every time stop is called
// as the user browses another Web page
for (int i = 0; i < threads.length; i++) {
threads[i] = null;
}

notifyAll();
}

public synchronized void actionPerformed(ActionEvent e) {
for (int i = 0; i < checkboxes.length; i++) {
if (e.getSource() == checkboxes[i]) {
suspended[i] = !suspended[i];

outputs[i].setBackground(
!suspended[i] ? Color.green : Color.red);

if (!suspended[i]) {
notify();
}

return;
}
}
}
}
Thanks in advance.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 28 2010
Added on Dec 31 2009
0 comments
251 views