Advertisement
Guest User

Java Thread

a guest
Oct 25th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1.  
  2. public class Main implements Runnable {
  3.  
  4.     private boolean running = true;
  5.    
  6.     public void stopExecution ()
  7.     {
  8.         running = false;
  9.     }
  10.    
  11.     public void run() {
  12.         do
  13.         {
  14.            
  15.             System.out.println("Hello from a thread!");
  16.             try {
  17.                 Thread.sleep(1000);
  18.             } catch (InterruptedException e) {}
  19.            
  20.         } while( running );
  21.     }
  22.  
  23.     public static void main(String args[]) {
  24.         Main main = new Main();
  25.         Thread thread = new Thread( main );
  26.         thread.start();
  27.        
  28.         //Call stopExecution(); at some point, e.g. key release
  29.         try {
  30.             Thread.sleep(2500);
  31.         } catch (InterruptedException e) {}
  32.        
  33.         main.stopExecution();
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement