Results 41 to 60 of 116
- 09-11-2008, 12:21 AM #41
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
- 09-11-2008, 02:43 AM #42
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.
- 09-11-2008, 08:58 AM #43
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
Let´s 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
- 09-11-2008, 09:18 AM #44
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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
- 09-11-2008, 09:19 AM #45
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
this monitor is actually the object with the lock?
- 09-11-2008, 04:16 PM #46
Yes. You must use the same object for the synchronize, wait and notify. I named that single object: monitor
- 09-11-2008, 07:35 PM #47
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
- 09-12-2008, 12:19 AM #48
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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.."); } }
- 09-12-2008, 12:26 AM #49
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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?
- 09-12-2008, 12:47 AM #50
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
lock.wait(); // <--- you want the lock to wait?
wait(); // <----- let the current thread wait instead
what is the differences (lock is an Object...)
- 09-12-2008, 02:57 AM #51You must use the same object for the synchronize, wait and notify
Look at my code. Try modifying it to fit in with your codelock.wait(); // <--- you want the lock to wait?
wait(); // <----- let the current thread wait instead
what is the differences (lock is an 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.
- 09-12-2008, 08:52 AM #52
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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?
- 09-12-2008, 09:09 AM #53
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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
- 09-12-2008, 09:15 AM #54
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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 } }
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!
- 09-12-2008, 09:18 AM #55
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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()
- 09-12-2008, 09:59 AM #56
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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
- 09-12-2008, 10:01 AM #57
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
IMPORTANT THE THIRD CODE LINE SHOULD BE OUT
private Object lock = new Object(); // <------- object for common use
- 09-12-2008, 10:08 AM #58
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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
- 09-12-2008, 10:30 AM #59
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 1,139
- Rep Power
- 15
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++ ?
- 09-12-2008, 02:06 PM #60
Similar Threads
-
passing a value from parent thread to child thread
By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM -
Waiting for a button to be pressed
By SomeGuyOverThere in forum New To JavaReplies: 6Last Post: 08-21-2008, 09:30 PM -
waiting for a file
By Fleur in forum New To JavaReplies: 2Last Post: 06-23-2008, 08:18 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM
Bookmarks