How can two threads communicate with each other?

amgang 2003-12-30 12:12:33
How can two threads communicate with each other?11.
...全文
86 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
amgang 2003-12-30
  • 打赏
  • 举报
回复
能否仔细说一下再,多谢!!
lionqun 2003-12-30
  • 打赏
  • 举报
回复
use PipedInputStream and PipedOutputStream
amgang 2003-12-30
  • 打赏
  • 举报
回复
thanks a lot
wangzhanhai 2003-12-30
  • 打赏
  • 举报
回复
这个程序的作用是什么意思?
我看不懂
lionqun 2003-12-30
  • 打赏
  • 举报
回复
很长的例子,不过结构清晰(摘于Core Java 卷2)

1. import java.util.*;
2. import java.io.*;
3.
4. /**
5. This program demonstrates how multiple threads communicate
6. through pipes.
7. */
8. public class PipeTest
9. {
10. public static void main(String args[])
11. {
12. try
13. {
14. /* set up pipes */
15. PipedOutputStream pout1 = new PipedOutputStream();
16. PipedInputStream pin1 = new PipedInputStream(pout1);
17.
18. PipedOutputStream pout2 = new PipedOutputStream();
19. PipedInputStream pin2 = new PipedInputStream(pout2);
20.
21. /* construct threads */
22.
23. Producer prod = new Producer(pout1);
24. Filter filt = new Filter(pin1, pout2);
25. Consumer cons = new Consumer(pin2);
26.
27. /* start threads */
28.
29. prod.start();
30. filt.start();
31. cons.start();
32. }
33. catch (IOException e){}
34. }
35. }
36.
37. /**
38. A thread that writes random numbers to an output stream.
39. */
40. class Producer extends Thread
41. {
42. /**
43. Constructs a producer thread.
44. @param os the output stream
45. */
46. public Producer(OutputStream os)
47. {
48. out = new DataOutputStream(os);
49. }
50.
51. public void run()
52. {
53. while (true)
54. {
55. try
56. {
57. double num = rand.nextDouble();
58. out.writeDouble(num);
59. out.flush();
60. sleep(Math.abs(rand.nextInt() % 1000));
61. }
62. catch(Exception e)
63. {
64. System.out.println("Error: " + e);
65. }
66. }
67. }
68.
69. private DataOutputStream out;
70. private Random rand = new Random();
71. }
72.
73. /**
74. A thread that reads numbers from a stream and writes their
75. average to an output stream.
76. */
77. class Filter extends Thread
78. {
79. /**
80. Constructs a filter thread.
81. @param is the output stream
82. @param os the output stream
83. */
84. public Filter(InputStream is, OutputStream os)
85. {
86. in = new DataInputStream(is);
87. out = new DataOutputStream(os);
88. }
89.
90. public void run()
91. {
92. for (;;)
93. {
94. try
95. {
96. double x = in.readDouble();
97. total += x;
98. count++;
99. if (count != 0) out.writeDouble(total / count);
100. }
101. catch(IOException e)
102. {
103. System.out.println("Error: " + e);
104. }
105. }
106. }
107.
108. private DataInputStream in;
109. private DataOutputStream out;
110. private double total = 0;
111. private int count = 0;
112. }
113.
114. /**
115. A thread that reads numbers from a stream and
116. prints out those that deviate from previous inputs
117. by a threshold value.
118. */
119. class Consumer extends Thread
120. {
121. /**
122. Constructs a consumer thread.
123. @param is the input stream
124. */
125. public Consumer(InputStream is)
126. {
127. in = new DataInputStream(is);
128. }
129.
130. public void run()
131. {
132. for(;;)
133. {
134. try
135. {
136. double x = in.readDouble();
137. if (Math.abs(x - oldx) > THRESHOLD)
138. {
139. System.out.println(x);
140. oldx = x;
141. }
142. }
143. catch(IOException e)
144. {
145. System.out.println("Error: " + e);
146. }
147. }
148. }
149.
150. private double oldx = 0;
151. private DataInputStream in;
152. private static final double THRESHOLD = 0.01;
153. }
源码链接: https://pan.quark.cn/s/a4b39357ea24 微信小程序是由腾讯公司设计的一种轻量级应用开发平台,主要面向移动设备,旨在为用户带来无需下载安装即可直接使用的应用服务体验。在微信小程序的开发实践中,"模仿美团菜单 swiper分类菜单"是一项常见的技术应用,其目的是达成与美团外卖应用相似的交互模式,使用户能够轻松地浏览和挑选各种商品或服务。 在微信小程序的技术体系中,`swiper`组件属于轮播图功能模块,通常用于呈现多张图片或卡片式的切换界面,能够达成动态滑动展示不同分类菜单的功能。在模拟美团菜单的应用情境下,`swiper`组件一般与`swiper-item`组件协同工作,每一个`swiper-item`对应一个分类,用户可通过左右滑动来切换不同的分类菜单。 为了达成这样的功能,首先需要在微信小程序的`json`配置文档中引入`swiper`组件,并在`wxml`结构文档中构建相应的布局代码,将每个分类以`swiper-item`的形式进行组织。同时,需要在`wxss`样式文档中定义恰当的样式,保证分类菜单的视觉呈现符合设计规范。 接下来,我们将在`js`逻辑文档中处理`swiper`的切换逻辑,通过监听`bindchange`事件来识别当前选中的分类,并根据这一信息动态获取对应的子菜单信息。为了达成类似美团的层级菜单功能,可能还需要借助`picker`或`navigator`组件来展示二级分类或商品详情。 在开发阶段,需要关注以下关键点: 1. 数据绑定:保证数据与界面之间的双向链接准确无误,以便在选择分类时即时更新显示内容。 2. 动态加载:针对大量数据,可能需要采取异步加载机制,防止一次性加载过多内容引发性能瓶颈...

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧