Page 3 of 6 FirstFirst 12345 ... LastLast
Results 41 to 60 of 116
  1. #41
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Object mon = cont.setstartFlag(false);

    and later

    Object mon = new Object();

    So the object is mon but why does it point to cont.setstartFlag(false);

  2. #42
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Sorry, my code was a bit quick and dirty there. I used the setStartFlag method to return the monitor object so that the calling method would have a reference to it to synchronize on.
    The mon object was created before the setStartFlag method was called.
    For synchronize, wait and notify to work, they must all use the same object, mon.

  3. #43
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Lets try this idea and see if I can get a better understanding by the hand of the following pseudo code:

    Java Code:
    Public class A  {
    Private B b;
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    play();  // starts the soundblock thread
    	}
    
    private play()  {  // the method that creates the thread object
    	play p = new Play();
    	p.start();
            [COLOR="blue"]notify() or better x.notify() could come here
            or anywhere as long as it is not in the Play class?[/COLOR]
    }
    
    
    Private class Play extends Thread  {
    
    	Public void run()  {
    
    		While (true)  {    // keeps the soundblock thread going
    
    			While (!b.getflg())  {   // when set thrue soundblock starts
                            [COLOR="Blue"]here goes wait() or better x.wait()
                            but where goes notify() or better x.notify()[/COLOR]
    			}
                   soundblock();  // contains all soundtracks 
                   }
    	}
    }  // end of play thread class
    
    
    private void soundblock()  {}  // here is all sound midi data
    
    }  //and of first public
    
    
    
    Public class B  extends JPanel {   // all the GUI stuff of the app.
    
    Boolean flg;
    
    Public Boolean getflg()  {   // starts the soundblock
    	return flg;     
    }
    
    } // end of second public class
    I hope by hand of this model to get some more understanding of notify and wait because api etc are confusing. Of course it is important not to have any wait or notify but issue it with a specific thread. One also needs to synchronize. (I hope the pseudo has no mistakes).

  4. #44
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    There are a least two mistakes
    1) the constructor should be public
    2) when creating the thread object p, play should be Play

    In the metafore of the article

    Thread Synchronization and the Java Monitor

    The monitor is a building with three rooms
    1) the hall where threads arrive
    2) the main room, where just one thread owns the monitor
    3) the wait room where threads go to sleep by wait() and are notified to enter the main room

  5. #45
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    this monitor is actually the object with the lock?

  6. #46
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Yes. You must use the same object for the synchronize, wait and notify. I named that single object: monitor

  7. #47
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post major improvement

    Your code is much better, the comments are telling of central progress. Trying to name classes according to what they do collapses some design and control techniques: Play - as a class - has an action as it's name, an action generally is better concepted as a method, thus some of the OO'ers may provide additional comments. I would, at least for now, put an action named play() in the controller class, which we would definitely view as the GUI class until this is sorted better.

    Musical data is organized into a system of lists, each containing staves, each staff containing a list of objects like notes, rests, and so on, and each object containing a variety of attributes. Every object in java has a monitor, thus one can wait on any stave to complete.

    Rather than using hall, main and wait, I would view the hall as constructor and initilaization, rename main to Play, and view wait as what to do once a stave has completed, for which I suggest enabling a button in the GUI or setting some Label to whatever musical notation has in it's rich history according to established practice in prior art.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #48
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Java Code:
    class WaitThread implements Runnable{
    *
    	private Object lock = new Object();  // <------- object for common use
    *
    	public Object getLockObject(){       // <----- to share the lock
    		return lock;
    	}
    *
    	public void run() {
    		
    		final Object lock=new Object();
    		System.out.println("Thread going to wait state..");
    	    synchronized ( getLockObject() ){ <-- use the lock directly or by method
    	    	System.out.println("synchronized block one "+lock);
    		try{			
    //			lock.wait(); // <--- you want the lock to wait?	
    			wait(); // <-----  let the current thread wait instead	
    		}catch(InterruptedException e){
    			System.out.println(e);
    		}		
    	 }
    	    System.out.println("Thread going to wokeup using notify method..");
      }
    }
     
    public class TestWait {
     
    	public static void main(String[] args) {
    		System.out.println("Enter into the program..");
    //		final Object lock=new Object();       <--------------- this is senseless
    		WaitThread wt1=new WaitThread();
    		Thread t1=new Thread(wt1);
    		t1.start();
    		
    		synchronized (wt1.getLockObject()) {  // <------------ use the common lock object
    			System.out.println("synchronized block two"+lock);
    //			lock.notify();   //<----  why you want to notify the lock? 
    			t1.notify(); // <---- notify the thread instead
    			System.out.println("after notifying..");				
    		}
    		System.out.println("Exit from the program..");
    	}
     
    }

  9. #49
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    I found this code (and lost the web site where it is from)

    wait(); // <----- let the current thread wait instead

    does that mean that the code stops running from here and contious
    after notify?

  10. #50
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    lock.wait(); // <--- you want the lock to wait?
    wait(); // <----- let the current thread wait instead

    what is the differences (lock is an Object...)

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    You must use the same object for the synchronize, wait and notify
    Your code is not using the same object. You create new ones for each method.
    Look at my code. Try modifying it to fit in with your code
    lock.wait(); // <--- you want the lock to wait?
    wait(); // <----- let the current thread wait instead

    what is the differences (lock is an Object...)
    The system associates a "monitor" with EVERY object. lock is one object and "this" is another object. "this" is the reference to the current object.
    wait();
    this.wait();
    are calling the wait method for the same object.
    If you don't use the same object to wait and notify, one thread will be waiting in the queue for object one and the other thread will doing notify on another object's queue. EVERY OBJECT HAS ITS OWN WAIT QUEUE.
    You must use the SAME object for the wait and notify.

  12. #52
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    norm, the code is not mine and is not using the same object (look carefully the // show a corrected situation).
    I am very interested in the answer of the first question:
    wait(); // <----- let the current thread wait instead

    does that mean that the thread stops running from here and continious
    after notify?

  13. #53
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    I know there is still no notify and object lock in this code.
    BUT DOES IT MAKE SENCE THE HAVE THE WAIT KEYWORD INSIDE THE WAIT LOOP OF THE RUN OBJET?
    So the wait() puts the current thread (which is the waitloop) to sleep, correct!


    Java Code:
    Public class A  {
    Private B b;
    
    
    	
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    play();  // starts the soundblock thread
    	}
    
    
    
    private play()  {  // the method that creates the thread object
    	Play p = new Play();
    	p.start();
    }
    
    
    Private class Play extends Thread  {
    
    	Public void run()  {
    
    		While (true)  {    // keeps the soundblock thread going
    
    			While (!b.getflg())  {   // when set thrue soundblock starts
    			
    				try{				
    				wait(); // <-----  let the current thread wait instead	
    				}catch(InterruptedException e){
    				System.out.println(e);
    				}
    			}
                            			}
         soundblock();  // contains all soundtracks 
                  
    	}
    }  // end of play thread class
    
    
    private void soundblock()  {}  // here is all sound midi data
    
    }  //and of first public
    
    
    
    Public class B  extends JPanel {   // all the GUI stuff of the app.
    
    Boolean flg;
    
    Public Boolean getflg()  {   // starts the soundblock
    	return flg;     
    }
    
    } // end of second public class

  14. #54
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    So one could say simply:

    Java Code:
    Public void run()  {
    
    		While (true)  {    // keeps the soundblock thread going
    			
    				try{				
    				wait(); // <-----  let the current thread wait instead	
    				}catch(InterruptedException e){
    				System.out.println(e);
    				}
                           
                     soundblock();  // contains all soundtracks 
                  
    	         }
    }
    And move the
    While (!b.getflg()) {
    into the mean thread together with the notify()
    (adding
    synchronized(lock) {
    on both wait and notify bassed on a one single
    private Object lock = new Object();

    This stuff is really confusing me!

  15. #55
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    So recapitulating, when the above run sees wait, it does not procede with the soundblock(); because it is already at sleep,
    and by awaking it in an other thread it will do soundblock()

  16. #56
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    This should do it though not sure about notify


    Java Code:
    Public class A  {
    Private B b;
    private Object lock = new Object();  // <------- object for common use
    
    	
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    play();  // starts the soundblock thread
    	}
    
    public Object getLockObject(){       //to share the lock
    		return lock;
    	}
    
    private play()  {  // the method that creates the thread object
    	Play p = new Play();
    	p.start();
    }
    
    
    Private class Play extends Thread  {
    
    	Public void run()  {
    
    		While (true)  {    // keeps the soundblock thread going
    
    				synchronized (b.getLockObject() ){   		
    					try{				
    					wait(); // let the current thread wait or maybe better p.wait()	
    					}catch(InterruptedException e){
    					System.out.println(e);
    					}
    				}
    		soundblock(); 
    		}
         }
         
                  
    	}
    }  // end of play thread class
    
    
    private void soundblock()  {}  // here is all sound midi data
    
    }  //and of first public
    
    
    
    Public class B  extends JPanel {   // all the GUI stuff of the app.
    
    Boolean flg;
    private Object lock = new Object();  // object for common use
    
    public B()   {
    		for (int count = 0; count < 900000; count++) ; // some delay
    			synchronized (getLockObject()) {  //use the common lock object
    				notify(); //  I am not sure if this notifies the above thread.... what a mess		
    			}
    
    
    		
    
    }
    *
    	public Object getLockObject(){       // <----- to share the lock
    		return lock;
    	}
    
    Public Boolean getflg()  {   // starts the soundblock
    	return flg;     
    }
    
    } // end of second public class

  17. #57
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    IMPORTANT THE THIRD CODE LINE SHOULD BE OUT

    private Object lock = new Object(); // <------- object for common use

  18. #58
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    I AM VERY SORRY THE ABOVE IS A MESS SO READ FROM HERE
    THIS SHOULD WORK NOT SURE ABOUT THE NOTIFY PART OF THE SECOND PUBLIC CLASS...... DOES IT NOTIFY THE THREAD OF THE FIRST PUBLIC CLASS


    Java Code:
    Public class A  {
    Private B b;
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    play();  // starts the soundblock thread
    	}
    
    
    
    private play()  {  // the method that creates the thread object
    	Play p = new Play();
    	p.start();
    }
    
    
    Private class Play extends Thread  {
    
    	Public void run()  {
    
    		While (true)  {    // keeps the soundblock thread going
    
    				synchronized (b.getLockObject() ){   		
    					try{				
    					wait(); // <-----  let the current thread wait instead	
    					}catch(InterruptedException e){
    					System.out.println(e);
    					}
    				}
    		soundblock(); 
    		}
         }
         
                  
    	}
    }  // end of play thread class
    
    
    private void soundblock()  {}  // here is all sound midi data
    
    }  //and of first public class
    
    
    
    
    Public class B  extends JPanel {   // all the GUI stuff of the app.
    
    Boolean flg;
    private Object lock = new Object();  // <------- object for common use
    
    public B()   {	// constructor notifies thread in above class?
    			for (int count = 0; count < 900000; count++) ;
    			synchronized (getLockObject()) {  // <------------ use the common lock object
    			notify(); //  I am not sure if this notifies the above thread.... what a mess		
    		  }	
    
    }
    *
    	public Object getLockObject(){       // <----- to share the lock
    		return lock;
    	}
    
    	
    
    } // end of second public class

  19. #59
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default

    Please do not change the general design of the two public classes A and B.
    I would like to do a backwards notifying
    I will look again over your second-wait-loop code norm... but I should say it is a bit confusing (I did look over it a lot).

    Of course I could say p.wait() in the above, BUT I CAN NOT SAY p.notify() to point to that specific thread. But I taught by synchronizing to the same object that might work.

    I might have to look into C++ ?

  20. #60
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    getLockObject().wait(); // <----- let the current thread wait instead
    getLockObject().notify(); // I am not sure if this notifies the above thread.... what a mess
    SEE my previous post ABOVE!!!! wait and notify MUST use the SAME object!!!
    Last edited by Norm; 09-12-2008 at 02:23 PM.

Page 3 of 6 FirstFirst 12345 ... LastLast

Similar Threads

  1. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 09:06 PM
  2. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 09:30 PM
  3. waiting for a file
    By Fleur in forum New To Java
    Replies: 2
    Last Post: 06-23-2008, 08:18 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •