This is a classic example of thread synchronization, the source code is as follows:[Java]View Plaincopy
"FONT-SIZE:16PX;"> Packagedemo.thread;
/**
* Classic producer and consumer issues: producers constantly store their products in warehouses and consumers consume products from warehouses.
* Both producers and consumers can have a number of. Storage
In the last article we analyzed the metadata update mechanism, which involves a problem, that is, sender how to communicate with the server, that is, the network layer. Like many Java projects, the Kafka client's network layer is also used for Java NIO, which is then encapsulated in the above layer.
Let's take a look at the section between the sender and the serv
In the previous producer consumer program, the data produced by the producer at a time can be read multiple times by the consumer. Obviously, this does not meet our requirements. In this regard, the above procedures are improved:
1) The producer produces data once and the consumer obtains the data once;
2) A bucket can only store one pair of data
We will use the
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 mainly used put and take method, The put meth
A small Java thread example (like train ticket sales), java thread
Public class MyThread extends Thread {private static int ticket = 100; public void run () {for (int I = 0; I
Similar to the sales of train tickets, different windows sell the same ticket. Two windows cannot sell the same ticket.
JAVA five windows sell
queue is often busy, and the other queue is very easy. RABBITMQ does not know that these are still evenly distributed messages.The reason for this is that RABBITMQ only forwards messages when the message arrives at the exit of the queue, and it does not care about the number of messages that have not reached the message consumer. It just blindly sends odd messages to a consumer, even to another consumer.Th
The Production and Consumption thread type is a good example of understanding multithreading. In fact, it should be accurate to say that it is the "producer-consumer-warehouse" model. when it leaves the warehouse, the producer and consumer model becomes unconvincing.
For this model, the following points should be clarified:
1. The producer only produces when the
Java Operations Redis Simple example
Beginner Redis, in the Java language and environment to complete the Redis learning. First, the official website downloads the source code, compiles, installs, modifies the configuration file redis.conf three items: 1.Comment out bind 127.0.0.1 2.Daemonize no change to daemonize Yes 3. Protected-mode Yes to Protected-mode no
Typical Example of Java thread learning-reader demonstration
Typical Example of Java thread learning-reader demonstration
The most typical example of Java Thread learning-the reader, mainly uses the Thread knowledge as follows:
-T
properties, byte[] body) throws IOException {System.out.println ("recv message:" + new STR ing (body)); try {TimeUnit.SECONDS.sleep (1); } catch (Interruptedexception e) {e.printstacktrace (); } channel.basicack (Envelope.getdeliverytag (), false); } }; Channel.basicconsume (queue_name, consumer); After waiting for the callback function to complete, close the resource
;CONSUMERTAG,NBSP;ENVELOPENBSP;ENVELOPE,NBSP;AMQP. Basicpropertiesproperties,byte[]body) throwsIOException{Stringmessage =newstring (body, "UTF-8"); System.out.println ("[x]Received" "+message+" ");}; Channel.basicconsume (Queue_name,true,consumer);}}Run the message sender and the message receiver separately, and you can see the real-time situation from the RABBITMQ console.650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/2A/wKioL1YWY_Cz1
implementation of CONCURRENTHASHMAP, the resources can be segmented processing, you can skillfully avoid multi-threaded resource requisition, so you can divide the list into different segments, to different threads to handle, the code is as follows:Package Tool;import Java.util.list;import Java.util.concurrent.brokenbarrierexception;import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.atomic.atomicinteger;public Class Mutisegmentmutithread{private static Atomicinteger lock = ne
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.