wood burning stoves*
The moose likes Beginning Java and the fly likes how to show busy when a task is running? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to show busy when a task is running?" Watch "how to show busy when a task is running?" New topic
Author

how to show busy when a task is running?

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
I want to start a thread when a task is executing and show user the application is busy at that moment. I know i need to use thread and timer to accomplish that, but i have no idea how to create a thread. I did read some tutorials on sun, but i still do not understand how it works, can anyone please show me a simple example or tutorial?
Mindy
Thanks!
Greg Up
Greenhorn

Joined: Feb 15, 2001
Posts: 4
Here's one way.


[This message has been edited by Greg Up (edited August 14, 2001).]
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Thank you Greg!
But, how can i stop the thread?
I did it in this approach in a Action Event inner class and it works fine but i don't know when will this thread stops.
Thread thread = new Thread(
new Runnable()
{
public void run()
{
try
{
\\do something
}
catch(Exception error){}
}
}//end of Run()
}//end of runnable
);//end of new thread

thread.start();
Thank you again!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

A thread stops when it reaches the end of it's run() method. This thread only print's out "Thread Running" once it starts, then dies.



This thread continually prints out "Thread Running"... but wastes a lot of CPU time doing this while other stuff is happening... the thread doesn't die until a System.exit() call or a Ctrl-C kills the java program.



This thread continuously prints out "Thread Running", but gives more time to other things by pausing and only printing every 100 milliseconds ( approximately ). This doesn't waste as much CPU time, but the thread still doesn't die until the JVM is explicitly killed.



This thread prints out "Thread Running" every 100 milliseconds ( approximately ), but can be killed by setting a boolean called threadRunning to false elsewhere in the program. Once a thread is killed, it cannot be restarted. You have to do another myThread = new Thread(); myThread.start(); to get it to restart...



Hopefully this helps you understand threads a bit more? Keep posting if you need more pointers....

-Nate
[This message has been edited by Nathan Pruett (edited August 14, 2001).]


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
You examples are very helpful! I still have one question would like to ask: where should i set that boolean to false to make that thread stops?
Thanks!
Mindy
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Where ever you want. Create a separate method.
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
If you are calling another class within the try even you set the boolean value to false, the thread is never actually stops.

Can you tell me what's going on of the following code?
public void run()
{
while( threadRunning )
{
try
{
Thread.sleep( 100 );
new ClassA();
threadRunning = false;
waitDialog.show() //show an animation dialog as it is busy and it will stop when execution finishes
}
catch( InterruptedException exception )
{ }
System.out.println( "Thread Running" ); }}
if ( waitDialog.show("Executing..") == WaitDialog.CANCEL_PRESSED)
{
System.out.println("Process was cnacelled");
JOptionPane.showMessageDialog(null, "Process was cancel.", "Application", JOptionPane.ERROR_MESSAGE);
}//when i pressed cancel, the thread still alive and the action in the the new ClassA is still continue executing.
My question is how to get the execution stops when Cancel button clicks?
Thanks!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The thread actually has to control whatever is happening, not just kick it off... for example, if you have an animation, the thread has to have a loop in which either the thread or a class the thread calls only draws one frame per call. The thread then loops, so the next frame is drawn with the next loop of the thread, and on and on, each frame drawn in succession.

If the thread kicks off a method that loops itself to draw the animation, there is no way to interrupt the other loop... it waits until it is done, then goes back to the thread, which then ends...

-Nate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to show busy when a task is running?