Archived

This topic is now archived and is closed to further replies.

[java] Damn I'm such a n00b. I can't set up a wait command properly.

This topic is 5133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

AFAIK the wait command is supposed to get rid of the lock on the block of code and let me change the value of the variable in the while statement which then it will detect with notifyAll() and then it will check the while condition and proceed as normal or repeat the procedure above. For some reason I spent a while trying to figure out the problem but everytime I look through tutorials it seems my code is 100% right. So I ask, what exactly is the problem? Thank You.
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class a extends JFrame
{
	
	boolean available = false;
	int contents = 0;
	
	int even = 0;
	
	String txtStr = "Available:";
	String txtVal = "0";
	
	JButton btn = new JButton("available");
	JTextArea txt = new JTextArea();
	JTextArea txtType = new JTextArea();
	
	a()
	{
		
		Container cont = getContentPane();
		JPanel pane = new JPanel();
		
		txtStr = "Available:" + txtStr.valueOf(available);
		txtStr+= " Contents:" + txtStr.valueOf(contents);
		
		pane.add(btn);
		pane.add(txt);
		pane.add(txtType);
		cont.add(pane);
		
		btn.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					available = false;
					
					even++;					
					
					put(contents);
					
					txtVal = txtVal.valueOf(contents);
		
					contents = get();
					
					txtStr = "Contents: " + txtStr.valueOf(contents) + " Available:" + txtStr.valueOf(available);
					
					txt.setText(txtStr);
				}
			}
		);
		
	}
	
	public synchronized int get() 
	{
    while(available == false) 
    {
        try 
        {
            // wait for Producer to put value

            wait();
        }catch (InterruptedException e) {}
        
    }
    available = false;
    // notify Producer that value has been retrieved

    notifyAll();
    return contents;
	}
	
	
	
	public synchronized void put(int value) 
	{
	    while (available == true) 
	    {
	        try 
	        {
	            // wait for Consumer to get value

	            wait();
	        }catch (InterruptedException e){}
	    }
	    contents = value;
	    available = true;
	    
	    // notify Consumer that value has been set

	    notifyAll();
	}
	
	public static void main(String[] args)
	{
		a frame = new a();
		
		frame.setVisible(true);
		frame.pack();
		frame.validate();
	}
	
}
EDIT: http://members.optusnet.com.au/ksaho/Algorithm/JAVA/thread.java It works perfect now. [edited by - K_I_L_E_R on November 27, 2003 3:25:44 AM]

Share this post


Link to post
Share on other sites
First, this is wrong:

txtStr = "Contents: " + txtStr.valueOf(contents) + " Available:" + txtStr.valueOf(available);

You should never access static methods of a class from an instance of that class. Do this instead:

txtStr = "Contents: " + String.valueOf(contents) + " Available:" + String.valueOf(available);

second, you could have just done this:

txtStr = "Contents: " + contents + " Available:" + available;

String conversion of primitive types is automatic in Java as long as there is a String in the equation.

Next, your code works as expected. There is no place where you modify contents, so it is always 0. And you only check available after it has been set to false.

Also, you are also breaking one of the rules of syncronized code that can cause unpredictable results. You should never modify variables that are part of a sync code block outside of that code. You modify contents in your ActionListener.



First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

[edited by - CaptainJester on November 28, 2003 7:14:44 AM]

Share this post


Link to post
Share on other sites