mod11ans.lst

来自「Mcgraw-Hill - Java 2 - A Beginner S Guid」· LST 代码 · 共 55 行

LST
55
字号
listing 1
// Make the TickTock class actually keep time. 
  
class TickTock {  
  
  synchronized void tick(boolean running) {  
    if(!running) { // stop the clock  
      notify(); // notify any waiting threads  
      return;  
    }  
  
    System.out.print("Tick ");  
 
    // wait 1/2 second 
    try { 
      Thread.sleep(500); 
    } catch(InterruptedException exc) { 
      System.out.println("Thread interrupted.");  
    } 
        
    notify(); // let tock() run  
    try {  
      wait(); // wait for tock() to complete  
    }  
    catch(InterruptedException exc) {  
      System.out.println("Thread interrupted.");  
    }  
  }  
  
  synchronized void tock(boolean running) {  
    if(!running) { // stop the clock  
      notify(); // notify any waiting threads  
      return;  
    }  
  
    System.out.println("Tock");  
 
    // wait 1/2 second 
    try { 
      Thread.sleep(500); 
    } catch(InterruptedException exc) { 
      System.out.println("Thread interrupted.");  
    } 
 
    notify(); // let tick() run  
    try {  
      wait(); // wait for tick to complete  
    }  
    catch(InterruptedException exc) {  
      System.out.println("Thread interrupted.");  
    }  
  }  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?