Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 6th, 2013  |  syntax: None  |  size: 0.94 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.Scanner;
  2.  
  3. public class Test {
  4.  
  5.     private static final Object LOCK = new Object();
  6.  
  7.     public static void main(String[] args) {
  8.         final Thread thread = new Thread() {
  9.             public void run() {
  10.                 while (true) {
  11.                     synchronized (LOCK) {
  12.                         System.out.println("Looped at: " + System.currentTimeMillis());
  13.                         try {
  14.                             LOCK.wait();
  15.                         } catch (Exception ignored) {
  16.                             ignored.printStackTrace();
  17.                         }
  18.                     }
  19.                 }
  20.             }
  21.         };
  22.         thread.start();
  23.         final Scanner in = new Scanner(System.in);
  24.         while (true) {
  25.             if (in.nextLine().equals("loop")) {
  26.                 synchronized (LOCK) {
  27.                     LOCK.notifyAll();
  28.                 }
  29.             }
  30.         }
  31.     }
  32.  
  33.  
  34. }
clone this paste RAW Paste Data