Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Thread throwing an Exception

Thread view: 
IveCal - 09 Nov 2006 05:50 GMT
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);
    }
}
Matt Humphrey - 09 Nov 2006 12:26 GMT
> 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.

You cannot throw an exception into another program or even another thread.
You can, however, signal (or interrupt) the other thread when the exception
or other condition appears in the timer and that signal can be used to throw
an exception or make the other thread do something.  You'll have to say more
about what you're really trying to do--exceptions are probably not the way
to accomplish it.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.