View Javadoc

1   package org.codehaus.mojo.exec;
2   
3   /**
4    * Created by IntelliJ IDEA.
5    * User: dsmiley
6    * Date: Jan 19, 2007
7    * Time: 4:43:19 PM
8    * To change this template use File | Settings | File Templates.
9    */
10  public class MainUncooperative extends Thread
11  {
12      public static final String SUCCESS = "1(interrupted)(f)2(f)";
13  
14      public static void main( String[] args )
15          throws InterruptedException
16      {
17          Thread daemonThread = new MainUncooperative();
18          daemonThread.setDaemon( true );
19          daemonThread.start();
20          Thread.sleep( 1000 );
21          //returns to caller now
22      }
23  
24      final long SIMWORKTIME = 15*1000;//15 seconds of work which is going to be more than exec:java wants to wait
25  
26      public void run()
27      {
28          boolean interruptedOnce = false;
29          long startedTime = System.currentTimeMillis();
30          for(int lap = 1; true; lap++ )
31          {
32              long remainingWork = SIMWORKTIME - (System.currentTimeMillis() - startedTime);
33              if ( remainingWork <= 0 )
34              {
35                  break;//done
36              }
37              try
38              {
39                  System.out.print( lap );
40                  Thread.sleep(remainingWork);//simulates doing work
41                  System.out.print( "(done)" );
42                  break;
43              }
44              catch ( InterruptedException e )
45              {
46                  //We want to ensure this only gets reported once. It's possible depending on a race condition for
47                  // ExecJavaMojo.terminateThreads() to interrupt this thread a second time.
48                  if ( ! interruptedOnce )
49                  {
50                      System.out.print( "(interrupted)" );
51                  }
52                  interruptedOnce = true;
53  
54                  //be uncooperative; don't quit and don't set interrupted status
55              }
56              finally
57              {
58                  System.out.print("(f)");//we should see this if Thread.stop() is called
59              }
60          }
61      }
62  }