Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Thread Countdown (extending Thread) Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 
// -----------------------------------------------------------------------------
// ThreadCountdownExtThread.java
// -----------------------------------------------------------------------------

/*
 * =============================================================================
 * Copyright (c) 1998-2005 Jeffrey M. Hunter. All rights reserved.
 *
 * All source code and material located at the Internet address of
 * http://www.idevelopment.info is the copyright of Jeffrey M. Hunter, 2005 and
 * is protected under copyright laws of the United States. This source code may
 * not be hosted on any other site without my express, prior, written
 * permission. Application to host any of the material elsewhere can be made by
 * contacting me at jhunter@idevelopment.info.
 *
 * I have made every effort and taken great care in making sure that the source
 * code and other content included on my web site is technically accurate, but I
 * disclaim any and all responsibility for any loss, damage or destruction of
 * data or any other property which may arise from relying on it. I will in no
 * case be liable for any monetary damages arising from such loss, damage or
 * destruction.
 *
 * As with any code, ensure to test this code in a development environment
 * before attempting to run it in production.
 * =============================================================================
 */
 
/**
 * -----------------------------------------------------------------------------
 * Used to provide an example of how to test the Java Thread facilities by
 * issuing an array of threads that perform looping operations. The loop was
 * used to utilize as much CPU as possible with very little I/O.
 *
 * Threading Concepts
 * ------------------
 * While it is true that the start() method eventually calls the run() method,
 * it does so in another thread of control; this new thread, after dealing
 * with some initialization details, then calls the run() method. After the
 * run() method completes, this new thread also deals with the details of
 * terminating the thread. Keep in mind that the start() method of the original
 * thread returns immediately. Thus, the run() method will be executing in the
 * newly formed thread at about the same time the start() method returns in the
 * first thread.
 *
 * In this example, we create a thread of control by extending the Thread class.
 * This is one of two methods for creating threads. Creating another thread of
 * control using this method is a two-step process. First we must create the
 * code that executes in the new thread by overriding the run() method in our
 * subclass. Then we create the actual subclassed object using its
 * constructor (which calls the default constructor of the Thread class
 * in this case) and begin execution of its run() method by calling the
 * start() method of the subclass.
 *
 * @version 1.0
 * @author  Jeffrey M. Hunter  (jhunter@idevelopment.info)
 * @author  http://www.idevelopment.info
 * -----------------------------------------------------------------------------
 */

public class ThreadCountdownExtThread extends Thread {

    private int countDown = 5;
    private static int threadCount = 0;
    private int threadNumber = ++threadCount;

    /**
     * Constructs a ThreadCountdown object that will executed in a separate
     * thread.
     */
    public ThreadCountdownExtThread() {
        System.out.println("\nStarting thread number => " + threadCount + "\n");
    }

    /**
     * A callback method that will be called by the start() method of the Thread
     * class. This method is very similar to the main() method of a typical
     * class. A new thread begins execution with the run() method in the same
     * way a program begins execution with the main() method.
     * <p>
     * While the main() method receives its arguments from the argv parameter
     * (which is typically set from the command line), the newly created thread
     * must receive its arguments programmatically from the originating thread.
     * Hence, parameters can be passed in via the constructor, static instance
     * variables, or any other technique designed by the developer.
     */
    public void run() {
        while(true) {

            System.out.println(
                "  - Thread number " +
                threadNumber +
                " ( Current Countdown = " + countDown + " )");

            for (int j = 0; j < 300000000; j++) {
                // This is a test...
            }
           
            if (--countDown == 0) {
                System.out.println(
                    "\nEnding thread number => " + threadNumber + "\n");
                return;
            }
        }
    }


    /**
     * Starts all five threads. This method will perform a sleep operation
     * before starting each thread to allow the output to reflect how each
     * thread operates.
     * @exception java.lang.InterruptedException Thrown from the Thread class.
     */
    private static void doThreadCountdown()
        throws java.lang.InterruptedException {
       
        for (int i = 0; i < 5; i++) {
            Thread.sleep(2000);
            new ThreadCountdownExtThread().start();
        }
        System.out.println("\n<< All threads have now been started!!! >>\n");
    }


    /**
     * Sole entry point to the class and application.
     * @param args Array of String arguments.
     * @exception java.lang.InterruptedException Thrown from the Thread class.
     */
    public static void main(String[] args)
        throws java.lang.InterruptedException {

        System.out.println("\n<< MAIN METHOD (Begin) >>");
       
        doThreadCountdown();
       
        System.out.println("<< MAIN METHOD (End) >>\n");

    }

}

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.lang.Thread

  org.omg.CORBA.Current

  java.lang.reflect.Array




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy