View Single Post
  #18 (permalink)  
Old 09-04-2008, 03:24 AM
Nicholas Jordan's Avatar
Nicholas Jordan Nicholas Jordan is offline
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 879
Nicholas Jordan is on a distinguished road
do{}while(); is not wait() / notifiy()
Quote:
Originally Posted by willemjav View Post
(Or how to get into one and, most importantly, how to get out of a one.)
Really simple:
Code:
class Music implements List of Some Kind{ List notes; Position position; Boolean playing; Music(){ // ....? maybe set music type or something } public void nextNote() { ++position; } } // // class Controller extends GUI / AWT / Graphical { Button goButton; Button stopButton; Boolean ExitProgram; // Actually, you do not need this // MasterClock mc; // Controller(MasterClock mc){ this.mc = mc; } public void ButtonHandler(ButtonPush bp) { if(bp == goButtonPush) { mc.startPlay(); } if(bp == stopButtonPush) { mc.stopPlay(); } } public void stopPlay() { mc.exit(); } } class MasterClock implements Runnable { // private Ticker;// private List waits; // Boolean running = new Boolean(true); // public void run { while(running.BooleanValue()) { // if(++Ticker > NextTimeToMoveOnList) { Music.nextNote(); } // Looks funny, but is closer to correct practice. sleep(0); } } public void startPlay() { running = new Boolean(true); } public void stopPlay() { running = new Boolean(false); } public void exit()// Not use yet.... }
Quote:
Originally Posted by willemjav View Post
I haven’t seen any better alternative to my previous loop constructions!
You have now, though this has considerable detail left out.
Quote:
Originally Posted by willemjav View Post
Maybe because loops form the very hearth of computers and there functioning.
Basically, this is close to technically correct.
Quote:
Originally Posted by willemjav View Post
Almost everything, concerning a computer, is loop cycle.
Yes.
Quote:
Originally Posted by willemjav View Post
A program is one the processor cycle is one, events are loops the poor thing has actually noting better to do than spinning its loops.
Correct.
Quote:
Originally Posted by willemjav View Post
(I know, now that nick is reading carefully - he thinks that I am joking, but I am not, this looping bUssiness is very serious to me so he will surprise me with another mysterious code which should put my thinking into a loop.
I realize you are not joking, and yes it is funny to me but try not to loop you thinking.
Quote:
Originally Posted by willemjav View Post
(because he is actually the joker of our forum).
Did you see my post about UFO's in Oklahoma?
Quote:
Originally Posted by willemjav View Post
This new loop waits for the sound tracks to start and is note at the GUI class where buttons etc. are pushed. It sits at the class where all the sounblocks are. The loop happens, again, inside a thread (I start to like threads)
Just replace the System.out.prints with Music.play(note)
Quote:
Originally Posted by willemjav View Post
The loop will keep the thread busy spinning until the method call of the GUI class turns true. Than the flag gets reset and the important mainSoundblock(1); method starts WITHIN THE THREAD.
Method call of GUI class is a method call of an instance of MusicPlayer. You have two runs, one is runs music - which runs along a List - iow Plays Music. The other is the system thread with is the gui and so on. You do not program that thread, the window controls take care of that for you. If you want to be able to start and stop your music, you have to have a Thread trapped in a while, which perforce must do a sleep or yield. Everyone codes it like Norm has it. Wait and NotifiyAll have performance issuse that are not important for low data-rate such as midi on one Mac. If that approach makes more sense to you, do it.
Quote:
Originally Posted by willemjav View Post
Consider the working of this method similar to a pin-ball machine. The little ball starts at the very top but eventually will reach the bottom. The if’s, when turn false, will push the ball hard (the esc key was pressed). Each soundblock takes from 20 sec. to several minutes (depending on midi pedals, remember). When reaching the end the thread is over and the cycle/ thread repeats by pushing the start button.
There are two ways to flip the pinball. One is with a Thread.new() the other is to release a boolean gate on button push, un-gatting a Thread trapped in a do / while. The problem with a do / while is that if the thread is not daemon, we get sluggish exits so what I have decided to do is construct all objects in main() setting daemon == true and calling start on the clock class. GUI class has a reference to the Clock, which does a check on boolean continue running each time around.
Code:
private void mainSoundblock(int trck) { if((trck > minValue)&&(trck < maxValue))this.settrackNumb(trck); else this.settrackNumb(defalutTrackNumber); }
Giant switches can usually be reduced to a simple variable, check for continue running is moved to MasterClock.
Quote:
Originally Posted by willemjav View Post
By the way PK, when coming out of the loop and NOT coming out of the thread, all the buttons, besides the escape, are turned off (xxx.setEnabled(false) so that no other thread, besides the midipedal and escape, can disturb the pin ball.
Correct.
Quote:
Originally Posted by willemjav View Post
this funny head appeared by itself?
Those smileys are actually certain character combinations such as semicolon/right round brace.
Quote:
Originally Posted by willemjav View Post
Location: Spain
I thought you were in Netherlands.....
Quote:
Originally Posted by willemjav View Post
Yes norm, we might get to the center of this problem (maybe a lack of java imagination of mine). Java does not support multiple inheritance, right. I read some stuff about that, and the point I got out of that all, is that they do not want to turn java into the spaggetti-code mess of C++.
I would place a joke here but those folks watch too many spaggetti-westerns.
Quote:
Originally Posted by willemjav View Post
I started out with java and do not know any better, good! (I choose for java, because I do not feel any sympathy for the micro-soft (Chicago)software-boys
Ditto.
Quote:
Originally Posted by willemjav View Post
if you want to know more about that, read Naomi Klein’s latest, The Shock Doctrine (yes, nick I believe we should show our code-biting-juniors about the many hidden background stories of this world).
Meek, and media-compliant service to normalacy - nothing more. Chicago packs Iron, Naomi doesn't.
Quote:
Originally Posted by willemjav View Post
Okay let’s follow the thread, when the Main class on top of the other classes creates an instance of the second Controler class, that has all the GUI stuff, THERE IS A ONE WAY DIRECTION OF INFORMATION, right.
GUI -> Controller, clock just slips out of loop if trap variable is false.
Quote:
Originally Posted by willemjav View Post
When the instance of the GUI-class, called count, created at the Main-class (where all the soundblocks are, remember) wants to get data from the GUI-class it gets it by cont.whatever(); If I create a thread at one of the event-notifying methods (of the GUI-class) I WOULD BE GOING INTO THE OPPOSITE DIRECTION.
Move all driver code to the controller class. Main just construct a few objects and call start on them. That is why Swing has it's own launcher, called javaw.
Quote:
Originally Posted by willemjav View Post
Where is the instance of the Waitloop class created?
Code:
public static void main
You do not have a class named
Code:
Waitloop
Quote:
Originally Posted by willemjav View Post
The return When I Can Execute method would loop/wait until the event occured when you want the mainSoundblock method to be called. This would simplify the design by not requiring the run method to know about setting and clearing flags in the cont class.
Events, ala AWT / Swing, set variables that are the boolean that Trap the Threads. Wait() and Notifiy are the same thing, except they are written by Engineers for people who think that way.
Quote:
Originally Posted by willemjav View Post
Finally, I have the the program working. I have tested the application many times and everything works just fine. I have to take care of some æsthetical things.... and done. Probably the underlaying mistake of the program is that I did not put the GUI stuff into the top class. I have two waitloops taking care of the GUI management, and one in a new created thread.
Timer / TimerTask if(nextClock)doSomething();yield();// runs at 1khz
Code:
do{music}while(run);// wait for start button
No wait states, just yield or sleep if no work to do....machine is vastly faster than what you think it is.
Quote:
Originally Posted by willemjav View Post
Sure next programming project I should design differently and I will try to get rid of those un-elegant cpu consuming loops.
Prove they consume cpu cycles needlessly, do not go nuts trying to do that.
Quote:
Originally Posted by willemjav View Post
1) when appending text into a JTextArea, how could I keep the new text on sight at the screen (the new appended text scrolls down and disappears)?
JTextArea.setText(" ...... ");
Quote:
Originally Posted by willemjav View Post
2) Before going out of the application, at the very last moment, I would like to clean up some things (clossing drivers etc.). I am using the standard
Code:
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
put all that in finally of try / catch block, which is entered when do / while is released by setting run boolean to false or better yet per Norm "Use a Window Listener to catch window closing and put a call to your cleanup code there."
Quote:
Originally Posted by willemjav View Post
There is something I am not getting here!
Vestigages of earlier work, you are moving along.
Quote:
Originally Posted by willemjav View Post
There happens an event in class B (at any time!!!) how this can be noted in A???
Code:
public static void main(String[] args) { A a = new A();// B b = new B(a);// // Or .... B b = new B(new A());// b.start(); }
Quote:
Originally Posted by willemjav View Post
At class A I could ask for b.getMidiped() and it will return the correct true or false of the var. But I do not know when that will happen from inside the code of class a so that is why I need a loop to wait this to happen......... Or not???
One way data-flow, as discussed earlier.
Code:
public static void main(String[] args) { do { ;// }while(boolean run); }
Norm's code is a classic intro to threads, Wait is not a class, has do{}while(); stuffed in somewhere other than where the wait needs to happen.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Reply With Quote