Logo
Distributed Genetic Programming Framework
print print

File org.sfc.parallel.CompoundActivity.java

Here you can find all the information about the file org.sfc.parallel.CompoundActivity.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-05-12 09:39:17
 * Original Filename: org.sfc.parallel.CompoundActivity.java
 * Version          : 1.0.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.sfc.parallel;

import java.util.List;

import org.sfc.collections.Collections;
import org.sfc.events.EventPropagator;
import org.sfc.events.IEventListener;
import org.sfc.events.SfcEvent;
import org.sfc.parallel.events.ParallelStateEvent;

/**
 * This class extends the normal activity by a delay support for the
 * <code>wait_for</code>-method.
 *
 * @author Thomas Weise
 */

public abstract class CompoundActivity extends Activity
  {
/**
 * The list with the sub-activities that need to be terminated before
 * this activity terminates.
 */

  private final List<Object>  m_sub ;
  
   
/**
 * Create a new activity by providing an event propagator and a thread
 * group.
 * @param p_events    The event propagator to be used.
 * @param p_threads   The thread group to use.
 */

  public  CompoundActivity  ( final EventPropagator p_events,
                              final SfcThreadGroup  p_threads )
    {
    super(p_events, p_threads);    
    this.m_sub = Collections.create_list(-1);
    }

  
/**
 * Create a new activity by providing an event propagator only.
 * @param p_events    The event propagator to be used.
 */

  public  CompoundActivity  (final EventPropagator p_events)
    {
    this(p_events, null);
    }


/**
 * Create a new activity by providing a thread group only.
 * @param p_threads   The thread group to use.
 */

  public  CompoundActivity  (final SfcThreadGroup  p_threads )
    {
    this(null, p_threads);
    }
  

/**
 * This method must be used to add sub-activities to an internal list.
 * Its sole purpose is to avoid the shutdown/state events to be thrown in
 * a wrong order. The state event for termination of this compound
 * activity will only be thrown after termination events of all sub-
 * components have been received.
 * @param p_o The sub-activity to be registered.
 */

  protected final void  add_compound_activity(final Object p_o)
    {
    synchronized(this.m_sub)
      {
      if(!(this.m_sub.contains(p_o))) this.m_sub.add(p_o);
      }
    }
  
/**
 * Wait for this object.
 */

  @Override
  public  void  wait_for  ()
    {
    final List<Object> l_l;
    
    super.wait_for();
    
    l_l = this.m_sub;
    
    for(;;)
      {
      synchronized(l_l)
        {
        if(l_l.size() <= 0) return;
        try
          {
          l_l.wait();
          }
        catch(Throwable l_t)
          {
          //
          }
        }
      }
    }
  
  
/**
 * Internally remove an activity.
 * @param p_o The activity to be removed.
 */

  final void rem_act(final Object p_o)
    {
    List<Object> l_l;
    
    l_l = this.m_sub;
    synchronized(l_l)
      {
      if(l_l.remove(p_o))
        {
        if(l_l.size() <= 0)
          {
          l_l.notifyAll();
          }
        }
      }
    }
  

/**
 * <p>
 * This method is used to defer a call to the <code>on_close()</code>-
 * method. The <code>on_close()</code>-method throws the final state event
 * and performs some cleanup for the search engine. <code>on_close()</code>
 * is the last method that has to be called after a search has been
 * performed. No other events must be created by the search engine
 * afterwards.
 * </p><p>
 * You may override <code>defer_on_close()</code> to provide an other
 * delaying method. The default implementation creates a thread that waits
 * for the engine and then calls <code>on_close()</code>.
 * </p>
 * @see #after_termination()
 */

  @Override
  final void  defer_on_close  ()
    {
    this.get_event_propagator().add_listener(
                new ShutdownListener());
    super.defer_on_close();
    }
  
/**
 * The listener which gets notified when everything is other.
 *
 * @author Thomas Weise
 */

  private final class ShutdownListener  implements  IEventListener
    {
/**
 * This method is called when the event sink receives an event.
 * @param p_event The event to be received.
 */

    public  final void  receive (final SfcEvent p_event)
      {
      ParallelStateEvent l_se;

      if(p_event instanceof ParallelStateEvent)
        {
        l_se = ((ParallelStateEvent)p_event);
        if( !(l_se.get_state()))
          {
          CompoundActivity.this.rem_act(l_se.getSource());
          //new Remover(l_se.getSource());
          }
        }
      }
    }
  
///**
// * Remove an activity from the activity's activity list.
// *
// * @author Thomas Weise
// */

//  private final class Remover extends Thread
//    {
///**
// * The activity to remove.
// */

//    private final Object m_o;
//    
///**
// * Create a new remover thread.
// * @param p_o The activity to remove.
// */

//    Remover(final Object p_o)
//      {
//      this.m_o = p_o;
//      start();
//      }
//    
///**
// * The run method.
// */
    
//    @Override
//    public  final void  run()
//      {
//      CompoundActivity.this.rem_act(this.m_o);
//      }
//    }
  }

File Information:

file name:CompoundActivity.java
package:org.sfc.parallel
qualified name:org.sfc.parallel.CompoundActivity.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/parallel/CompoundActivity.java
size:6.038 KB (6183 B)
uploaded: 2018-01-07 12:03:36 GMT+0000
last update: 2006-06-15 10:41:59 GMT+0000
last access: 2018-04-21 09:53:33 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 2018-01-07 12:03:34 GMT+0000 served at 2018-04-21 09:53:33 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo