Runnable version of sample code in Exchanger's javadoc.
import java.util.concurrent.Exchanger;
public class FillAndEmpty {
static String PART = "abcde";
static int PART_LEN = 5;
static int STEP_COUNT = 10;
Exchanger<StringBuilder> exchanger = new Exchanger<StringBuilder>();
StringBuilder initialEmptyBuffer = new StringBuilder(50);
StringBuilder initialFullBuffer = new StringBuilder(50);
class FillingLoop implements Runnable {
StringBuilder currentBuffer = initialEmptyBuffer;
@Override
public void run() {
try {
while (currentBuffer != null) {
currentBuffer.append(PART);
if (currentBuffer.length() == PART_LEN * STEP_COUNT) {
System.out.println("full buffer: " + currentBuffer);
currentBuffer = exchanger.exchange(currentBuffer);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class EmptyingLoop implements Runnable {
@Override
public void run() {
StringBuilder currentBuffer = initialFullBuffer;
try {
while (currentBuffer != null) {
currentBuffer.setLength(currentBuffer.length() - PART_LEN);
if (currentBuffer.length() == 0) {
System.out.println("empty buffer: " + currentBuffer);
currentBuffer = exchanger.exchange(currentBuffer);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void start() {
for (int i = 0; i < STEP_COUNT; i++)
initialFullBuffer.append(PART);
new Thread(new FillingLoop()).start();
new Thread(new EmptyingLoop()).start();
}
public static void main(String[] args) {
FillAndEmpty fae = new FillAndEmpty();
fae.start();
}
}
分享到:
相关推荐
6. **非接触式操作**:ACR122U支持NFC(近场通信)功能,SDK可能包含与NDEF(Near Field Communication Data Exchange Format)消息相关的示例,用于读取和写入NDEF记录。 7. **错误处理**:任何编程项目都必须考虑...
8. **Microsoft.ML.OnnxRuntime.dll**:OnnxRuntime是由微软开发的跨平台机器学习推理引擎,用于执行ONNX(Open Neural Network Exchange)格式的模型。在本案例中,它用于运行图像修复模型。 9. **C# CodeFormer ...
标题"sample_code:一个简单的示例,演示了Dart GSoC的SPDX许可证检测器中的每个功能"明确指出这是一个示例代码,用于展示如何在Dart编程语言环境中,针对Google Summer of Code (GSoC)项目中的SPDX(Software ...
Jump into the world of Near Field Communications NFC the fast growing technology that lets devices in close proximity exchange data using radio signals With lots of examples sample code exercises and ...
<END><br>3,cfInternet.zip a sample program of Internet source code is strictly prohibited without written permission<END><br>4,cfSocket.zip a sample program of cfSocket source code is strictly ...
Note: MAPI is used by various industry-standard e-mail clients, such as the Microsoft Exchange client, all versions of Microsoft Outlook and Outlook Express, including QUALCOMM Incorporated (Eudora) ...
2. **授权码(Authorization Code)**:如果用户同意授权,授权服务器会返回一个授权码给客户端。这是一个临时凭证,用于后续的令牌交换。 3. **令牌交换(Token Exchange)**:客户端使用授权码向授权服务器请求...
MS Windows Sample Code MS KnowledgeBase - MS Windows 以及 Options => Library 菜单下提供的 9 个重要的参考资料,其中就有 C 和 MASM 这些重要的参考资料。这些是已安装的目录部分,鉴于 MASM 的重要性,特将...
So 下载 the code and get involved with the News Group, help us to help you.<END><br>4 , urllink.zip User control to launch web browser and jump to URL.<END><br>5 , vbftp.zip Sample application ...
You can use these functions to insert a file into the active file, insert a string into the file at every specified increment, sample colors from anywhere on your screen, and more. Using Bookmarks ...
This sample code does just that. If you do any kind <END><br>55,splitfile.zip This project contains several useful and interesting bits of code, but the best thing is a function/routine to split a...
Data Exchange Code in Practice Data Validation Code with DDV_ Using ClassWizard Performing Cross-Edits About Live Edits Using Common Dialogs File Save and Save As File Open Print Font Browser...
Code to read input and construct the graph ... // ... Apply Dijkstra's algorithm for each valid range and find the minimum cost ... return 0; } ``` #### 总结 通过对题目描述的理解,我们可以将其...
关于 该示例应用程序显示了如何使用TwiML 和信用卡通过电话进行账单支付。 其他语言的实现: 。网 Java Python ...基本的Web用户界面,使用进行模板化,并使用Bootstrap进行UI ... 一键部署按钮,用于Heroku和Glit
由Twilio提供支持的约会提醒关于约会提醒使您可以自动为即将到来的约会提前与客户联系。 在此示例中,您将学习如何使用Twilio为业务用户创建自动约会提醒。 使用约会提醒来减少未出现的情况,并确保客户在约会之前...
3. `sample_code`:示例代码,展示了如何在TensorRT环境中加载和运行优化后的模型进行推理。 4. `requirements.txt`:列出项目所需的Python依赖库,确保环境配置正确。 5. `README.md`:项目说明文档,包含详细步骤...
Twilio示例应用程序模板关于这是用于创建其他示例/模板应用程序的GitHub模板。 它包含各种功能,理想情况下,每个Twilio示例应用程序都应包含这些功能。 您可以使用功能来创建它的副本。 其他语言的实现: 。...
对话中通过SMS屏蔽的电话号码关于Twilio对话允许您构建虚拟空间(“对话”)以跨多个渠道进行交流。 此应用程序显示如何将多个外部电话号码添加到Twilio对话中并跟踪其活动。 其他语言的实现: 。...
具有自动驾驶仪的语音驱动IVR聊天机器人 关于 交互式语音响应或IVR是一种自动电话系统,可通过使用语音和按键式小键盘选择(DTMF音调)与人工呼叫者进行交互。 此应用程序展示了如何使用构建IVR应用程序。...
you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy ...