/*
1.5 cars passed through the caves in turn. Each car takes 10 seconds through a cave and is implemented using multithreading.
Class Shan {
}
Class Car extends Thread {
Shan Shan;
String name;
Public Car (String name, Shan Shan) {
THIS.name = name;
This.shan = Shan;
}
public void Run () {
Synchronized (Shan) {
System.out.println ("Wo is" + name);
try {
Thread.Sleep (2000);
}catch (Exception e) {
}
}
}
}
Class Demo {
public static void Main (string[] arg) {
Shan Shan = new Shan ();
Car C1 = new Car ("1", Shan);
Car C2 = new Car ("2", Shan);
Car C3 = New Car ("3", Shan);
Car C4 = new Car ("4", Shan);
Car c5 = new Car ("5", Shan);
Car C11 = new Car ("one", Shan);
Car C12 = new Car ("N", Shan);
Car C13 = new Car ("Shan");
Car C14 = new Car ("+", Shan);
Car C15 = new Car ("Shan");
C1.start ();
C2.start ();
C3.start ();
C4.start ();
C5.start ();
C11.start ();
C12.start ();
C13.start ();
C14.start ();
C15.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.
Class Fengmi {
int nu;
}
Class Beam extends thread{
FENGMI FM;
Public Beam (fengmi FM) {
THIS.FM = FM;
}
public void Run () {
Synchronized (FM) {
while (true) {
System.out.println (++fm.nu);
if (fm.nu = = 100) {
fm.nu=100;
try {
Fm.notify ();
Fm.wait ();
} catch (Exception e) {
}
}
}
}
}
}
Class Bear extends thread{
FENGMI FM;
Public Bear (fengmi FM) {
THIS.FM = FM;
}
public void Run () {
while (true) {
Synchronized (FM) {
if (fm.nu = = 100) {
fm.nu = 0;
System.out.println ("Bear All Ate");
try {
Fm.notify ();
System.out.println ("Waiting in");
Fm.wait ();
} catch (Exception e) {
}
}
}
}
}
}
Class Demo {
public static void Main (string[] arg) {
FENGMI fm = new Fengmi ();
Beam Beam = new BEAM (FM);
Bear Bear = new bear (FM);
Beam.start ();
Bear.start ();
}
}
*/
Java Foundation Eighth Day