Home arrow static arrow Java Programming [Archive] - Simple thread question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Simple thread question
This topic has 2 replies on 1 page.

Posts:26
Registered: 6/6/04
Simple thread question  
Jul 9, 2004 1:58 AM



 
Hi everybody,i have this thread problem(i'm new to java and h
ave no idea what exactly to do).
Well,in my applet i have this action performed method
that i imagine is in the event dispatch thread.
This method calls as u see, two other methods of a class named CPU(.
Note that fetch and execute are observed classes and my applet class
is the observer.
The problem is that during the execution of fetch() and execute(),
inside these methods change some things and so they call notifyObservers(Object arg)
passing to the applet the argument and so the applet updates some text components.
But as you can immagine there is no time for the gui to update himself so i see nothing.
So, my question is which of the methods need to be in a separate thread so to ensure
visible results(component updating)?
Perhaps,the update method needs to be invokedLater with SwingUtilities?
Please give me an example code if possible.

Here is the actionPerformed code(from the applet class) and the fetch method:
    void executeProgramButton_actionPerformed(ActionEvent e) {        int numInstr = this.machine.ram.segmentSize;        for (int i = 0; i < numInstr; i = i + 4) {            machine.cpu.fetch();            machine.cpu.execute();        }    }/*The following method is in the CPU classfetch method:here the observedPC is the observable value that notifies the observer(the main applet) that does: pcTextField.setText(arg.toString());*/    public void fetchInstructionProgram() {        observedPC = Integer.tothe String(Pc);        setChanged();        notifyObservers(observedPC);        instructions++;        readOperation(cache.instructionfetch);        pc = pc + 4;     } 


Thank you very much
 

Posts:7,499
Registered: 02-11-14
Re: Simple thread question  
Jul 9, 2004 2:28 AM (reply 1 of 2)



 
One way could be to put the content of the action performed method into a new thread, then use invokeLater in your Observers that cause GUI updates:
    void executeProgramButton_actionPerformed(ActionEvent e) {      new Thread()       {        public void run()        {          int numInstr = this.machine.ram.segmentSize;          for (int i = 0; i < numInstr; i = i + 4)           {            machine.cpu.fetch();            machine.cpu.execute();          }        }      }.start();    } // In your Observer    public void update(Observable o, Object arg)    {      SwingUtilities.invokeLater(new Runnable() {        public void run()        {  /* your GUI afecting code */ }      });


Or something like that.

You might need to watch for synchronizing things
 

Posts:26
Registered: 6/6/04
Re: Simple thread question  
Jul 9, 2004 8:24 AM (reply 2 of 2)



 
Thank you very much.
Exactly what i needed...everything's ok

:-)

thx,thx
 
This topic has 2 replies on 1 page.