Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.SearchTaskQueue.java

Here you can find all the information about the file org.dgpf.search.api.SearchTaskQueue.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-03-31 06:26:19
 * Original Filename: org.dgpf.search.api.SearchTaskQueue.java
 * Version          : 2.1.0
 * Last modification: 2006-05-12
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.dgpf.search.api;

import java.io.Serializable;

import org.sfc.collections.Arrays;
import org.sfc.parallel.IAbortable;
import org.sfc.parallel.IWaitable;


/**
 * A search queue helps you by queueing search tasks an allows search
 * threads to remove pending tasks.
 * 
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.   
 *
 * @author Thomas Weise
 */

public class      SearchTaskQueue<Genotype extends Serializable>
       implements IWaitable, IAbortable
  {
/**
 * The task queue.
 */

  private volatile  SearchTask<Genotype>[]  m_queue ;
/**
 * The insert position.
 */

  private volatile  int                     m_ins_pos ;
/**
 * The remove pointer.
 */

  private volatile  int                     m_rem_pos ;
/**
 * The count of tasks currently either enqueued or in progress.
 */

  private volatile  int                     m_count   ;
  
/**
 * The tasks-are-available synchronizer.
 */

  private final     Object                  m_avail_sync  ;
/**
 * The synchronizer to ensure that the queue is empty.
 */

  private final     Object                  m_empty_sync  ;
  
/**
 * Only <code>true</code> as long as the system is running.
 */

  private volatile  boolean                 m_is_running     ;
/**
 * The count of threads using this search queue.
 */

  private volatile  int                     m_thread_count  ;
  
/**
 * The count of tasks currently in progress.
 */

  private volatile  int                     m_in_progress ;
  
/**
 * Create a new search task queue.
 * @param p_min_size  The minimum size of the search queue.
 */

  public  SearchTaskQueue(final int p_min_size)
    {
    super();
    this.m_queue = Arrays.create(SearchTask.class,
                                ((p_min_size > 128) ? p_min_size : 128));
    this.m_avail_sync = new Object();
    this.m_empty_sync = new Object();
    this.m_is_running = true;
    }
  
  
  
/**
 * Obtain the next task to be processed from the queue. This method blocks
 * until a new task becomes available or the queue is aborted. In case the
 * queue is aborted, <code>null</code> will be returned.
 * @return  A search task to be processed, or <code>null</code> if the
 *          search queue has been aborted. 
 */

  final SearchTask<Genotype>  next_task ()
    {
    int                   l_rp;
    SearchTask<Genotype>  l_st;
    
    for(;;)
      {      
      synchronized(this.m_avail_sync)
        {
        if(!(this.m_is_running)) return null;
        if(  (l_rp = this.m_rem_pos) >= this.m_ins_pos )
          {
          try
            {
            this.m_avail_sync.wait();
            }
          catch(Throwable l_t)
            {
            //
            }
          continue;
          }
        
        l_st = this.m_queue[l_rp];
        l_rp++;
        
        if(l_rp >= this.m_ins_pos)
          {
          this.m_rem_pos = 0;
          this.m_ins_pos = 0;
          }
        else
          {
          this.m_rem_pos = l_rp;
          }
        
        this.m_in_progress++;
        
        return l_st;
        }
      }
    
//    return null;
    }
  
/**
 * Informs the task queue that a task has been processed.
 */

  final void  done  ()
    {
    synchronized(this.m_avail_sync)
      {
      synchronized(this.m_empty_sync)
        {
        if( (--this.m_count) <= 0)
          {
          this.m_count       = 0;
          this.m_in_progress = 0;
          this.m_empty_sync.notifyAll();
          }
        else
          {
          this.m_in_progress--;
          }
        
        if(this.m_in_progress <= 0)
          {
          this.m_avail_sync.notifyAll();
          }
        }
      }
    }
  
/**
 * Abort the search task queue and end all activity.
 */

  public  final void  abort ()
    {
    synchronized(this.m_avail_sync)
      {
      if(this.m_is_running)
        {
        this.m_is_running  = false;
        
        synchronized(this.m_empty_sync)
          {        
          this.m_rem_pos     = 0;
          this.m_ins_pos     = 0;
          this.m_count       = Math.min(this.m_count, this.m_in_progress);
                  
          if( (this.m_count        <= 0) ||
              (this.m_thread_count <= 0) )
            {
            this.m_count       = 0;
            this.m_in_progress = 0;
            this.m_empty_sync.notifyAll();
            }
          }
        }
      
      this.m_avail_sync.notifyAll();
      }   
    }
  
/**
 * Wait until all tasks in the search task queue have been performed
 * completely.
 */

  public  final void  wait_for ()
    {
    for(;;)
      {
      synchronized(this.m_empty_sync)
        {
        ifthis.m_count <= 0 ) return;
 
        try
          {
          this.m_empty_sync.wait();
          }
        catch(Throwable l_t)
          {
          //
          }          
        }
      }
    }
  
/**
 * Add a task to the task queue.
 * @param p_task    The task to be added.
 */

  public  final void  add_task  (final SearchTask<Genotype> p_task)
    {
    int                     l_ip, l_rp;
    SearchTask<Genotype>[]  l_ar;
    
    if(p_task == null) return;
    p_task.clear();
    
    synchronized(this.m_avail_sync)
      {
      if(this.m_is_running)
        {
        l_ip = this.m_ins_pos;        
        l_ar = this.m_queue;
        
        if(l_ip >= l_ar.length)
          {
          l_rp = this.m_rem_pos;
          if(l_rp > 0)
            {
            l_ip          -= l_rp;
            System.arraycopy(l_ar, l_rp, l_ar, 0, l_ip);
            this.m_rem_pos = 0;
            }
          else
            {          
            l_ar = Arrays.create(SearchTask.class, l_ip << 1);
            System.arraycopy(this.m_queue, 0, l_ar, 0, l_ip);
            this.m_queue = l_ar;
            }
          }
        
        l_ar[l_ip]     = p_task;
        this.m_ins_pos = (l_ip + 1);
        this.m_count++;
        
        this.m_avail_sync.notify();
        }
      }
    }
  
/**
 * Called when a new thread begins to use the task queue.
 */

  final void  add_thread()
    {
    synchronized(this.m_avail_sync)
      {
      synchronized(this.m_empty_sync)
        {
        this.m_thread_count++;        
        }
      }    
    }
  

/**
 * Called when a thread doesn't use the queue anymore.
 */

  final void  remove_thread()
    {
    synchronized(this.m_avail_sync)
      {
      synchronized(this.m_empty_sync)
        {  
        if( (this.m_thread_count--) <= 0)
          {
          this.m_thread_count = 0;
          this.abort();
          }          
        }
      }    
    }
  
/**
 * Tells the activity to stop its actions. This is the proper method to
 * allow the activity to terminate itself.
 * This method does block until the activity has finished.
 * @see #abort()
 */

  public  final void  abort_and_wait  ()
    {
    this.abort();
    this.wait_for();
    }
  }

File Information:

file name:SearchTaskQueue.java
package:org.dgpf.search.api
qualified name:org.dgpf.search.api.SearchTaskQueue.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/SearchTaskQueue.java
size:8.151 KB (8347 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-08-16 06:32:52 GMT+0000
last access: 2017-11-23 05:31:20 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-23 05:31:20 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo