Current location: Hot Scripts Forums » Programming Languages » Everything Java » infinite loop versus guarded blocks


infinite loop versus guarded blocks

Reply
  #1 (permalink)  
Old 11-11-07, 03:53 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
infinite loop versus guarded blocks

I'm currently working on a shortest path algorithm (very basic), and to speed things up i wanted to make it bi-directional (the algorithm will start scanning in 2 directions instead of one). To accomplish this, i was thinking of using Threads to run 2 scanners at the same time.

Now when one of the Scanners meats the other one, a match is found (the shortest path must be within the scanned area), and both threads need to stop running. To prevent my application from stopping to run when both Threads are started, i placed them in an infinite loop, and check if one of the scanners have found a match yet.

After some reading on the Java Tutorial, i discovered something named Guarded Blocks. I don't quite understand how it works yet, but i think this is exactly what i need. In the description they speek of unnecessary use of memory and wasted processor time when using a neverending loop (or at least till the condition fails).

I tried working with those Guarded Blocks, but it doesn't seem to work (no doubt i'm doing something wrong). Here's a basic example of what i have now, and what needs to be changed to those Guarded Blocks if possible:
Java Code:
  1. public class ShortestPath{
  2.  
  3.   private Scanner start = new Scanner();
  4.   private Scanner end = new Scanner();
  5.  
  6.   public boolean search() throws NoPathFoundException {
  7.     start.start();
  8.     end.start();
  9.     while (!end.hasMatch() && !start.hasMatch()) {
  10.       // waste some processing time
  11.     }
  12.     return end.hasMatch() || start.hasMatch();
  13.   }
  14.  
  15. public class Scanner extends Thread {
  16.  
  17.   private boolean match = false;
  18.  
  19.   public void run() {
  20.     while(true){
  21.       // loop over all entries of the map to see what the shortest path is
  22.       // checks in the scanned list of the second Scanner to see if the current entry of the map is allready in there, if so a match is found
  23.       // if match:
  24.         setMatch(true);
  25.     }
  26.   }
  27.  
  28.   public boolean hasMatch() {
  29.     return match;
  30.   }
  31. }
If you look at this article at the Java Tutorials, you'll see that there must be a better way to do what i want. I just don't understand how it works exactly.

I tried letting my ShortestPath class wait() in the while(!end.hasMatch() && !start.hasMatch()) loop, and then trigger a notifyAll event in the Scanner (when a match was found), but it didn't unlock the ShortestPath class, leaving it in an infinite loop

Many thanks if someone can help me understand the concept of Guarded Blocks
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL infinite loop problem with VB application Syed Visual Basic 2 05-07-04 11:19 AM


All times are GMT -5. The time now is 01:38 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.