jason.core
Class ThreadableService

java.lang.Object
  |
  +--jason.core.AbstractService
        |
        +--jason.core.ThreadableService
All Implemented Interfaces:
java.io.Serializable, Service, ServiceDataListener
Direct Known Subclasses:
jason.core.service.InnerService, TaskableService, TaskService

public class ThreadableService
extends AbstractService

Created Wed May 15 16:05:33 2002

This is a core class for services that needs to manage a thread. This class will enable a thread to be started for the Start method. User can specify to loop in the run automatically.

You shouldn't write a main loop in the run method. Please call setThreadLoopMode with the correct value for that.

Author:
Alexandre Brillant (abrillant@wanadoo.fr)
See Also:
Serialized Form

Field Summary
protected  java.lang.Thread activeThread
           
 
Fields inherited from class jason.core.AbstractService
INITIALIZED, name, NOSTATE, serviceContext, STARTED, STOPPED
 
Fields inherited from interface jason.core.Service
ERROR_MESSAGE, INFORMATION_MESSAGE, VERBOSE_MESSAGE, WARNING_MESSAGE
 
Constructor Summary
ThreadableService()
          By default the thread will not be started at the start method.
ThreadableService(boolean autoStart)
          This constructor will let the service be started as a thread automatically with the start method.
 
Method Summary
protected  void actionForStart()
          Don't override it, this method is called when starting the service for starting the thread is needed of the service.
protected  void enableServiceForThread()
          Be careful with this method, it is normally in the constructor of your service you choose it When starting the thread will call the runThread method
protected  long getThreadLoopDelay()
           
protected  boolean isThreadLoopMode()
           
protected  boolean isThreadStarted()
           
protected  boolean isWaiting()
          This method will be called is the current thread is waiting (user has call the threadWait method.
protected  void notifyThreadRunning()
          This method can be overrided for a task before the run method.
protected  void runThread()
          This method call run method.
 void setPriority(int newPriority)
          Here the thread priority.
protected  void setThreadLoopDelay(long delay)
          This value is only for the thread loop mode.
protected  void setThreadLoopMode(boolean loopMode)
          This is an important method, because it permits to use the run in a loop.
 void threadNotify()
           
 void threadWait()
           
 
Methods inherited from class jason.core.AbstractService
canRestartOnError, checkForContext, fireCriticalErrorDetected, getAutoRestartCounter, getBooleanProperty, getInfo, getIntProperty, getName, getPropertiesName, getProperty, getServiceContext, getServiceState, getState, getStringProperty, getType, getVersion, hasServiceContext, init, isAutoStop, isEnabled, isInitialized, isOverride, isPersistent, isPrivate, isProcessable, isStarted, isStopped, isTest, isThreadable, isVerbose, manageRequest, manageRequestForType, notifyData, notifyInitReady, notifyStartReady, notifyStopReady, postStop, restart, run, sendCustomMessage, sendErrorMessage, sendInformationMessage, sendInnerError, sendWarningMessage, setAutoRestartCounter, setAutoRestartOnError, setAutoStop, setEnabled, setName, setPersistent, setPrivate, setProperty, setServiceContext, setTest, setType, setVerbose, setVersion, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activeThread

protected java.lang.Thread activeThread
Constructor Detail

ThreadableService

public ThreadableService()
By default the thread will not be started at the start method. For enabling it, please call the enableServiceForThread at the right time


ThreadableService

public ThreadableService(boolean autoStart)
This constructor will let the service be started as a thread automatically with the start method.

Parameters:
autoStart - true for starting the service as a thread automatically
Method Detail

enableServiceForThread

protected void enableServiceForThread()
                               throws ServiceException
Be careful with this method, it is normally in the constructor of your service you choose it When starting the thread will call the runThread method

Throws:
if - another thread is already running
ServiceException

actionForStart

protected void actionForStart()
Don't override it, this method is called when starting the service for starting the thread is needed of the service. Note that if you don't specify to run the service as a thread for starting, the run will be called once

Overrides:
actionForStart in class AbstractService

setThreadLoopMode

protected void setThreadLoopMode(boolean loopMode)
This is an important method, because it permits to use the run in a loop. This is useful for the thread state. By default this method is to true. so if you want a thread that is launched one time and expired after the first task use the false value. Note that is you change this value while running, it will stopped the current loop if the initial value was true.


isThreadLoopMode

protected boolean isThreadLoopMode()
Returns:
true if the code is in a loop mode for the threadable state

setThreadLoopDelay

protected void setThreadLoopDelay(long delay)
This value is only for the thread loop mode. It inserts automatically a sleep method with the delay value. By default this value is with 100 ms. If you use a value <= 0 then it will disable the delay


getThreadLoopDelay

protected long getThreadLoopDelay()
Returns:
the delay in ms for the loop thread period

runThread

protected void runThread()
This method call run method. This is called inside a thread. This method is able to stop It could be dangerous to override it


notifyThreadRunning

protected void notifyThreadRunning()
This method can be overrided for a task before the run method. This is called when the thread is starting. If you have specified a thread loop mode , this method will not be called in the loop, it will be called only once.


setPriority

public void setPriority(int newPriority)
Here the thread priority. You can use Thread.MAX_PRIORITY, Thread.NORM_PRIORITY or Thread.MIN_PRIORITY. This method will be take into account in every case, when the start will be started or when it is running


isWaiting

protected boolean isWaiting()
This method will be called is the current thread is waiting (user has call the threadWait method. Else by calling the threadNotify method, the response should be false


threadWait

public void threadWait()
                throws java.lang.InterruptedException
java.lang.InterruptedException

threadNotify

public void threadNotify()

isThreadStarted

protected boolean isThreadStarted()