當前位置:編程學習大全網 - 編程軟體 - JAVA編程 進水和出水

JAVA編程 進水和出水

class InPipe extends Thread{

Object vault;

public InPipe(Object vault){

this.vault=vault;

}

public void run(){

synchronized(vault){

System.out.println("Water filling");

try{

Thread.sleep(300000);

}

catch(InterruptedException e){}

System.out.println("Filling complete");

vault.notify();

try{

vault.wait();

}catch(InterruptedException e){}

}

}

}

class OutPipe extends Thread{

Object vault;

public OutPipe(Object vault){

this.vault=vault;

}

public void run(){

synchronized(vault){

System.out.println("Water emptying");

try{

Thread.sleep(180000);

}

catch(InterruptedException e){}

System.out.println("Emptying complete");

vault.notify();

try{

vault.wait();

}catch(InterruptedException e){}

}

}

}

public class WaterPool {

public static void main(String[] args){

Object vault=new Object();

InPipe ip=new InPipe(vault);

OutPipe op=new OutPipe(vault);

ip.start();

op.start();

}

}

大概是這樣,註水和排水交替進行。

  • 上一篇:請問壹下,螺紋銑刀轉速壹般都打多少轉,可以壹次成型嗎?要不要分幾次進刀像車床上壹樣,如加工M30X3的內
  • 下一篇:% 在C語言裏是什麽意思?
  • copyright 2024編程學習大全網