Enclose the code snippet:
public class demothread3 {/** * @author Shepherd's queen * @param args * 2015-10-15 * simulates a cat and dog thread, sharing a bucket of water in a house. That is, the house is the target object of the thread. * when water is drunk cat and dog Enter death State */public static void main (string[] args) {house house = new house (); House.setwateramount ( 10 ); thread dog; //using thread to declare thread thread cat;dog = new thread ( house ); // dog and cat target objects are the same are house cat = new Thread ( house ), // dog.setname ("dog"); // change thread name, Make it the same as the parameter name Cat.setname ("Cat");d Og.start (); // boot thread cat.start ();}} Public class house implements runnable {private int wateramount;public void setwateramount (Int wateramount) {this.wateramount = wateramount;} @Overridepublic voId run () {while (True) {// currentthread () : returns a reference to the currently executing thread object. getname () returns the name of the thread. string name = thread.currentthread (). GetName (), if (Name.equals ("Dog")) {System.out.println ( name + " Water"); wateramount = wateramount - 2;} Else if ( name.equals ("Cat")) {System.out.println ( name + " water");wateramount = Wateramount -1;} System.out.println (" remaining : " + waterAmount ); Try {thread.sleep (+); // interval Enter interrupt status } catch (interruptedexception e) {// The exception is thrown when the thread is waiting, hibernating, or occupied during the activity or during the activity and the thread is interrupted. E.printstacktrace ();} if ( waterAmount <=0 ) {return;}}}}
Learning notes a simple implementation of 3:java threads