• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inifinite Loop

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be any file, even an empty one. Your code should create it when the application starts:
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Provided that the file system is honest to your program (about .exists()) then that should work.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honest mean ?

or I can apply some lock to the txt file ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic