Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help wanted using wait() and notify()

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help wanted using wait() and notify()

    Hello, my name is Sandra. I have a question about wait() and notify() in Java.
    The task is to implement something like:

    1) wait for the boolean variable "world" to be true
    2) when its true, then run "usa()"

    Write a method by extending the existing class...

    the starting point is:
    class abc {
    	private bool world;
    	public static usa(){System.out.println("USA");}
    }


    Now i have to implement the task using wait() and notify/notifyAll().
    My solution is:

    class abc {
    	private bool world;
    	public static usa(){System.out.println("USA");}
    	public synchronized void act(){
    		while (!world) try{
    			wait();
    		} catch(InterruptedException ex)
    		usa();
    		notifyAll();}
    }

    Is this correct? Sorry, i have no java skills, but we have to take this course
    So pls help me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help wanted using wait() and notify()

    To test the code for the task, you'll need two threads: one to wait and one to do the notify after world is set true.

    The wait and the notify methods must use the same object when called.

    How did you get this assignment if you have no java skills? This is not a beginning programmer problem.
    What have gotten in class about using the wait and notify methods and about threads?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help wanted using wait() and notify()

    Yes i know, this is usually not for beginners. but i have to take this course, giving me no alternative. the course is about parallel programming...
    i got the examples from the lecture notes and tried to understand it. i read much in the web, so that was my result.

    so what i didn't get: is my code wrong? I found a similar example and tried to transform it to my problem...

    class abc {
        private bool world;
        public static usa(){System.out.println("USA");}
     
     
        public void act(){
            synchronized(this){
                while(!world){
                    wait();
                }
            }
            usa();
        }
     
        public void maketrue(){
            synchronized(this){
                 this.world = true;
                 notifyAll();
            }
        }
    }

    So please help me. When you have a good page for me, where i can get the basics, i would really be happy. i have to pass this course, however...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help wanted using wait() and notify()

    Do you have code to test with? A class that has a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help wanted using wait() and notify()

    no, thats what you meant with
    Quote Originally Posted by Norm View Post
    To test the code for the task, you'll need two threads: one to wait and one to do the notify after world is set true.
    right?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help wanted using wait() and notify()

    How do you test the code you have written? Can you post some code here that will call the methods for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help wanted using wait() and notify()

    actually i am not testing the code, don't know how to do. thats the reason why i am here. in the course the teacher gives many examples, one in java, the next in c,...
    we didnt get any introduction, cause the course is normally not for beginners like me. so what i did until now: i read many on the web, actually beginning with my first hello world project. so maybe it could be possible, that you tell me, how the main method looks like and i could test it in eclipse?

    my solution would be to call in main the act() method -> nothing happens
    call the maketrue() followed by act() -> an output appears

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help wanted using wait() and notify()

    The testing code needs to start two threads.
    The first will wait for the variable to be set.
    The second will set the variable and notify those waiting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. wait(), sleep() and notify()
    By evthim in forum Threads
    Replies: 3
    Last Post: October 30th, 2012, 11:37 AM
  2. How to notify() and proceed past wait()
    By kernel_klink in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 21st, 2012, 07:40 PM
  3. let first thread through, the rest has to wait
    By hamsterofdeath in forum Threads
    Replies: 0
    Last Post: November 19th, 2010, 01:32 PM
  4. How to use and notify
    By Saeid in forum Threads
    Replies: 4
    Last Post: August 12th, 2010, 11:12 AM
  5. Delay/Wait/Pause in Java + AirBrushing
    By obliza in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 27th, 2009, 10:27 AM