Java Threading Interaction

Source: Internet
Author: User
Tags class manager

Java Threading Interaction

Threads run independently, but when multiple threads need to access a shared resource simultaneously, a thread's synchronization (synchronized) is used, which is equivalent to a lock on the shared object resource, with only one thread acquiring the object lock at a time. The minimalist

Single example: Sell movie tickets, several windows (threads) at the same time to sell movie tickets, each other threads are mutually exclusive, only one thread at a time to access the movie ticket (this object). But there is also a producer-consumer process, then need to use the thread of interaction (Wait () and notify ()/notifyall (), wait-wake), such as writing a production of steamed buns and sell steamed buns, each production of buns to 100 after the notice to sell buns, when buns to 0 o'clock, Inform the production of steamed buns.

1: Define a Bun object,

public class manager{

The number of private int count;//buns

Private String name;//The name of the bun

The maximum number of public int maxcount=100;//Buns

Public boolean k;//is used to determine whether the bun is still, the default is False, with true for the bun has, notify consumption, with false to represent the bun did not notify the production.

constructor function

Public Manager (String name,int count) {

This.name=name;

This.count=count;

}

public int GetCount () {

return this.count;

}

public int setcount (int y) {

return this.count+=y;

}


}

Define a thread that produces buns

public class Producer implements runnable{

Private Manager ma;//defines a shared object

Public Producer (Manager Ma) {

this.ma=ma;//guarantees that the same shared object is passed in

}

public void Run () {

Because it is not known whether the production line runs first or the consumer thread first runs, you must first determine

while (true) {

Infinite circulation, guaranteed to be produced when sold out

Synchronized (MA) {//Pass object lock

if (ma.k==true) {//status is true, the bun already has, wait, notify consumption.

Ma.wait ()////Here you need to add try-catch, once the number of buns has, then the consumer thread to notify (), the current thread waits to wake up.

}

if (ma.getcount<100) {//When the number of buns is less than 100, the production

Ma.setcount (5);

System.out.println ("produced 5, Total is;" +ma.getcount ());

}

The number of ma.k=true;//buns is more than 100, so change the status

Ma.notify ();//Notify consumers to consume


}

}

}

}


Define a consumer thread

public class Custer implements runnable{

Private Manager Ma;

Public Custer (Manager Ma) {

This.ma=ma;

}

public void Run () {

while (true) {

Synchronized (MA) {

if (Ma.k==false) {

Ma.wait ();//When the status of the bun is false, the consumption needs to wait for production.

}

if (Ma.getcount () >0) {

Ma.setcount (-2);

System.out.println ("Bun has consumed 2, left:" +ma.getcount ());

}

ma.k=false;//buns have been consumed and need to be produced.

Ma.notify ();

}

}

}


}

Define a main thread class and start running

public class testdemo{

public static void Main (string[] args) {

Manager ma=new Manager ("Bun", 0);//define a Bun object

Producer p=new Producer (MA);//Create production Object

Custer c=new Custer (MA); Create Consumer objects

Thread t1=new thread (p);//Create production line Object

Thread t2=new thread (c); Create Consumer thread objects


T1.start ();//Start production line

T2.start (); Start consumer Thread

}

}

This article is from the Java Learning Log blog, so be sure to keep this source http://20150523.blog.51cto.com/10261028/1654647

Java Threading Interaction

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.