5 cars passed through the caves in turn. Each car takes 10 seconds through a cave and is implemented using multithreading.
Class Cave{//private Boolean deng=true;//public synchronized Getdeng () {//if (deng=true) {//}//}}class Car extends Thread {Private String name;private Cave cave;public Car () {System.out.println ("car^ ^^ ^^ ^^ ^");} Public Car (String name,cave Cave) {this.name=name;this.cave=cave;} public void Run () {synchronized (cave) {System.out.println (name+ "into the Hole"); Try{thread.sleep (1000); System.out.println (name+ "Out of the Hole");} catch (Exception e) {}}}}class cavedemno{public static void Main (string[] args) {Cave cave=new Cave (); Car c1=new car ("Car1", Cave); Car c2=new car ("Car2", Cave); Car c3=new car ("Car3", Cave); Car c4=new car ("Car4", Cave); Car c5=new car ("Car5", Cave); C1.start (); C2.start (); C3.start (); C4.start (); C5.start ();}}
2. Use multithreading to simulate the relationship between bees and bears.
Bees are producers and bears are consumers. Bees produce honey is a cumulative process, the bear to eat honey is a batch (100 eat) process.
Notify the other party using the notification method between the producer and the consumer. Note that there is no deadlock phenomenon.
/*
* * Use multithreading to simulate the relationship between bees and bears.
* * Bees are producers, bears are consumers. Bees produce honey is a cumulative process, the bear to eat honey is a batch (100 eat) process.
* * Notify the other party using the notification method between the producer and the consumer. Note that there is no deadlock phenomenon.
*/
/*
* * Use multithreading to simulate the relationship between bees and bears.
* * Bees are producers, bears are consumers. Bees produce honey is a cumulative process, the bear to eat honey is a batch (100 eat) process.
* * Notify the other party using the notification method between the producer and the consumer. Note that there is no deadlock phenomenon.
*/
import java.util.arraylist;import java.util.list;class bee extends thread{final static int max=100;java.util.list<integer> list; String name;public bee (java.util.list<integer> list,string name) {this.list=list; This.name=name;} Runpublic void run () {Int i=1;while (true) {//Synchronizes the collection synchronized (list) {int size=list.size ( ); if (Size >= max) {try{list.wait ();} catch (exception e) { }}else{//Not enough 100 to produce Honey List.add (New integer (i)); System.out.println ("Production honey:" +i); I++;list.notifyaall ();}} Yield ();}}} Bear Class class bear extends thread{java.util.list<integer> list; String name;public bear (java.util.list<integer> list,string name) {this.list=list; This.name=name;} Eat Honey Public void run () {while (true) {synchronized (list) {//) synchronizes the collection int size=list.size (); if (size <=max) {try{list.wait ();} catch (exception e) {}}ELSE{INTEGER&NBSP;I=LIST.Remove (99);//Finish System.out.println ("Bear Eat Honey:" +i); List.notifyall ();} Yield ();}}} Class beardemo{public static void main (String[] args) {java.util.List<Integer> list=new java.util.ArrayList<Integer> (); Bee bee=new bee (list, "bee"); Bear bear=new bear (list, "Bear"); Bee.start (); Bear.start ();}}
Big Data 8th day job