We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Breaking out of an Infinite loop if user clicks a button

louiscal asked
Medium Priority
Last Modified: 2012-05-04
        Here's my problem. I need to keep on looping through some calculations till the answer is found. While it's doing this I would like to have a pause button so that the user can change the variables in real time. The problem is that the loop will continue till the end. If I use the action() method to capture the mouse click (such as a pause button) the results will only be shown when the calculations are done.

    Any idea of how to temporally stop the loop? I tried make the loop so that it looks like this:
            while(running == true)

    and set the running variable false when the user hit's pause but this is done when the answer is found.

     I also thought of creating another thread for the buttons and pausing the default thread that to allow the other thread a chance.  This brought up some questions also:

        If I declare a thread such as (Thread my_thread null) is this the default thread that Java uses or is there another thread created every time a program runs? If this isn't the main thread does anyone know how to pause it and give control to another thread that may be waiting around?

     Finally, someone suggested to me that I should use the
Thread.yield() in my loop to allow the action method a chance to do it's work but the loop continue's on till it's all done.

Louis Calisi
Watch Question

To pause the thread try using sleep().

The default thread is actualy the message loop for the app. or thread. You cna hook into it at the begining using main() for apps or init() for threads.

When you instantiate a thread, it represnts a new thread (child process). Now, there is nothing wrong with the yield()idea. The problem is that you are not pausing the threads thats yealding. If both threads have the same priority it will usualy just chug allong as if you hadn't yealded in non premptive schedulers.

To solve this, I would suggest you use Thread's setPriority()method and give the button processing thread a higher priority than the main loop. Higher is diferent on each platform, a diference of 1 is usualy good enough for UNIX, Windows on the other hand usualy requires a difference of several thousands.

The following program can help you test priorities:

public class RunnablePottato implements Runnable{
   public void run(){

public class PriorityTester{
   public static void main(String argv[]){
      RunnablePottato aRP = new RunnablePottato();
      Thread t1 = new Thread(aRP, "onePottato);
      Thread t2 = new Thread(aRP, "twoPottato");

      t2.setPriority(t1.getPriority() + 1);

If you set bot priorities equal, in a non premptive environment like windows, you will only print data from a single thread. If you are running under windows change the priority value from 1 to 1000 or more.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


    That was helpfull but it brought up some new questions. Using Visual Cafe I can only get this program to execute as am application and not an applet. I modified the code that you sent me and this is what it currently looks like (all in one file):

import java.awt.*;
import java.applet.*;

 public class PriorityTester extends Applet implements Runnable
    public static void main(String argv[])
        System.out.println("hello main");
        RunnablePottato aRP = new RunnablePottato();
        Thread t1 = new Thread(aRP, "onePottato");
        Thread t2 = new Thread(aRP, "twoPottato");

        t2.setPriority(t1.getPriority() + 1);
    public void run()
            System.out.println("hello run");


   What is the the string arg thing in this program used for?

   When it comes time to write my real program it has these functions: init, run, calculate (does the mathematical operations for the model), handle event, paint and now main.

   Should my new program framework look like this so that the buttons can have higher priority than the calculations:

import ...

public class model extends Applet implements Runnable
   global variables

   public static void main(String argv[])
      - handle event() and all the button functions
      - start()





   Or does it matter if the handle event stuff is outside the main? If this is the case then how do you create a thread so that "it is connented" to the buttons.

Thanks alot

Louis Calisi
First of all, main() is a method that is only valid in the context of an application.:-) In order to get it to run in applet, you can place the code contained in the main() method I sent you and place it either in the init() method, the start() method, or in a separate method that is called by one of these two.

For a good refresher on writting applets see:




String args[] is used by applications to be able to parse command line arguments. Say you invoke your Java app. as follows:

java copy file1 file2

then, args[0] will contain "file1" and args[1] will contain "file2".

It seems that the code I sent you acomplished to confuse you.

The purpose of the code was to give you a stand alone application that would aid you in determining if your OS has pre emptive or non premtive multitasking (threads). Based on that, you could use the same code to find priority settings that would work well under your plattform. (diferent plattforms work better with diferent settings).

The final reason I sent that block of code is that you can implement the techniques for setting priorities and calling the yield() method in your own code, with something that works as a referance.:-)

Using an application, rather than an applet, is a combination of kindliness and lazyness. An application does not need an HTML file and/or a web server to run, making using it eisier.

Have a look at the material for applets that I have refered to you, then look at:


for good info on threads. When your done reviewing all this info, you'll be prety close to mastering both subjects.

I'll be available to answer any further questions you might have on these subjects by e-mail at:


Feel free to ask away.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.