Link to home
Start Free TrialLog in
Avatar of louiscal
louiscal

asked on

Breaking out of an Infinite loop if user clicks a button

Hello;
        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
louiscal@bu.edu
ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of louiscal
louiscal

ASKER

    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);
        t1.start();
        t2.start();
     }
 
    public void run()
    {
   
        while(true)
        {
            System.out.println("hello run");
            System.out.println(Thread.currentThread().getName());
            Thread.yield();

        }
    }
     
 }


   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()
   }

   init()
   
   run()

   calculate()

   repaint()

}


   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
louiscal@bu.edu
 
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:

http://www.mcp.com/que/et/se_java2e/15javafi.htm

and

http://www.mcp.com/que/et/se_java2e/16javafi.htm

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:

http://www.inf.uni-hohenheim.de/top/java/tutorial/java/threads/index.html

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:

jkelleghan@usa.net

Feel free to ask away.