Java Foundation Eighth Day

Source: Internet
Author: User

/*




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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.