当前位置:网站首页>【线程通信】
【线程通信】
2022-06-29 03:09:00 【我会一直】
最粗暴的“文件共享”通信
通过文件共享
package com.fly.threadDome;
import java.nio.file.Files;
import java.nio.file.Paths;
/** * @Title: 线程通信 * @Author: Fly * @Date: 2020/8/30 - 13:50 * @Description: */
public class ThreadDome {
public static void main(String[] args) {
//线程1:写入数据
new Thread(()->{
try{
while (true){
Files.write(Paths.get("E:\\TestFile.txt"),
("当前时间"+String.valueOf(System.currentTimeMillis())).getBytes());
Thread.sleep(1000L);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
//线程2:读取数据
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();
}
}
文件TestFile.txt记录最后一次时间
控制台while(true)每一次读取最新文件的内容
变量共享
利用内存的变量区域
public static String content="空";
private static void variableThread() {
//线程1:写入数据
new Thread(()->{
try{
while (true){
content=("当前时间:"+String.valueOf(System.currentTimeMillis()));
Thread.sleep(1000L);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
//线程2:读取数据
new Thread(()->{
try{
while (true){
Thread.sleep(1000l);
System.out.println(content);
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
线程协作-JDK API
老生常谈的话题,就不在这里贴代码了
- JDK中对于需要多线程协作完成某一任务的场景,提供了对应API支持
- 多线程协作的典型场景是:生产者-消费者模型。(线程阻塞、线程唤醒)
- 示例:线程A去买包子,没有包子,则不再执行。线程B生产出包子,通知线程A继续执行
边栏推荐
- Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
- For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.
- 数据离散化
- 18. `bs object Node name next_ Sibling` get sibling nodes
- [leetcode daily question] number of schemes to reconstruct a tree
- Jerry's watch pause [chapter]
- Is the account opening of GF Securities really safe and reliable
- On the fact that lambda expressions cannot handle recursion
- Square root of X
- 1110: 最近共同祖先(函数专题)
猜你喜欢

STM32L4系列单片机ADC通过内部参考电压精确计算输入电压
![Jerry's monitoring alarm clock [chapter]](/img/b7/a5ca7a79af51bd79e4f5c1322b72ff.jpg)
Jerry's monitoring alarm clock [chapter]

EMC、EMI、EMS的關系

For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.

Analytic hierarchy process (AHP)

解决allegro中测量距离时,点击一个点后光标闪烁的问题
![[leetcode daily question] number of schemes to reconstruct a tree](/img/82/2ed8c9747f9fa36fde4f18cf8966be.jpg)
[leetcode daily question] number of schemes to reconstruct a tree
![[线性代数] 1.2 全排列和对换](/img/04/18fc358c6c426e10c8598bcee9cd43.png)
[线性代数] 1.2 全排列和对换

Today's sleep quality record 82 points

In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day
随机推荐
手机开户股票开户安全吗?开户很难么?
测试入门——集成测试
LabVIEW generate application (exe) and installer
Analytic hierarchy process (AHP)
Codeforces Round #771 (Div. 2) ABC
Three methods of time series prediction: statistical model, machine learning and recurrent neural network
Informatics Olympiad 1361: Produce
allegro 设计中显示网络飞线或关闭网络飞线的方法
2022-2028 global bubble CPAP system industry survey and trend analysis report
微信小程序安全登录,必须先校验微信返回openid,确保信息唯一性。
priority_ Understanding of queue
层次分析法(AHP)
2022年启牛学堂证券开户安全的嘛?
Only in the past four years, Microsoft finally gave it up!
Relationship between EMC, EMI and EMS
sql连续登录问题
Movement state change of monitoring device of Jerry's watch [chapter]
Informatics Olympiad all in one 1361: production | Luogu P1037 [noip2002 popularization group] production
Leetcode daily question - 324 Swing sort II
Getting started with testing - integration testing