Inlet class inflow implements runnable{//water object water wat;public inflow (water w) {This.wat = W;} @Overridepublic void Run () {//Water inlet while (true) {synchronized (WAT) {while (true) {if (Wat.count >=) {wat.notify (); try {wat.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} try {//Sleep thread thread.sleep (+),} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} wat.count++; System.out.println ("Now in water, water:" + Wat.count);}}}} Effluent class outflow implements Runnable{water Wat;public outflow (water w) {This.wat = W;} @Overridepublic void Run () {//Effluent while (true) {//Line lock synchronized (WAT) {while (true) {if (wat.count <= 0) {wat.notify (); try {wat.wait ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} wat.count--; System.out.println ("Is now in water, the remaining amount is:" + Wat.count);}}}} Class Water{int count = 50;} public class Demo5{public static void main (string[] args) {//Create water object water water = new water ();//Water object inflow in = new inflow (water);//Drain Object Outflo W out = new outflow (water),//thread thread = new thread (in); Thread the = new thread (out);//Open thread Thread.Start (); The.start ();}}
Threads in Java to solve incoming and outgoing water problems