我想确保我的serialevents不会受到另一个类的影响

时间:2013-05-07 19:20:02

标签: java multithreading event-handling serial-port

我正在编写与多台机器串行通信的代码。每台机器都与一个通信器类的实例交互,该类具有一个串口事件监听器。

当机器收到足够的数据时,它会执行二次测试。 (因为测试本身是二次的,所以无法帮助)并且输入可能很大。因此,如果代码正在进行计算,我担心某些串行事件不会被注册。

作为一种解决方案,我考虑创建一个运行计算的线程,并将其设置为在其循环期间休眠一段时间,该时间由连接的机器数决定。然而我当时认为如果我可以将那个Thread从serialevent方法中保持睡眠状态,那么这可能是一个更好的主意吗?这是可能的,还是线程不会运行,直到正在运行的方法完成?

现在在下面的代码中,我在计算方法中包含了Thread.sleep,因为如果serialevent无法中断线程,我就会这样做

private class CalculationThread implements Runnable{
@Override
  public void run() 
    {
          calculateResult();
    }
}}

private void calculateResult() {
    ArrayList<Double> theoretical_vals;
    ArrayList<ArrayList<Double>> theoretical_curves = new ArrayList();
    double current_maxdiff, maxdiff;
    double ao = measurements.get(0).getMeasurement();
    theoretical_vals = RadioCalculations.theoreticalVals(measurements, hf, ao);
    theoretical_curves.add(theoretical_vals);
    int index = 1;
    for (MeasurePoint m : measurements) {
        theoretical_vals = RadioCalculations.calibratecontrolValues(measurements, index, hf);
        try {
            Thread.sleep(20*(parent.getNumberOfTests()-1));} 
        catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        theoretical_curves.add(theoretical_vals);
        index++;
    }
    index = 1;
    maxdiff = 0;
    for (ArrayList a : theoretical_curves) {
        try {
            Thread.sleep(20*(parent.getNumberOfTests()-1));
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        current_maxdiff = compareValues(a, measurements);
        if (current_maxdiff > maxdiff) {
            if (current_maxdiff > pass_limit) {
                passed = false;
                failed_measurementpoint = index;
                break;
            }
            maxdiff = current_maxdiff;
            index++;
        }

    }
    passed = true;
    max_dev = maxdiff;
    logResults();
}




public void serialEvent(SerialPortEvent spe) {
    try {
        Thread.sleep(10);
    } catch (InterruptedException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
   pauseListen(false);     
   if (spe.getEventType()== SerialPortEvent.DATA_AVAILABLE){   

         try {
    while (inputStream.available() > 0) {
                   numBytes =  inputStream.read(readBuffer);}} 
        catch (IOException e) {e.printStackTrace();}
        input_line= new String(readBuffer,0,numBytes);
        input_line = input_line.replaceAll("[\n\r]","*");
        buffer.append(input_line);
        if (input_line.contains("*")){   
        input_line= buffer.toString();
        input_line = input_line.replaceAll("[*]","");
        buffer.setLength(0);
        pauseListen(true);
        update(input_line);}}
}
} 

1 个答案:

答案 0 :(得分:0)

您可以使用BlockingQueue(自Java 5开始)将新计算放入此队列,而进程中有一个计算。

首先,您需要一个包装器类来接收串口数据:

class CalculationWrapper {

    // fields
    // getters setters

    public void calculateResult() {
        // operations
    }
}

用于计算的方法calculateResult可以在此课程中或在下一课程中进行:

class Calculator implements Runnable {
    private final BlockingQueue<CalculationWrapper> queue;

    Calculator(BlockingQueue<CalculationWrapper> q) {
        queue = q;
    }

    public void run() {
        try {
            while (true) {
                CalculationWrapper wrapper = queue.take();
                wrapper.calculateResult();
            }
        } catch (InterruptedException ex) {
            // log error
        }
    }
}

方法take等待队列中有更多新计算。 串口事件监听器的类(以及用于放置新计算的)可以是:

class Receiver implements Runnable, SerialPortEventListener {

    private final BlockingQueue<CalculationWrapper> queue;

    Receiver(BlockingQueue q) {
        queue = q;
    }

    public void run() {
        try {
            while (true) {
                Thread.sleep(1000);
            }
        } catch (InterruptedException ex) {
            // log
        }
    }

    public void serialEvent(SerialPortEvent evt) {
        switch (evt.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE:
                try {
                    // read
                    CalculationWrapper wrapper = new CalculationWrapper();
                    // set data on wrapper
                    queue.put(wrapper);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
        }
    }
}

主要类 setup 类:

class Setup {
    public static void main(String args[]) {
        // get port
        // register listener

        BlockingQueue q = new ArrayBlockingQueue(10);
        Receiver p = new Receiver(q);
        Calculator c1 = new Calculator(q);
        new Thread(p).start();
        new Thread(c1).start();
    }
}

这在一个方面。查看更多: