Three ways of data transmission between threads in Java and source code display

Jiedao jdon 2021-05-04 14:50:41
ways data transmission threads java

When communicating or signaling between threads, the first thing that comes to mind is java.lang.Object Class method :wait,notify and notifyAll. This is the most basic and widely accepted concept , But this blog is not about that .

JVM The thread running on the instance lacks a strong default model , As in the UNIX It's the same as interactive communication in , Although there are some third-party frameworks that can help us achieve this intention and work well in the corner case , This blog is especially for the use of Java Three local concepts in to communicate between threads : Cyclic Barrier Circulation barrier ,Count Down Latch Countdown latch and Pipes The Conduit .

1.Cyclic Barrier

Expand java.util.concurrent.CyclicBarrier Class for data transfer between threads . It's basically a synchronization technology , It allows the program counter of each thread to stop at a common barrier point in each thread , here , You can use shared state to transfer data , Then the thread can continue accordingly .

The demo sample code will use the producer - Consumer model . Parent thread activation class.Producer and class.Consumer Thread start . When a producer reads data from a file , Consumers first reach the public barrier and stop . The producer thread stores the results in the blocking queue of the parent thread . Once the producer reaches the barrier point , At this point, producers and consumers will release barriers , Consumers retrieve data from the queue and continue .

A good use case for this model is that the input of a thread depends on the output of another thread . under these circumstances , The consumer thread can perform all the necessary startup and wait at the barrier to receive input data . According to this github Link to complete implementation of circular barrier .

 Logical template :
class.Producer thread 
step2: Do the necessary calculations and logic
step3: Write all data to the blocking queue
step4: Reaching the circulation barrier point , If all threads reach the barrier point - > continue
step 5: Carry out cleaning and / Or end
class.Consumer thread 
step2: Start any logical processing of data independent of the producer
step3 Reach the circulation barrier , If all threads reach the barrier - > continue
step4: Remove data from the queue
step5: Processing data , Clean up and finish

2.Count Down Latch

This is a synchronization technique used in the wait and arrive model , But it's here as a producer - Consumer way to achieve .

The producer thread reached a point , Put on a sign and move on immediately , Unlike the loop barrier, the producer thread will pause here , The consumer thread stops at some point and starts counting the program counter , Wait until the producer thread is marked with a flag before continuing to run .

This model can be modified for inter thread communication . Before reaching this point, the producer thread pushes some data to the blocking queue and raises a flag , Waiting consumers track all of these signs and wait to get the desired sign number , Then it starts to extract data from the blocking queue .

The demo code will have two generator threads , One consumer thread and one size 2 Bounded blocking queue for . Each producer thread reads a different input file , Then transfer it to the producer thread , The producer thread prints it to the screen . A good use case is when database objects in the server are only written in bulk , It acts as the user and waits for the default number . Many producer threads push their data into queues , Raises the flag and continues without waiting . According to this github Link to complete implementation of countdown latch .

 Logical template :
class.Producer1 and class.Producer2
step1: start-up  
step2: Do the necessary calculations and logic
step3: Write all the data to be transmitted to the blocking queue
step4: Mark and continue without waiting
step5: Do some logic and end
step1: start-up
step2: Do some logic and wait for a point to receive data
step3: Tracking signs / Latch , If the quantity reaches - > continue
step4: Pull data out of the queue step5 
: Use data and end

3. come from Of Pipes

 class.PipedReader and class.PipedWriter  Allows direct communication with other threads at defined points . Producers and consumers have to visit a point together , Production starts pushing data in blocks , Consumers waiting for the pipeline to open begin extracting data .

It's relatively easy to understand and implement the concept .pipe Not performing well in the field environment , So it has been abandoned in the developer community . Another major drawback is that it only supports one-to-one transfers . There can only be one producer and one consumer . A good use case is when it's used to guide data in a one-time task , It's related to the newer java.nio Use bags together . Focus on pipeReader and pipeWriter Fully implemented github link .

 Logical template
class.Producer thread 
step1: start-up
step2: Do some logic and get to the loop of the execution pipeline
step3: Push the data into the stream and continue when it's done
step4: Do some logic and end
class.Consumer thread 
step1: start-up
step2: Do some logic and get to the pipeline reader point
step3: Once the pipeline has a value, pull the data and store it
step5: Read all the data completely , Do some logic and end

The three ways of communication between heterogeneous threads are only modified when the author completes the work / Efforts to expand the concept of native . As understood in the use case scenario , Countdown latch has a wider range of availability and can be widely used .

One of the main hints is the exception , Provide an obstacle or latch with an underlying exception , The exception handler also has the opportunity to retry the data transfer . These concepts are widely implemented , This blog tries to make producers - Consumer comments and github Examples are put in .

本文为[Jiedao jdon]所创,转载请带上原文链接,感谢

  1. Help, Java how to get all the current processes of the system
  2. Has anyone ever used JMeter or written tests in httpUnit????
  3. Living in a mountain village in late spring
  4. Partridge day, spring of HKUST
  5. JavaScript异步编程4——Promise错误处理
  6. 海康摄像SDK开发笔记(一):海康威视网络摄像头SDK介绍与模块功能
  7. JavaScript asynchronous programming 4 -- promise error handling
  8. Haikang video SDK development notes (1): introduction and module functions of Hikvision webcam SDK
  9. JOP:用于FPGA的嵌入式实时系统中的Java优化处理器内核
  10. Spring Boot源码:使用MongoDB MongoTemplate公开REST在几分钟内实现CRUD功能
  11. Spring Boot应用程序事件教程 - reflectoring
  12. 带有Resilience4j断路器的Spring云网关 - rome
  13. 经验分享:Apache Kafka的缺点与陷阱 - Emil Koutanov
  14. 通过Spring Boot Webflux实现Reactor Kafka
  15. 从Java 8升级到Java 11应该注意的问题
  16. Jop: Java optimized processor core for FPGA embedded real time system
  17. Spring boot source code: use mongodb mongotemplate to open rest to realize crud function in a few minutes
  18. Spring boot application event tutorial - reflecting
  19. Spring cloud gateway with resilience4j circuit breaker - ROM
  20. Experience sharing: shortcomings and pitfalls of Apache Kafka - Emil koutanov
  21. Realization of reactor Kafka through spring boot Webflux
  22. RPC框架设计----Socket与I/0模型
  23. Problems in upgrading from Java 8 to Java 11
  24. RPC framework design -- socket and I / 0 model
  25. RPC框架设计----I/0模型
  26. RPC framework design: I / 0 model
  27. RPC框架设计----NIO编程缓冲区Buffer
  28. RPC框架设计----NIO编程缓冲区Buffer
  29. RPC framework design -- NiO programming buffer
  30. RPC framework design -- NiO programming buffer
  31. Java多线程基础
  32. Java multithreading Foundation
  33. 码农飞升记-00-Java发展历程
  34. Development history of coder-00-java
  35. 码农飞升记-00-Java发展历程
  36. Development history of coder-00-java
  37. Spring and Autumn Moon
  38. Node.js与Spring Boot比较? - Ryan Gleason
  39. Spring WebFlux的明显陷阱 - ŁukaszKyć
  40. Spring创始人Rod大叔对YAML的真实想法
  41. Compare node.js with spring boot- Ryan Gleason
  42. Obvious pitfalls of spring Webflux- Ł ukaszKy ć
  43. Spring founder uncle rod's real thoughts on yaml
  44. 码农飞升记-02-OracleJDK是什么?OracleJDK的版本怎么选择?
  45. What is manong feisheng-02-oracle JDK? How to choose the version of Oracle JDK?
  46. Spring tide surging Xinanjiang
  47. Linux内核软中断
  48. Linux kernel soft interrupt
  49. Linux内核软中断
  50. Linux kernel soft interrupt
  51. Java multithreading Foundation
  52. The construction of Maven private library nexus
  53. I / O stream in Java
  54. JDK 16:Java 16的新功能 - InfoWorld
  55. 在Java中本地进行线程间数据传输的三种方式和源码展示
  56. jdon导致cpu 99%最后tomcat死掉---banq给予回复
  57. 用领域事件模拟AOP注入
  58. JDK 16: new function of Java 16 - InfoWorld
  59. Cartoon: from JVM lock to redis distributed lock
  60. Spring 3.1 终于加入了Cache支持