File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Inifinite Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inifinite Loop" Watch "Inifinite Loop" New topic
Author

Inifinite Loop

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Hi,

We have a Java program that will run infinately

for (;;)
{
// Business Logic
}

Currently running in Unix machine. How to stop this without killing ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You can't. That code will only stop if it exits the loop, either by a return or break call. If those are not present then you cannot make the loop stop except by terminating the entire JVM.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
In case if I try

boolean flag = true;

while(flag)
{
// business logic
}

Assume this as a standalone Java program, From where I can make the flag as false ?..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You'll need to create an entry point in your application to be able to set it to false. Two obvious ways are:

1) let the program use a server socket, then let another application send a quit command to this server socket. If this quit command is received the flag needs to be set to false. Note that the flag needs to be a volatile instance field, declared outside the method. The volatile is required to ensure that the thread that executes this loop doesn't use a cached value that never changes.

2) let the program use a specific file; in Linux and Unix these are called lock files. Let the application create it when it starts. The test then becomes (lockFile.exists() could also have been used). If you delete the lock file the loop will end after its current iteration.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Second solution suits my requirement. Could you please explain me more about that. What is the lock file means ? It’s normal text file can I create it from my application.?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

It can be any file, even an empty one. Your code should create it when the application starts:
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Please correct me if am wrong,

when the user executed start.sh(start.java) my infinite loop program will start as

stast.java


when the user executed stop.sh(call stop.java) my infinite loop program will end as
// Another program stop.java

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Provided that the file system is honest to your program (about .exists()) then that should work.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Honest mean ?

or I can apply some lock to the txt file ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inifinite Loop