Java:通过Java.util.concurrent线程访问读取的线程串行端口 [英] Java : threaded serial port read with Java.util.concurrent thread access

查看:89
本文介绍了Java:通过Java.util.concurrent线程访问读取的线程串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Java串行设备驱动程序,并希望使用(对我来说是新的)java.util.concurrent程序包.我有一种发送数据包然后等待ACK的方法.我打算吃炭.接收运行在另一个线程中.如果接收线程收到ACK,则应使用发送数据包功能通知该线程.接收线程实现状态机,并应将解码后的数据包通知所有侦听器.

I'm trying to write a Java serial device driver and want to use the (new to me) java.util.concurrent package. I have one method which sends a packet then waits for an ACK. I plan to have char. reception run in a different thread. If the receive thread gets an ACK it should notify the thread with the send packet function. The receive thread implements a state machine and should notify any listeners of decoded packets.

我想我知道如何使用直接线程,waitnotify等来执行此操作,但是不确定如何使用新的并发程序包执行此操作.非常感谢任何指针.

I think I know how to do this using direct Threads, wait, notify etc., but am not sure how to do this with the new concurrent package. Would be very grateful for any pointers.

推荐答案

使用

Use a CyclicBarrier. Here's a cite of relevance from its Javadoc:

一种同步辅助工具,它允许一组线程互相等待以到达一个公共的障碍点. CyclicBarriers在涉及固定大小的线程方的程序中很有用,该线程方有时必须互相等待.该屏障称为循环屏障,因为它可以在释放等待线程之后重新使用.

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.

因此,您需要为多个2参与者创建一个CyclicBarrier,并让接收方线程在ACK之后调用await(),并让发送方线程在执行SEND之前调用await().

So, you need to create a CyclicBarrier for a number of 2 parties and let the receiver thread invoke await() after ACK and let the sender thread invoke await() before doing the SEND.

这是一个 SSCCE ,可以帮助您入门.

Here's an SSCCE to get you started.

package com.stackoverflow.q3379797;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(2);
        Receiver receiver = new Receiver(barrier);
        Sender sender = new Sender(barrier);

        ExecutorService executor = Executors.newFixedThreadPool(2);
        executor.submit(receiver);
        executor.submit(sender);
    }

}

class Receiver implements Runnable {

    private CyclicBarrier barrier;

    public Receiver(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        while (true) {
            try {
                // Wait for ACK (the sleep just simulates that).
                Thread.sleep(2000);
                System.out.println("ACK");

                // Then notify.
                barrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

class Sender implements Runnable {

    private CyclicBarrier barrier;

    public Sender(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        while (true) {
            try {
                // Wait for notify.
                barrier.await();

                // Now do SEND.
                System.out.println("SEND");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

您将在标准输出中看到以下内容:

You'll see the following in the stdout:


(2 seconds)
ACK
SEND
(2 seconds)
ACK
SEND
(2 seconds)
ACK
SEND

这篇关于Java:通过Java.util.concurrent线程访问读取的线程串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆