Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Threads?

 
Thread Tools Search this Thread
Old 12-08-2005, 12:40 PM   #1
Default Threads?


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



S
  Reply With Quote
Old 12-08-2005, 01:07 PM   #2
Viator
 
Posts: n/a
Default Re: Threads?
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



Viator
  Reply With Quote
Old 12-08-2005, 01:13 PM   #3
S
 
Posts: n/a
Default Re: Threads?
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.



S
  Reply With Quote
Old 12-08-2005, 03:12 PM   #4
Viator
 
Posts: n/a
Default Re: Threads?
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



Viator
  Reply With Quote
Old 12-08-2005, 03:52 PM   #5
S
 
Posts: n/a
Default Re: Threads?
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.jTextAreaCo mmandLine.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



S
  Reply With Quote
Old 12-08-2005, 04:57 PM   #6
zero
 
Posts: n/a
Default Re: Threads?
"S" <> wrote in
news: oups.com:

> 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.jTextAreaCo mmandLine.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
>
>


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".


--
Beware the False Authority Syndrome


zero
  Reply With Quote
Old 12-08-2005, 05:21 PM   #7
oulan bator
 
Posts: n/a
Default Re: Threads?
something like this should work !

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")){
this.jTextAreaCommandLine.setText(this.jTextAreaCo mmandLine.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");
}
}

}



oulan bator
  Reply With Quote
Old 12-08-2005, 08:00 PM   #8
Thomas Hawtin
 
Posts: n/a
Default Re: Threads?
S wrote:
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/


Thomas Hawtin
  Reply With Quote
Old 12-09-2005, 03:09 PM   #9
Mike
 
Posts: n/a
Default Re: Threads?
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..)



Mike
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cancelling Timer and TimerTask threads wfalby Software 1 04-30-2009 01:04 PM
Why multiple threads are not started ? pls help mandrake Software 0 04-21-2009 10:19 AM
NETFLIX has gone down the tubes funnsun34@hotmail.com DVD Video 95 03-03-2006 03:15 PM
Wtd: The extended version of Stargate SG-1 episode Threads midnightpatches@msn.com DVD Video 0 01-21-2006 02:47 AM
Stargate SG-1 - Season 8 - How long is the uncut version of the episode 'Threads' supposed to be? Zygon Curry DVD Video 10 11-27-2005 07:11 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46