I
IveCal
Hello, I got this code from David Reilly's site. What this prog does is
terminate the prog after a specified time. What I want to do is just
throw an Exception to be caught by other java programs. I tried to
throw an exception in timeout (ex. timeout() throws Exception) and run
(ex. run throws Exception) methods but it is not allowed. Please help.
import java.net.*;
public class Timer extends Thread
{
/** Rate at which timer is checked */
protected int m_rate = 100;
/** Length of timeout */
private int m_length;
/** Time elapsed */
private int m_elapsed;
/**
* Creates a timer of a specified length
* @param length Length of time before timeout occurs
*/
public Timer ( int length )
{
// Assign to member variable
m_length = length;
// Set time elapsed
m_elapsed = 0;
}
/** Resets the timer back to zero */
public synchronized void reset()
{
m_elapsed = 0;
}
/** Performs timer specific code */
public void run()
{
// Keep looping
for (;
{
// Put the timer to sleep
try
{
Thread.sleep(m_rate);
}
catch (InterruptedException ioe)
{
continue;
}
// Use 'synchronized' to prevent conflicts
synchronized ( this )
{
// Increment time remaining
m_elapsed += m_rate;
// Check to see if the time has been exceeded
if (m_elapsed > m_length)
{
// Trigger a timeout
timeout();
}
}
}
}
// Override this to provide custom functionality
public void timeout()
{
System.err.println ("Network timeout occurred.... terminating " );
System.exit(1);
}
}
terminate the prog after a specified time. What I want to do is just
throw an Exception to be caught by other java programs. I tried to
throw an exception in timeout (ex. timeout() throws Exception) and run
(ex. run throws Exception) methods but it is not allowed. Please help.
import java.net.*;
public class Timer extends Thread
{
/** Rate at which timer is checked */
protected int m_rate = 100;
/** Length of timeout */
private int m_length;
/** Time elapsed */
private int m_elapsed;
/**
* Creates a timer of a specified length
* @param length Length of time before timeout occurs
*/
public Timer ( int length )
{
// Assign to member variable
m_length = length;
// Set time elapsed
m_elapsed = 0;
}
/** Resets the timer back to zero */
public synchronized void reset()
{
m_elapsed = 0;
}
/** Performs timer specific code */
public void run()
{
// Keep looping
for (;
{
// Put the timer to sleep
try
{
Thread.sleep(m_rate);
}
catch (InterruptedException ioe)
{
continue;
}
// Use 'synchronized' to prevent conflicts
synchronized ( this )
{
// Increment time remaining
m_elapsed += m_rate;
// Check to see if the time has been exceeded
if (m_elapsed > m_length)
{
// Trigger a timeout
timeout();
}
}
}
}
// Override this to provide custom functionality
public void timeout()
{
System.err.println ("Network timeout occurred.... terminating " );
System.exit(1);
}
}