It 18 Palm Job _java Foundation Eighth Day _ multithreading

Source: Internet
Author: User

/*

5 cars passed through the caves in turn. Each car takes 10 seconds through a cave and is implemented using multithreading.

Idea: Create 5 threads, manipulate the same resource, Cave

The time to manipulate the resource is 10 seconds sleep (10*1000)

While a thread is executing, other threads cannot manipulate the resource. Sync synchronized () {}

*/


Class Bus extends Thread

{

private String name;

private static Object obj = new Object ();

Public Bus (String name)

{

THIS.name = name;

}

Public String Getbusname ()

{

return name;

}

public void Run ()

{

Synchronized (obj)

{

System.out.println (Thread.CurrentThread (). GetName () + "\ T" +this.getbusname () + "Crossing the cave!!" ");

try{

Thread.Sleep (10*1000);

}catch (Exception e) {}

System.out.println (Thread.CurrentThread (). GetName () + "\ T" +this.getbusname () + "has crossed the cave");

}

}

}


Class Threaddemo

{

public static void Main (string[] args)

{

Bus T1 = new bus ("one car");

Bus t2 = new Bus ("Car No. second");

Bus T3 = new Bus ("Car No. third");

Bus T4 = new bus ("Car No. Fourth");

Bus T5 = new bus ("Car No. Fifth");

T1.start ();

T2.start ();

T3.start ();

T4.start ();

T5.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.

Idea: Create two threads class, Bee class and Bear class, create a collection of accumulated honey number, when full 100, notify the bear can eat.

Note: If the honey count is not in the 100 honey, then the bear thread waits,

In the process of the bear eating bees, the bees wait

The CPU automatically chooses the thread execution from the lock pool waiting queue.


*/

Producers

Import java.util.*;

Class Beans extends Thread

{

Final static int MAX = 100;

List<integer> list;

String name;

Public Beans (list<integer> list,string name)

{

This.list = list;

THIS.name = name;

}

public void Run ()

{

int i = 1;

while (true)

{

Synchronized (list)

{

int size = List.size ();

if (Size<max)

{

List.add (New Integer (i));

System.out.println (name+ "Bee production" + i+ "Part honey!! ");

i + +;

List.notifyall (); //Wake up all threads in the waiting queue that belong to the current object and get the lock eligibility.

}

else//If the number of honey is greater than or equal to 100, inform the bear to eat

{

Try

{

List.wait ();//causes the current thread to enter the waiting queue and releases the lock, allowing other threads to enter the synchronization code block.

}

catch (Exception e) {}

}

}

}

}

}

Consumers

Class Bear extends Thread

{

List<integer> list;

String name;

Public Bear (list<integer> list,string name)

{

This.list = list;

THIS.name = name;

}

public void Run ()

{

while (true)

{

Synchronized (list)

{

int size = List.size ();

if (size>=100)

{

for (int i=0;i<size;i++)

{

List.remove (0);

}

SYSTEM.OUT.PRINTLN (name+ "ate 100 parts of honey!! -----------------");

List.notify ();//wakes up all threads in the waiting queue that belong to the current object and prepares to acquire locks.

}

Else

{

Try

{

List.wait ();

}

catch (Exception e) {}

}

}

}

}


}

Class ThreadDemo2

{

public static void Main (string[] args)

{

list<integer> list = new arraylist<integer> ();

Beans T1 = new Beans (list, "Lucy");

Beans t2 = new Beans (list, "Lili");

bears T3 = new Bear (list, "Xiong da");

T1.start ();

T2.start ();

T3.start ();

}

}


This article from "Rookie Achievement Data Road" blog, reproduced please contact the author!

It 18 Palm Job _java Foundation Eighth Day _ multithreading

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.