I don't know how to use Notify and wait in my program but I am required to. The point of my program is that there are two threads, one which assigns the inputted object to the numbers 1-10 and another that reads them. Could anyone help.

package vairables;

public class Vairables {

    public static void main(String[] args) 
    {
        class IntegerHolder
        {
            String variable = null;
            synchronized void insert(String v)
            {
                while (variable != null)
                {
                    
                    notify();
                    
                    wait();
                }
                variable = v;
                notify();
            }
            
            synchronized int extract()
            {
                while ( variable == null)
                {
                    notify();
                    
                    wait();
                }
                String temp = variable;
                variable = null;
                notify();
                return temp;
            }
        }
    }
}

What happened to your earlier post of this problem?

Where is the code to test the methods? The main method looks empty.

The code you posted does NOT compile.

try 
		{
		Thread.sleep(5000); // do nothing for 5000 milLiseconds (5 seconds)
		} 
		catch(InterruptedException e)
		{
		e.printStackTrace();
		}

Timer, no need for another method

What is the posted code for? How is it related to the problem?

It makes the program pause for 5 seconds. Doesn't he want to make it wait...?

No. He is talking about using the Object class's wait() method.

Sorry :(. I'm relatively new here & to programming.

This is the assignment. And i need to know how to use it and how to add the values to 1-10 and then extract it.
Write a short program that creates two threads, one of which successively sets a variable to the integers from 1 to 10, and another that reads the values, printing each one as it goes. Use synchronized methods, wait() and notify(). Use a separate condition variable to signify that the integer variable is empty. Since the whole point of the exercise is to make sure that every written value is read, without any values being skipped or overwritten, pay special attention to access control. Look at the sample code in Section 7 of this module's commentary for an idea of how to proceed.

My code now is. How would I assign the user input to 1-10
and extract it by telling it

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vairables;
import java.io.*;

public class Vairables {

    public static void main(String[] args) throws IOException 
    {
        
        final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in), 1);
        final String userInput1 = stdin.readLine();
        class IntegerHolder
        {
            
            String variable = null;
            synchronized void insert(String v) throws IOException
            {
                for (int i = 0; i < 10; i++)
                {
                    String one = userInput1;
                    System.out.println(one);
                }
                while (variable != null)
                {
                    
                    notify();
                    
                    //wait();
                 
                }
                variable = v;
                notify();
            }
            
            synchronized int extract()
            {
                while ( variable == null)
                {
                    notify();
                    
                    //wait();
                }
                String temp = variable;
                variable = null;
                notify();
                return temp;
            }
        }
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                IntegerHolder in = new IntegerHolder();
            }
        });
    }

}

I changed the whole thing

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program;
import java.io.*;
import java.util.LinkedList;

public class program<E>
{
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in), 1);
        LinkedList<E> q = new LinkedList<E>();
        public synchronized void push(E o){
            q.add(o);
            this.notifyAll();
        }
        public synchronized E pop()
        {
            while (q.size() == 10)
            {
                try {this.wait();}
                catch (InterruptedException ignore) {}
                
            }
            return q.remove(0);
                
        }
   
    public static void main(String[] args) 
    {
        
    }
}

Not sure what to recommend with these partial pieces of code. There really isn't anything here that makes sense.

Aren't you supposed to extends a Thread class or at least incorporate the class to your implementing thread class? Then you create/use 2 thread objects using your new class in another class. You can call wait() and/or notify() in your implementing thread class, but it will depend on how you implement the thread class.

/*
     main class
  (int global_value)
       /  \
      /    \
     /      \
 thread1   thread2
write_int  read_int
*/

Hmm... You did not talk about the relationship between read/write the number...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.