当前位置:网站首页>[thread communication]
[thread communication]
2022-06-29 03:10:00 【I will always】
【 Thread communication 】
The roughest “ File sharing ” signal communication
Through file sharing 
package com.fly.threadDome;
import java.nio.file.Files;
import java.nio.file.Paths;
/** * @Title: Thread communication * @Author: Fly * @Date: 2020/8/30 - 13:50 * @Description: */
public class ThreadDome {
public static void main(String[] args) {
// Threads 1: Write data
new Thread(()->{
try{
while (true){
Files.write(Paths.get("E:\\TestFile.txt"),
(" current time "+String.valueOf(System.currentTimeMillis())).getBytes());
Thread.sleep(1000L);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
// Threads 2: Reading data
new Thread(()->{
try{
while (true){
Thread.sleep(1000l);
byte[] allBytes =Files.readAllBytes(Paths.get("E:\\TestFile.txt"));
System.out.println(new String(allBytes));
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
}
file TestFile.txt Record the last time 
Console while(true) Read the contents of the latest file every time 
Variable sharing
Use the variable area of memory 
public static String content=" empty ";
private static void variableThread() {
// Threads 1: Write data
new Thread(()->{
try{
while (true){
content=(" current time :"+String.valueOf(System.currentTimeMillis()));
Thread.sleep(1000L);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
// Threads 2: Reading data
new Thread(()->{
try{
while (true){
Thread.sleep(1000l);
System.out.println(content);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
Thread collaboration -JDK API
A trite topic , Don't post code here
- JDK For the scenarios that need multi-threaded cooperation to complete a task , The corresponding API Support
- A typical scenario for multithreading collaboration is : producer - Consumer model .( Thread blocking 、 Thread wake up )
- Example : Threads A Go to buy steamed buns , No bun , No more execution . Threads B To produce a bun , Notification thread A Carry on
边栏推荐
- 18. `bs对象.节点名.next_sibling` 获取兄弟节点
- Setting alarm mode of Jerry's watch [chapter]
- 线程池是什么老鸡?
- FPGA (VIII) RTL code IV (basic circuit design 1)
- 2022-2028 global low carbon concrete industry research and trend analysis report
- allegro设置网络飞线以及网络颜色的方法
- Pat class a a1057 stack
- PMP Business Analysis Overview
- For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.
- 50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
猜你喜欢

Analytic hierarchy process (AHP)

图扑软件智慧能源一体化管控平台

Allegro's method of setting network flying line and network color

深入解析 Apache BookKeeper 系列:第三篇——读取原理
![[线性代数] 1.1 二阶与三阶行列式](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[线性代数] 1.1 二阶与三阶行列式
![[Shangshui Shuo series] the simplest subtitle configuration](/img/22/7e0bcb489d0f2d35c7fe3248ad3419.png)
[Shangshui Shuo series] the simplest subtitle configuration
![[線性代數] 1.1 二階與三階行列式](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[線性代數] 1.1 二階與三階行列式

问题——adb shellerror: insufficient permissions for device: verify udev rules.

蓝桥杯2022初赛——扫雷

Overview of PMP project management
随机推荐
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre
Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
兰宝传感科技冲刺科创板:年营收3.5亿 许永童家族色彩浓厚
map,set用pari作为key值,如何定义
[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!
Nvisual helps integrators transform
Getting started with testing - integration testing
2022-2028 global pneumatic test probe industry survey and trend analysis report
Is it safe for qiniu school to open an account in 2022?
Matlab exercises - image drawing exercises
层次分析法(AHP)
Bluebridge cup 2022 preliminaries - minesweeping
今日直播|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭
In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
图扑软件智慧能源一体化管控平台
Bluetooth solution | Lenz technology Amazon direct connected magic lantern solution
Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color
Pat class a a1057 stack
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
Merge sort