Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Threads?

Thread view: 
S - 08 Dec 2005 12:40 GMT
I have no clue about threads, but imagine it would be a good thing to
use in the following scenario:
I have a JApplet which makes a SWT-GUI, a class that extends JComponent
(for drawing a figure on a JCompoent inside the SWT-GUI) and a
Calculation.class that does all the calculations.

When a button is pushed I want to:
1. Calculate the right data via the Calculate.class
2. _Wait untill all the calculations are saved and updated
3. Paint the new JComponent from the JComponentMy.class with the NEW
values from Calculate.class

I have tried to find some tutorials about Threads, and how to implement
them, but have not succeeded. Is there an easy way to wait untill a
call is finished before continuing?

Thanks in advantage
Viator - 08 Dec 2005 13:07 GMT
I think you want to do a long operation on the click of the button and
still want the GUI to be responsive. Am I right?

Amit :-)
S - 08 Dec 2005 13:13 GMT
I want to do a lont operation on the click of the button, then draw the
JComponent with the new values. The problem now (I think) is that the
Calculation is not finished before the JComponent starts drawing -
resulting in error drawing the values.
Viator - 08 Dec 2005 15:12 GMT
The components are drawn in the event dispatch thread - the same thread
that handles the button click event. So if you do your calculation in
the same thread the component will not be drawn before the calculation
is over, but the GUI will not be responsive - it might well go blank
-  for that period of time. If you want the GUI to be responsive during
that period then you have to do the calculation in a separate thread
and then draw the values. For the period of time that calculation ids
being done the values in the component will be invalid. For that you
can show a message to the user like "please wait while processing...".
This is a standard practice to freeze certain parts of GUI during long
operations - like the button that submits the calculation for the
period of the operation.

Regards,
Amit
S - 08 Dec 2005 15:52 GMT
The following code is in the actionPerformed when pushing the button:

public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Start")){
this.jTextAreaCommandLine.setText(this.jTextAreaCommandLine.getText()+"\nStarted");
            this.run = true;
            while (run) {
                this.nextGen(); //Updates all the necessary datas in the
Calculations - THIS have to be finished before going to the next point
                jComponentRobotArm1.paintREAL(this.getGraphics()); // This will
(hopefully) paint the JComponent with the new values - so it have to be
started AFTER this.nextGen() is finished
                this.jButtonStartPause.setText("Pause");
            }
               }
}

So how can I make a new thread inside this method, so it finnishes
before the jComponentRobotArm1.paintREAL(this.getGraphics()) is called?

Thanks for your fast reply
zero - 08 Dec 2005 16:57 GMT
> The following code is in the actionPerformed when pushing the button:
>
[quoted text clipped - 21 lines]
>
> Thanks for your fast reply

As your code is now, it will do what you want: nextGen() will finish
before paintREAL() is called.  If you're not convinced, test this by
printing some output to System.err:

public void nextGen()
{
  System.err.println("nextGen starting"); // add this line

  // your normal code goes here

  System.err.println("nextGen ended"); // add this line
}

public void actionPerformed(ActionEvent e)
{
  // normal code

  while(run)
  {
     this.nextGen();

     System.err.println("Start painting"); // add this line

     // normal code
  }
}

For applets under windows you can check this output by right-clicking  
the java icon on the tray, and selecting "open console".

Signature

Beware the False Authority Syndrome

oulan bator - 08 Dec 2005 17:21 GMT
something like this should work !

public void actionPerformed(ActionEvent e) {
               if (e.getActionCommand().equals("Start")){
this.jTextAreaCommandLine.setText(this.jTextAreaCommandLine.getText()+"\nStarted");
//here add it: it launches a new thread to perform your computation and
the refresh
new Thread(new Runnable() {public void run() {
// end
this.run = true;
                       while (run) {
                               this.nextGen(); //Updates all the
necessary datas in the
Calculations - THIS have to be finished before going to the next point

jComponentRobotArm1.paintREAL(this.getGraphics()); // This will
//here again
}}).start();
//end 'till there, will be executed

(hopefully) paint the JComponent with the new values - so it have to be
started AFTER this.nextGen() is finished

this.jButtonStartPause.setText("Pause");
                       }
               }

}
Thomas Hawtin - 08 Dec 2005 20:00 GMT
> I want to do a lont operation on the click of the button, then draw the
> JComponent with the new values. The problem now (I think) is that the
> Calculation is not finished before the JComponent starts drawing -
> resulting in error drawing the values.

It sounds as if at some point need to do some copying. You probably
don't want to manipulate the same objects from two different threads.

Either paint with a copy of the results. Or clone the state within the
EDT, work on your private copy of the data in your own thread, and then
copy back in the EDT within EventQueue.invokeLater.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Mike - 09 Dec 2005 15:09 GMT
I would first do some simple things that can use threads:
http://www.google.com/search?q=java+threading+example
http://www.javaworld.com/jw-09-1998/jw-09-threads.html

the Javaworld threading example is kinda old, but the syntax and ideas
haven't changed (don't worry about platform independence... that hasn't
been too much of an issue with recent operating systems..)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.