Page 4 of 6 FirstFirst ... 23456 LastLast
Results 61 to 80 of 116
  1. #61
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking cat on a hot tin roof

    Quote Originally Posted by willemjav View Post
    Please do not change the general design of the two public classes
    Далас (па-ангельску: Dallas) — горад у ЗША, разьмешчаны ў паўночна-ўсходняй частцы штата Тэхас на рацэ Трыніці. Адміністрацыйны цэнтар графства Далас. Разам з Форт-Уэртам і іншымі гарадамі агламэрацыі (Арлінгтан, Гарланд, Дэнтан, Ірвінг, Мэскіт і Плэйна) Далас складае мэгаполіс («metroplex») «Далас—Форт-Уорт». Далас зьяўляецца 3-м па колькасьці насельніцтва горадам Тэхаса і 9-м у ЗША (1 213 825 чалавек па зьвестках на 2005 год); сам мэгаполіс «Далас—Форт-Уорт» зьяўляецца 5-м па колькасці насельніцтва ў ЗША (5,7 млн. чал.).
    Quote Originally Posted by willemjav View Post
    I might have to look into C++ ?
    Jump out of the fryin pan and into the fire, you ain't seen nothin yet. C++ took down the enitre nation's phone system. What you are doing is a backwards notifiy, what you need is easily done with wait and notifiy, but I do not use them because it leads to this discussion - data hiding and KISS-OFF are established design paradigm arising from a rich history of difficulties such as we see here.
    Java Code:
    class Second{}
    class First{First(Second s){}}
    Class Second now may be operated by Class First, but we have to new somewhere - I can tell you without looking at either of your code that Norm's code will be fundamenatlly correct from a systems design point of view. You are looking at the code as being the code, there are several compilation steps from viewable code to machine operation.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #62
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Nick,
    Do you think we could get Willemjav to fly someone to Spain for a weeks tutoring in java?
    Willemjav,
    You've said in the past you were going to take a break and go to the beach. Where are you located?
    Norm

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

    Arrow The this pointer

    I think what we need more than tutoring is a few months as a card-puller, it would all be over in a hurry then. What willemjav needs to get a grasp on is actually the this pointer, but I do not want to do that until he can envision a basic call stack in machine opcodes. What he is trying to do is get a reference back to the object that is waiting so he can notifiy the controller that worker thread is done. This is traditionally thought of and written as "Barrirer" in Java, of which willemjav has already advised by ftr - thus the dispatching of tutors to Spain would likely just blow holes in the tropopause....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

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

    Default

    Look, I underestimated threads....... they are complicated and stuff is not explained very well concerning this subject!

    But there is some hoop (besides preparing classes, piano playing, composing, going to the beach) I did made some process!!

    Ignore the previous mess (It just expressed my internal confusion).

    Next code, I got to work (just a beginning, so do not give up on me yet)


    Java Code:
    package waitthread;
    
    
    
    public class A  {
    private B b;
    
    
    	
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    play();  // starts the soundblock thread
                    b.note();
                   
    	}
    
    
    private void play()  {  // the method that creates the thread object
    	Play p = new Play();
    	p.start();
            
    }
    
    
    private class Play extends Thread  {
    
    	public void run()  {
                    System.out.println("enters in the run method");
    		
                        synchronized (b.getLockObject()){ 		
    			try{				
                              b.getLockObject().wait(); 	
    			}
                            catch(InterruptedException e){
                                System.out.println(e);
                            }
                        }
    		soundblock();
                    System.out.println("exit thread");
            }
    }  // end of play thread class
    
    
    private void soundblock()  {
        System.out.println("soundblock started");
    }  // here is all sound midi data
    
    public static void main(String[] args) {
            new A();
    }
    
    }  //and of first public class
    
    
    //=============================
    
    package waitthread;
    
    import java.io.IOException;
    import javax.swing.JPanel;
    
    
    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()   {
    		
                  System.out.println("constructor of B loaded");
    }
    
    	public Object getLockObject(){       // share the lock
    		return lock;
    	}
    
            public Boolean getflg()  {   // starts the soundblock
                return flg;     
            }
            
            public void note()  {
            try {
                do {
                int       d = System.in.read();
                System.out.println( "having read " + (char)d);
                }
                while(System.in.available()>0);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    		synchronized (getLockObject()) {  //use the common lock object
    				getLockObject().notify(); 	
    	        }
            }
    
    } // end of second public class

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

    Default

    the c++ remark moved you guys!

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

    Default

    So now than the c++-or-java-question:

    I got rid of looping
    and have a backwards notifying from
    the B class towards the A class
    the thread starts and by hitting any key
    done in the A class
    the soundblock starts
    and after finishing that
    the thread stops.

    So far so good!!!

    BUT I NEED TO REPEAT THE PROCESS
    AND AFTER EXITTING THE THREAD
    CREATE A NEW ONE
    UNTILL THE USER STOPS THE PROGRAM


    (that should be some kind of loop
    avoid a recursive one)

  7. #67
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    thread starts and by hitting any key
    done in the A class
    the soundblock starts
    and after finishing that
    the thread stops.
    Is it all driven/controlled by the hitting of a key?
    NEED TO REPEAT THE PROCESS
    that should be some kind of loop
    Would the user hitting a key start the procces again?
    No need for a program loop. The user takes care of starting the next process by hitting a key.

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

    Default

    remember the events are at the B class and the things to happen at the A-class (if it would be the other way around there is no problem and your concept goes).
    So for the backwards notifying I will need some kind of loop with a flag.

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

    Arrow use barrier, more classes needed

    Quote Originally Posted by willemjav View Post
    Look, I underestimated threads.......
    Nope, not yet.
    Quote Originally Posted by willemjav View Post
    they are complicated and stuff is not explained very well concerning this subject!
    They are not complicated, it is the creative v mechanic - if threads were explained correctly, we would have a full orchestral symphony available.
    Quote Originally Posted by willemjav View Post
    I did made some process!!
    The work shows definite progress, it will be better to take a light approach for awhile so we do not risk frustrations.
    Quote Originally Posted by willemjav View Post
    Next code, I got to work (just a beginning, so do not give up on me yet)
    Work remains. I had to obliterate Class A as it has main() and soundblock and run() all in the same class, get and release lock object have to be collapsed into the same synchronized function call - this code here is still fundamentally flawed but uses what you have instead of trying to do too much at once:
    Java Code:
    package demo;
    // all the GUI stuff of the app.
    public class B  extends JPanel
    {
        // Actually, volatile is not implemented correctly.
        private volatile boolean flag;
        private Object lock = new Object();  // object for common use
        //
        public B(){super();}
        /**
        * This is actually a critically flawed lock aquisition routine,
        * and if studied in detail will illustrate the core of the 
        * double-checked locking is broken problem. True 'realtime'
        * requires dedicated hardware and code,....
        */
        // There is no getLockObject() in java.lang.Object
        public synchronized boolean getLockObject()
        {
            if(flag){do{this.sleep(0)}while(flag);flag=true;return true;}
            else{flag=true;return true;}
        }
        // Releasing of shared resources ..... uh, Houston - we have a problem.
        public synchronized boolean releaseLockObject()
        {
            if(flag){do{this.sleep(0)}while(flag);return true;}
            else{flag=false;return true;}
        }
        // starts the soundblock
        public void startSoundblock()
        {
            this.play();
            // This is the callback you have been trying to achieve.
            A.notify();   
        }
        private void play()
        {
            nextStave.play();
        }
        //probably should be moved to some sort of music class or sequence. 
        public void note()  {
        ...
        }
        public static void main(String[] args)
        {
            B b = new B();//
        }
    }
    // end of second public class
    Quote Originally Posted by willemjav View Post
    the c++ remark moved you guys!
    Well, yes - because we would have lost you.
    Quote Originally Posted by willemjav View Post
    So now than the c++-or-java-question
    Java has built in Threading concepts, going to C++ would be like baiting catfish with something that has not sunk to the bottom: You would get no takers.
    Quote Originally Posted by willemjav View Post
    (that should be some kind of loop avoid a recursive one)
    Norm's advice reflects the same reason systems programmers avoid application programming. For one machine, one performance, we do it like Norm says. Otherwise we get what ftr advised yesterday in next step
    Quote Originally Posted by willemjav View Post
    remember the events are at the B class and the things to happen at the A-class.
    The A-Class is Player class, B is a JPanel as you have it and also the main() class - pass notifications to A using the Event model of JPanel, setting variables in A NOT by calling into A's methods from the Event in B (!!!). If you do a.play() from B.Event - you have a single threaded program. This is core work, I can slap the code up in a hurry but we overlook the double-checked-locking-is-broken thus introducing unforseen failure.
    Quote Originally Posted by willemjav View Post
    So for the backwards notifying I will need some kind of loop with a flag.
    That is the Barrier sample code which ftr already directed.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #70
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    events are at the B class and the things to happen at the A-class
    When there is an event in the B class, it should call the A class to do things.
    The A class should only respond to a direction from the B class. The A class should not be asking the B class for direction. It should just do what B tells it to do and then exit.

    What things does A need to tell B? Perhaps B should disable some of its GUI controls while A is doing things to allow A to complete before B tells it to do something.
    Last edited by Norm; 09-13-2008 at 06:40 PM.

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

    Exclamation Barrier class sample code

    Quote Originally Posted by Norm View Post
    ....Perhaps B should disable some of its GUI controls while A is doing things to allow A to complete before B tells it to do something.
    willemjav, this is the classic use of Barrier - which is an encoding of the callback you are trying to accomplish. It will salvage some bandwidth if you will sink into this for a couple of days.....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

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

    Default

    I am thinking how to start the thread again without using an ugly while/ do loop.

    not easy

  13. #73
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Start the thread when the user does something like press a button.
    What thread are you talking about? The one that is to do the work or ?

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

    Default

    Almost there, note the red remark, there should come a simultanious notifying.... what you think, a good idea? (doing things upside down is very instructive, that is way I wont change the design).



    /*
    * A.java
    *
    * Created on September 12, 2008, 11:59 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package waitthread;

    /**
    *
    * @author willemjav
    */

    public class A {
    private B b;
    private Play p;
    private int threadcount=0;




    A() { // constructor creates an object of the GUI class B
    b = new B();
    repeatcycle();

    }


    private void play() { // the method that creates the thread object

    p = new Play();
    p.start();


    }

    private void repeatcycle() { // the wait loop
    while(true) {
    play();
    b.noteThread();
    while (p.isAlive()) {
    // here should come a wait of main thread
    // notify by p thread

    }

    }
    }

    private class Play extends Thread { // the thread that plays the soundblocks

    Play() {
    threadcount++;
    }

    public void run() {

    System.out.println("entering in thread " + threadcount);

    synchronized (b.getLockObject()){
    try{
    b.getLockObject().wait();
    }
    catch(InterruptedException e){
    System.out.println(e);
    }
    }
    soundblock();
    // this.notify();
    System.out.println("exit thread " + threadcount);

    }
    } // end of play thread class


    private void soundblock() {
    System.out.println("soundblock started");
    } // here is all sound midi data



    public static void main(String[] args) {
    new A();
    }

    } //and of first public class



    //==============================

    /*
    * B.java
    *
    * Created on September 13, 2008, 12:00 AM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package waitthread;

    import java.io.IOException;
    import javax.swing.JPanel;

    /**
    *
    * @author willemjav
    */
    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() {

    System.out.println("constructor of B loaded");
    }

    public Object getLockObject(){ // share the lock
    return lock;
    }

    public Boolean getflg() { // starts the soundblock
    return flg;
    }

    public void noteThread() {
    try {
    do {
    int d = System.in.read();
    System.out.println( "having read " + (char)d);
    }
    while(System.in.available()>0);
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    synchronized (getLockObject()) { //use the common lock object
    getLockObject().notify();
    }
    }

    } // end of second public class

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

    Default

    forgot the code tag


    Java Code:
    /*
     * A.java
     *
     * Created on September 12, 2008, 11:59 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package waitthread;
    
    /**
     *
     * @author willemjav
     */
    
    public class A  {
    private B b;
    private Play p;
    private int threadcount=0;
    
    
    	
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    repeatcycle();
                   
    	}
    
    
    private void play()  {  // the method that creates the thread object
       
    	  p = new Play();
    	  p.start();
               
            
    }
    
    private void repeatcycle() {        // the wait loop
        while(true)  {
             play();  
             b.noteThread();
             while (p.isAlive())  {
                 // here should come a wait of main thread
                 // notify by p thread
             }
                    
        }
    }
    
    private class Play extends Thread  {        // the thread that plays the soundblocks
        
        Play()  {
        threadcount++;
        }
    
    	public void run()  {
                    
                    System.out.println("entering in thread " + threadcount);
    		
                        synchronized (b.getLockObject()){ 		
    			try{				
                              b.getLockObject().wait(); 	
    			}
                            catch(InterruptedException e){
                                System.out.println(e);
                            }
                        }
                        soundblock();
                       // this.notify();
                    System.out.println("exit thread " + threadcount);
                  
            }
    }  // end of play thread class
    
    
    private void soundblock()  {
        System.out.println("soundblock started");
    }  // here is all sound midi data
    
    public static void main(String[] args) {
            new A();
    }
    
    }  //and of first public class
    
    
    //========================
    
    /*
     * A.java
     *
     * Created on September 12, 2008, 11:59 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package waitthread;
    
    /**
     *
     * @author willemjav
     */
    
    public class A  {
    private B b;
    private Play p;
    private int threadcount=0;
    
    
    	
    
    	A()  {  // constructor creates an object of the GUI class B
    		b = new B();
                    repeatcycle();
                   
    	}
    
    
    private void play()  {  // the method that creates the thread object
       
    	  p = new Play();
    	  p.start();
               
            
    }
    
    private void repeatcycle() {        // the wait loop
        while(true)  {
             play();  
             b.noteThread();
             while (p.isAlive())  {
                 // here should come a wait of main thread
                 // notify by p thread
             }
                    
        }
    }
    
    private class Play extends Thread  {        // the thread that plays the soundblocks
        
        Play()  {
        threadcount++;
        }
    
    	public void run()  {
                    
                    System.out.println("entering in thread " + threadcount);
    		
                        synchronized (b.getLockObject()){ 		
    			try{				
                              b.getLockObject().wait(); 	
    			}
                            catch(InterruptedException e){
                                System.out.println(e);
                            }
                        }
                        soundblock();
                       // this.notify();
                    System.out.println("exit thread " + threadcount);
                  
            }
    }  // end of play thread class
    
    
    private void soundblock()  {
        System.out.println("soundblock started");
    }  // here is all sound midi data
    
    public static void main(String[] args) {
            new A();
    }
    
    }  //and of first public class
    
    
    
    /*
     * B.java
     *
     * Created on September 13, 2008, 12:00 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package waitthread;
    
    import java.io.IOException;
    import javax.swing.JPanel;
    
    /**
     *
     * @author willemjav
     */
    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()   {
    		
                  System.out.println("constructor of B loaded");
    }
    
    	public Object getLockObject(){       // share the lock
    		return lock;
    	}
    
            public Boolean getflg()  {   // starts the soundblock
                return flg;     
            }
            
            public void noteThread()  {
            try {
                do {
                int       d = System.in.read();
                System.out.println( "having read " + (char)d);
                }
                while(System.in.available()>0);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    			synchronized (getLockObject()) {  //use the common lock object
    				getLockObject().notify(); 		
    			}
            }
    
    } // end of second public class

  16. #76
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I still do not understand why you need a loop. What would happen to the program if you did NOT have a loop?

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

    Angry that's what barrier class is!.....

    Quote Originally Posted by willemjav View Post
    I am thinking how to start the thread again without using an ugly while/ do loop.
    That's what barrier class is, spare us - it is all over the web. Your entire do{}while(); disappears. the wait() in the barrier does the backwards notifiy.

    Second time, in case you did not read the above:

    Barrier causes the backwards notifiy from at the end of the do{}while(); to disappear completely from the entire codebase.

    Third time, in case you did not read the above:

    BARRIER CAUSES THE BACKWARDS NOTIFIY FROM AT THE END OF THE DO{}WHILE(); TO DISAPPEAR COMPLETELY FROM THE ENTIRE CODEBASE!!!!!.....

    What I did in my work is to emplace a soft-spin on an entire collection of Threads, it was so simple. It makes this look vanishingly small - running some 800 lines or so.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

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

    Default

    When the A constructor calls only

    play();
    b.noteThread();

    play, will start the thread and put it to sleep rigth away
    b.noteThread(), will wake the thread after some action (start pushed in class B) and the soundblocks are performed, and the program exits

    In order to repeat this process (from-out class A) each time the soundblock end, the created thread terminates, and a new thread is created until the user exits the program completely. The code on the bottom part does just that but again creates a spinning loop! So I need an other wait/ notify to get the main thread (of the repeatcycle) to sleep and wake it by the p.thread of the start bottom.

    SURE WHEN SWAPPING THE A AND B CLASS AND HAVING THE GUI IN THE TOP CLASS, ALL WOULD BE A PIECE-OF-CAKE! (I learn best by doing things upside down, instead of just copying working code fragments).


    Java Code:
    private void repeatcycle() {        // the wait loop
        while(true)  {
             play();  
             b.noteThread();
             while (p.isAlive())  {
                 // here should come a wait() of main thread
                 // notify() by p thread
             }
                    
        }
    }

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

    Default ariadne the red line and the minotaurus

    To start a thread is no big deal you e.g. create a class which extends Thread and situate a run() method in that class, which contains all code of the thread. Next you create an object of that class and start() the thread.
    My first question was, how to stop a thread? Answer: when the code exists the run() method the threads ends (for ever! In case you want to use the thread again you have to create it new).

    When you want threads to share data you need to synchronize things and when one threads needs to consume data of an other thread you can have that thread wait() for that data and notify() the thread when the data is available. So far everything is pretty straightforward.

    The confusing part for me is when you synchronize or wait/ notify (wait/ notify always needs to be synchronized), WHERE YOU NEED TO synchronize/ wait/ notify ON?
    1) the shared data
    2) the specific thread
    3) an Object (concerning the API)
    What is an OBJECT?
    “CLASS was, is, and always will be,
    CLASS created Object,
    and from here on everything else descents
    (apple, pc etc.)!”
    -For instances once an object is created should one synchronize/ wait/ notify always on that same object. This makes sense (I saw something similar in an other forum)?
    Java Code:
    synchronized (adam){ 		
    			try{				
                              eve.wait(); 	
    			}
                            catch(InterruptedException e){
                                System.out.println(e);
                            }
    }
    -this.wait() inside a run(), puts that current thread to sleep?
    -if so, how would you notify that specific thread only to wakeup?
    -why would you synchronize/ wait/ notify on the shared data (which is produced by one thread and consumed by the other)?

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

    Default

    Now I understand (some), I confused Object with object
    But anyway:
    an object of any class is always a member of the class Object
    (I looks like a joke but it isn´t)

Page 4 of 6 FirstFirst ... 23456 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
  •