IntroductionProducer and consumer issues are a classic problem in threading models: producers and consumers share the same storage space during the same time period , as shown in, producers store data in space, and consumers access data, If not coordinated, the following conditions may occur:Producer Consumer ChartStorage space is full, and producers occupy it, consumers wait for producers to give up space to remove products, producers wait for consumers to consume products, thereby adding produ
(interruptedexception E1) { - e1.printstacktrace (); Wu } - } AboutE e = list.get (0); $List.remove (0); -System.out.println (Thread.CurrentThread (). GetName () + "consumer Products"); - Try { -Thread.Sleep (1000); A}Catch(interruptedexception E1) { + e1.printstacktrace (); the } - List.notifyall (); $ returne; the } the } the the /** - * Determine if the current warehouse is empty in * the * @return the
to remove products, producers wait for consumers to consume products, thereby adding products to the space. Wait for each other, thus the deadlock occurs.Three ways in which Java solves threading models1. Wait () and notify ()Import Java.util.linkedlist;public class Producerconsumer {private linkedlist2, await () and signal (), the way the thread locksPackage Sort;import Java.util.linkedlist;import Java.util.concurrent.locks.condition;import Java.
Input implements Runnable { Privateperson person; PublicInput (person person) { This. person = person; } @Override Public voidRun () {intx =0; while(true) {synchronized (person) {if(Person.flag) {if(x = =0) {//judgment interval to produce different peoplePerson.name ="Tom"; Person.sex ="Male"; }Else{Person.name ="Zhang San"; Person.sex ="female"; } x = (x +1) %2;//interval to produce different peoplePerson.flag =false;//Put it once, take it oncePerson.notify ();//wake-up thread of a person}E
--; System.out.println ("Consumers are consuming Xiaomi mobile phones sell 1, the current number of products:" +con.currentnum+ "department"); }Else if(Con.currentnum = = 0) {//If you don't have a cell phoneSystem.out.println ("The phone has been bought, consumers stop spending, is waiting for production ..."); Try{con.wait ();//suspend consumption and release locks}Catch(interruptedexception e) {e.printstacktrace (); } }//else if}//syn Try{Thread.Sleep (14
Use Linkedblockingqueue in Java to implement producer, consumer mode
Linkedblockingqueue implementation is thread-safe, which realizes FIFO (first-in first out) and other characteristics. is the first choice as producer consumers, Linkedblockingqueue can specify capacity, or can not be specified, not specified, the default is the largest integer.max_value, which
Label:First, the environment
One Centos6.5 console
Mongo 3.0
kafka_2.11-0.8.2.1
Storm-0.9.5
Zookeeper-3.4.6
Java 1.7 (later because the jar packaged on Mac is not run by the 1.8 compilation, instead Java 1.8)
Other environment Temporary
Second, the operation starts
Start ZookeeperVerify that the configuration is correct, and that the configuration is self-searching
{//produce an apple and put it in the box +box.buff[(++box.putpoint)% box.boxlength] = 1; Abox.boxcount++; theSystem.out.println ("Put in an apple"); +SYSTEM.OUT.PRINTLN ("Total apples:" +box.boxcount); - box.notify (); $ Try { $Thread.Sleep (1000); -}Catch(interruptedexception e) { - e.printstacktrace (); the } -}//ElseWuyi the } - } Wu - } About } $ - //consumption of apples, consumers - classCus
Import Java.util.linkedlist;import java.util.queue;import java.util.random;/** * Simple Java program to demonstrate how T o use Wait, notify and Notifyall () * method in Java by solving producer consumer problem. * * @author Javin Paul */public class Producerconsumerinjava {public static void main (String args[]) {SYSTEM.O Ut.println ("How to use Wait and
IntroductionProducer and consumer issues are a classic problem in threading models: producers and consumers share the same storage space during the same time period , as shown in, producers store data in space, and consumers access data, If not coordinated, the following conditions may occur:Producer Consumer ChartStorage space is full, and producers occupy it, consumers wait for producers to give up space to remove products, producers wait for consumers to consume products, thereby adding produ
Package Com.subject01;import Java.util.priorityqueue;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * condition is only present in Java 1.5, It is used instead of the traditional object's Wait (), notify () to implement inter-threading collaboration, compared to wait (), notify () with object, * using Condition's await (), signal () This approach enables inter-thre
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.