// Sets up an infinite loop .
while (true)
{
// do the thread processing here.
}
I will alter this to be a Boolean value that I can change using some set methods in the object. This will provide me an interface for turning the thread on and off. Below is a partial listing of the class I end up with.
public class FileEvictor
{
private Boolean threadOn = true;
private int start = 1;
private int stop = 2;
private void init()
{
try
{
//Create background thread, which will be responsible for purging expired items.
Thread threadCleanerUpper = new Thread( new Runnable()
{
public void run()
{
System.out.println("File Evictor Service is running.");
try
{
// Sets up an infinite loop .
while (threadOn)
{
// do the thread processing here.
}
}
catch (Exception e)
{
System.out.println("Unknown exception" + e.getMessage());
e.printStackTrace();
}
finally
{
//TODO more info on what bombed
}
return;
}
}, "File Evictor"); // End class definition and close new thread definition
// Sets the thread's priority to the minimum value.
threadFileEvictor.setPriority( Thread.MIN_PRIORITY );
// Starts the thread.
threadFileEvictor.start();
}
public FileEvictor( int aStartHour, int aStopHour)
{
start = aStartHour;
stop = aStopHour;
init();
}
public void setThreadOn()
{
threadOn = true;
}
public void setThreadOff()
{
threadOn = false;
}
Once I have these start and stop methods in my thread, then all I have to do is create a context listener that will start the thread and stop the thread when the context comes up or down.
No comments:
Post a Comment