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 / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Threads

Thread view: 
Jenski - 10 Dec 2005 14:50 GMT
I'm in the middle of refactoring a program, it is controlled by a
single thread. I was wondering if there a more efficient way of running
the program without using Threads? As this seems to be in an infinite
loop.

thanks
Andrew Thompson - 10 Dec 2005 14:57 GMT
> I'm in the middle of refactoring a program, it is controlled by a
> single thread. I was wondering if there a more efficient way of running
> the program without using Threads? As this seems to be in an infinite
> loop.

A Thread will not make an 'infinite loop' more efficient.
Or rather, if the 'infinite loop' is a bug, a Thread will
not improve things.

It seems you need to find the cause of the infinite loop
before proceeding with refactoring, though it would pay to
check first, if the infinite loop was present in the
application *prior* to refactoring.

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

Jenski - 10 Dec 2005 15:57 GMT
> A Thread will not make an 'infinite loop' more efficient.
> Or rather, if the 'infinite loop' is a bug, a Thread will
> not improve things.

I know, that's why I want to change it. The cause of the infinite loop
is the fact that the while statement cannot complete without throwing
an exception.

run() {
while (true) {

}
}
Andrew Thompson - 10 Dec 2005 16:30 GMT
>>A Thread will not make an 'infinite loop' more efficient.
>>Or rather, if the 'infinite loop' is a bug, a Thread will
[quoted text clipped - 6 lines]
> run() {
> while (true) {

What happens here?

Is the basic problem that your program/GUI has become
unresponsive at a time that the program is doing
something that takes a long time?

> }
> }

(If the answer to the above is 'yes', you might find tips
by searching the groups for 'block EDT'.  Otherwise, I
might need more information about what is happening inside
that loop, and why you need to throw an exception to stop it.)

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

VisionSet - 10 Dec 2005 18:55 GMT
> > A Thread will not make an 'infinite loop' more efficient.
> > Or rather, if the 'infinite loop' is a bug, a Thread will
[quoted text clipped - 9 lines]
> }
> }

> ...seems to be in an infinite loop...
?? Surely you know it is an infinite loop

Well don't use an infinite loop then.
If you know the condition that you want to stop the thread, then specify it
in the while statement.

or a really generic approach:

Runnable myRunnable = new Runnable() {

   boolean keepRunning = true;

   void stopThread() {
       keepRunning = false;
   }

   public void run() {

       while(keepRunning) {
           //stuff
       }
   }
}

--
Mike W
Mark Haase - 10 Dec 2005 21:19 GMT
> I'm in the middle of refactoring a program, it is controlled by a
> single thread. I was wondering if there a more efficient way of running
> the program without using Threads? As this seems to be in an infinite
> loop.
>
> thanks

We need a lot more information to give a good answer. What does the
program do and how is it currently designed? In general, Threads are
useful for running tasks that take a while to finish. By running it in a
Thread you can keep the GUI responsive to user input. Notice that any
code you call from main() is already in its own thread, and any code
called from an event handler is running on the EDT. In the latter case
you definitely don't want to perform lengthy calculations because you'll
lock up the GUI.

|\/|  /|  |2  |<
mehaase(at)gmail(dot)com
Roedy Green - 10 Dec 2005 21:36 GMT
>I'm in the middle of refactoring a program, it is controlled by a
>single thread. I was wondering if there a more efficient way of running
>the program without using Threads? As this seems to be in an infinite
>loop.

Adding threads won't fix an infinite loop problem just complicate the
solution. Threads help when you have a mixture of short running and
long running things to do and you have clients waiting for results.
Threads also help when you have a number of i/o bound things to do.

Threads help least in a pure cpu bound problem unless you actually
have multiple CPUS or at least multiple cores.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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.