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 5 of 5

Thread: How to use and notify

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to use and notify

    Dear all
    I have four classes that extends thread. the names are for example C1, C2, C3, C4. now these threads shares a linked list between each other. C1 writes data to a list and C2 reads from that list and writes to other list and C3 reads from that other list. now C4 executes the run() method for these threads like this:

    ...
    for(int i=0;i<Filters.size();i++)
       Filters.get(i).run();
    ...

    but the problem is that when C1 executes, it writes the data to list and after that C2 executes and reads that data. but C2 needs another data but in this state the list is empty. so C2 calls wait() and waits. but this leads to C3 not run. I want to continue running after C2 waits. I want to run C3 and after that C1 and so on

    but I don't know what is the problem when C2 waits, my all program waits!!!!!!!!!!!!!!!

    Please help me if there is a solution

    thanks: Saeid


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

    Default Re: How to use and notify

    How are you using the wait and notify methods?

  3. The Following User Says Thank You to Norm For This Useful Post:

    Saeid (August 12th, 2010)

  4. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to use and notify

    the method that writes into list, the list is called OutChannel

        	synchronized(OutChannel)
        	{
        		DataItem.DataField =Data;
        		DataItem.Valid=true;
     
                   OutChannel.Push(DataItem);
                   OutChannel.notify();
        	}

    and the method that reads from list, the list called InChannel

    		synchronized(InChannel)
    		{
     
    		    while(InChannel.Size()==0)
    			    try
    		        {
    			       InChannel.wait();
    			    }catch (InterruptedException e){}
    		    Poped=InChannel.Pop();
    		    System.out.println("Poped: " + Poped.DataField + "   " + Poped.Valid);
    		    if (!Poped.Valid)
    		    {
    			    return null;
    		    }
    		    else
    		        return (Poped.DataField);

    the names are different in threads C1 and C2, but both pointers points to one list, and these pointer are set in C4. in the above code if C2 executes InChannel.wait(), all my program waits. but I want just C2 waits and C4 runs the C3 after that C1 and again C2 and C3 and so on

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use and notify

    Do the variables: InChannel and OutChannel refer to the same object? wait and notify won't work if they are different objects.
    The use of wait and notify requires that they be used with the same object. Read the Object class doc for those two methods.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Saeid (August 12th, 2010)

  7. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to use and notify

    OK. thanks