001    package org.codehaus.mojo.exec;
002    
003    /**
004     * Created by IntelliJ IDEA.
005     * User: dsmiley
006     * Date: Jan 19, 2007
007     * Time: 4:43:19 PM
008     * To change this template use File | Settings | File Templates.
009     */
010    public class MainUncooperative extends Thread
011    {
012        public static final String SUCCESS = "1(interrupted)(f)2(f)";
013    
014        public static void main( String[] args )
015            throws InterruptedException
016        {
017            Thread daemonThread = new MainUncooperative();
018            daemonThread.setDaemon( true );
019            daemonThread.start();
020            Thread.sleep( 1000 );
021            //returns to caller now
022        }
023    
024        final long SIMWORKTIME = 15*1000;//15 seconds of work which is going to be more than exec:java wants to wait
025    
026        public void run()
027        {
028            boolean interruptedOnce = false;
029            long startedTime = System.currentTimeMillis();
030            for(int lap = 1; true; lap++ )
031            {
032                long remainingWork = SIMWORKTIME - (System.currentTimeMillis() - startedTime);
033                if ( remainingWork <= 0 )
034                {
035                    break;//done
036                }
037                try
038                {
039                    System.out.print( lap );
040                    Thread.sleep(remainingWork);//simulates doing work
041                    System.out.print( "(done)" );
042                    break;
043                }
044                catch ( InterruptedException e )
045                {
046                    //We want to ensure this only gets reported once. It's possible depending on a race condition for
047                    // ExecJavaMojo.terminateThreads() to interrupt this thread a second time.
048                    if ( ! interruptedOnce )
049                    {
050                        System.out.print( "(interrupted)" );
051                    }
052                    interruptedOnce = true;
053    
054                    //be uncooperative; don't quit and don't set interrupted status
055                }
056                finally
057                {
058                    System.out.print("(f)");//we should see this if Thread.stop() is called
059                }
060            }
061        }
062    }