Thread interrupt not ending blocking call on input stream read

前端 未结 3 1339
时光取名叫无心
时光取名叫无心 2021-01-04 13:30

I\'m using RXTX to read data from a serial port. The reading is done within a thread spawned in the following manner:

CommPortIdentifier portIdentifier = Co         


        
3条回答
  •  庸人自扰
    2021-01-04 13:49

    i am using the code below to shutdown rxtx. i run tests that start them up and shut them down and the seems to work ok. my reader looks like:

    private void addPartsToQueue(final InputStream inputStream) {
        byte[] buffer = new byte[1024];
        int len = -1;
        boolean first = true;
        // the read can throw
        try {
            while ((len = inputStream.read(buffer)) > -1) {
                if (len > 0) {
                    if (first) {
                        first = false;
                        t0 = System.currentTimeMillis();
                    } else
                        t1 = System.currentTimeMillis();
                    final String part = new String(new String(buffer, 0, len));
                    queue.add(part);
                    //System.out.println(part + " " + (t1 - t0));
                }
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException e) {
                    //System.out.println(Thread.currentThread().getName() + " interrupted " + e);
                    break;
                }
            }
        } catch (IOException e) {
            System.err.println(Thread.currentThread().getName() + " " + e);
            //if(interruSystem.err.println(e);
            e.printStackTrace();
        }
        //System.out.println(Thread.currentThread().getName() + " is ending.");
    }
    

    thanks

    public void shutdown(final Device device) {
        shutdown(serialReaderThread);
        shutdown(messageAssemblerThread);
        serialPort.close();
        if (device != null)
            device.setSerialPort(null);
    }
    
    public static void shutdown(final Thread thread) {
        if (thread != null) {
            //System.out.println("before intterupt() on thread " + thread.getName() + ", it's state is " + thread.getState());
            thread.interrupt();
            //System.out.println("after intterupt() on thread " + thread.getName() + ", it's state is " + thread.getState());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName() + " was interrupted trying to sleep after interrupting" + thread.getName() + " " + e);
            }
            //System.out.println("before join() on thread " + thread.getName() + ", it's state is " + thread.getState());
            try {
                thread.join();
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getName() + " join interruped");
            }
            //System.out.println(Thread.currentThread().getName() + " after join() on thread " + thread.getName() + ", it's state is" + thread.getState());
        }
    

提交回复
热议问题