Java: streaming serial port with access to Java.util.concurrent thread

I am trying to write a Java serial device driver and want to use a package (new to me) java.util.concurrent. I have one method that sends a packet and then waits for the ACK. I plan to have a char. Reception is in another thread. If the receive stream receives an ACK, it must notify the stream using the send send function. The adopted thread implements a finite state machine and should notify listeners of decoded packets.

I think I know how to do it using the direct-Forums Threads, wait, notifyand so on, but I'm not sure how to do this in parallel with the new package. Would be very grateful for any pointers.

+2
source share
1 answer

Use CyclicBarrier. Here is a quote related to her Javadok:

A synchronization assistant that allows multiple threads for all to wait for each other to reach a common barrier point. CyclicBarriers are useful in programs related to the fixed side of the parties, which must wait for each other from time to time. A barrier is called cyclic because it can be reused after waiting threads are released.

So, you need to create CyclicBarrierfor several parties 2and allow the receiver thread to call await()after the ACK and allow the sender thread await()before executing SEND.

Here is 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();
            }
        }
    }
}

:

(2 seconds)
ACK
SEND
(2 seconds)
ACK
SEND
(2 seconds)
ACK
SEND
+4

Source: https://habr.com/ru/post/1759657/


All Articles