<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>High Fibre Programming &#187; indicator</title>
	<atom:link href="http://www.4pmp.com/tag/indicator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.4pmp.com</link>
	<description>PHP, MySQL, C, Java, Linux and other great after dinner speech topics</description>
	<lastBuildDate>Thu, 08 Jul 2010 07:35:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spinning command line cursor in Java</title>
		<link>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/</link>
		<comments>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 12:48:31 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[ctrl+c]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[indicator]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[multithreaded]]></category>
		<category><![CDATA[spin]]></category>
		<category><![CDATA[spinning]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=238</guid>
		<description><![CDATA[As a follow-up to my previous article on displaying a spinning cursor to display program activity, I have created a more useful example that could be adapted for use.
The original problem was this &#8211; I was writing a program that performed a particular task and I needed some way of showing the program was alive [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow-up to my<a href="http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/"> previous article</a> on displaying a spinning cursor to display program activity, I have created a more useful example that could be adapted for use.</p>
<p>The original problem was this &#8211; I was writing a program that performed a particular task and I needed some way of showing the program was alive and functioning correctly whilst doing the task.</p>
<p>In Windows this is done by changing the mouse cursor into an hourglass, unfortunately on the command line we don&#8217;t have such GUI luxuries.   One classic option is to create a spinning line by displaying | / &#8211; \ and so on.</p>
<p>The example below creates two child threads; one to do a particular job (in this case slowly count to 10) and another thread to display a spinning cursor.</p>
<pre class="brush: java;">
/**
 * Spinning cursor test class
 *
 * @author Nick Giles
 */
public class SpinTest
{
    /**
     * Thread which does the stuff we're interested in
     *
     */
    private Thread stuff;

    /**
     * Thread class which does stuff in the background
     *
     */
    private class Stuff implements Runnable
    {
        public void run()
        {
            int result = 0;

            try
            {
                // Slowly count to 10, although you can of course put whatever
                // you want here, presumably something a bit more useful
                while (result &lt; 10)
                {
                    result++;
                    Thread.sleep(1000);
                }
            }
            catch (InterruptedException e)
            {
                // If interrupted then quietly end the thread, you may well
                // want to handle this in some way
            }
        }
    }

    /**
     * Spinner thread
     *
     */
    private class Spinner implements Runnable
    {
        public void run()
        {
            String[] phases = {&quot;|&quot;, &quot;/&quot;, &quot;-&quot;, &quot;\\&quot;};

            try
            {
                while (true)
                {
                    for (String phase : phases)
                    {
                        System.out.printf(&quot;\b&quot;+phase);

                        Thread.sleep(100);
                    }
                }
            }
            catch (InterruptedException e)
            {
                // No need to do anything if interrupted
            }
        }
    }

    /**
     * Handles all shutdown functions
     *
     */
    public class ShutdownHandler extends Thread
    {
        public void run()
        {
            // On interrupt, stop doing stuff
            stuff.interrupt();
        }
    }

    public void doStuff()
    {
        try
        {
            // Attach an object to handle shutdown signals (i.e. ctrl+c)
            Runtime.getRuntime().addShutdownHook(new ShutdownHandler()); 

            // Create a new thread for spinning the cursor
            Thread spinner = new Thread(new Spinner());

            // Create a new thread for doing stuff
            this.stuff = new Thread(new Stuff());

            // Nice message to user
            System.out.printf(&quot;Doing stuff...  &quot;);

            // Start doing stuff
            this.stuff.start();

            // Start spinning the cursor
            spinner.start();

            // Check the thread doing stuff is still doing stuff
            while (stuff.isAlive())
            {
                stuff.join(1000);
            }

            System.out.printf(&quot;\bDone.\n&quot;);

            // The thread doing stuff has finished, so stop spinning
            spinner.interrupt();

            // Wait for the spinning thread to terminate
            spinner.join();

            System.out.println(&quot;The End.&quot;);
        }
        catch (InterruptedException e)
        {
            System.out.println(&quot;Interrupted&quot;);
        }
    }

    /**
     * Main method
     *
     */
    public static void main(String[] args)
    {
        SpinTest test = new SpinTest();

        test.doStuff();
    }
}
</pre>
<p>It&#8217;s a fairly basic implementation but it should be fairly simple to extend it for a real-world application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
