Java中线程的串行写入

Java中线程的串行写入,java,multithreading,serial-port,javafx-2,rxtx,Java,Multithreading,Serial Port,Javafx 2,Rxtx,我使用java线程与设备进行串行通信。它在调试模式下工作得非常好,但当我运行程序时,线程不会执行指令。我使用RXTX库进行串行通信,程序的其余部分使用JavaFX(应用程序) 这是am用于串行通信的类(SerialClass.java): Am使用另一个类ImageInterface,该类使用以下类: import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import org.apac

我使用java线程与设备进行串行通信。它在调试模式下工作得非常好,但当我运行程序时,线程不会执行指令。我使用RXTX库进行串行通信,程序的其余部分使用JavaFX(应用程序)

这是am用于串行通信的类(SerialClass.java):

Am使用另一个类ImageInterface,该类使用以下类:

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.sanselan.ImageReadException;
import finImage.FinImage;
import img.ImageAccessor;
import img.ImageBean;

public class ImageInterface {

    String image;
    public static BufferedReader input;
    public static OutputStream output;

    public static synchronized void writeData(String data) {
        System.out.println("Sent: " + data);
        try {
            output.write(data.getBytes());
        } catch (Exception e) {
            System.out.println("could not write to port");
        }
    }

    public void openWithImage(File ImgFile) {

        try {
            // TODO code application logic here
            ImageAccessor sa = new ImageAccessor();
            ImageBean s = new ImageBean();
            String ImgId = FinImage.getImageFromFin(ImgFile);
            System.out.print(ImgId);
            String sm = ImgId.toString();
            System.out.print(sm);
            String imageid = FinImage.getImageFromFin(ImgFile);
            String sme;

            Boolean flag = false;

            if (imageid.startsWith("'")) {
                sme = imageid.substring(1, imageid.length() - 1);
            } else {
                sme = imageid;
            }
            s = sa.getAllImageInfoById(sme);
            System.out.print(s);
            image = s.getFinPin();
            //System.out.print(FinPin);
            //System.out.print(FinPin);S
            // image = ImgPin.toString();
            System.out.print(image);
            try {
                SerialClass obj = new SerialClass();
                int c = 0;
                obj.initialize();
                input = SerialClass.input;
                output = SerialClass.output;
                InputStreamReader Ir = new InputStreamReader(System.in);
                BufferedReader Br = new BufferedReader(Ir);

                Thread t = new Thread() {
                    public void run() {
                        obj.writeData(image);
                    }
                    //catch (InterruptedException ie){}
                };

                t.start();
                System.out.println("Started");

                obj.close();
            } catch (Exception e) {
            }
        } catch (ImageReadException ex) {
            Logger.getLogger(ImageInterface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ImageInterface.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
在上面的代码中,我访问图像中的一个id,然后通过串行通信将其写入。在上面的程序中,线程在程序运行时不会写入,但在程序调试时会写入串行端口。我无法找出发生这种问题的确切原因

我使用Arduino进行输出。我还同时使用了两个操作系统:Ubuntu 12.04LTS和Windows 7。在Windows7中,线程在调试中执行,而在Ubuntu 12.04中,线程在启动时不启动run方法


请指出我在哪里犯了错误

您已经在“主”线程和“写”线程之间创建了竞争条件。在开始编写线程之后,您可以立即“主”线程调用
close
。由于您不必等待写入线程完成,因此可能在写入任何内容之前关闭通道。在“调试”模式下,运行速度往往较慢,因此在主线程调用close之前,写线程可能有时间完成


一个简单的解决方案是在
obj.close()
调用之前添加
t.join()
。这将强制主线程等待写入线程完成。但是,在您当前的代码中,不清楚为什么要使用第二个线程进行编写。

格式设置使代码很难阅读。您的程序可能正在退出吗?您可以尝试在启动线程之前添加
t.setDaemon(false)
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.sanselan.ImageReadException;
import finImage.FinImage;
import img.ImageAccessor;
import img.ImageBean;

public class ImageInterface {

    String image;
    public static BufferedReader input;
    public static OutputStream output;

    public static synchronized void writeData(String data) {
        System.out.println("Sent: " + data);
        try {
            output.write(data.getBytes());
        } catch (Exception e) {
            System.out.println("could not write to port");
        }
    }

    public void openWithImage(File ImgFile) {

        try {
            // TODO code application logic here
            ImageAccessor sa = new ImageAccessor();
            ImageBean s = new ImageBean();
            String ImgId = FinImage.getImageFromFin(ImgFile);
            System.out.print(ImgId);
            String sm = ImgId.toString();
            System.out.print(sm);
            String imageid = FinImage.getImageFromFin(ImgFile);
            String sme;

            Boolean flag = false;

            if (imageid.startsWith("'")) {
                sme = imageid.substring(1, imageid.length() - 1);
            } else {
                sme = imageid;
            }
            s = sa.getAllImageInfoById(sme);
            System.out.print(s);
            image = s.getFinPin();
            //System.out.print(FinPin);
            //System.out.print(FinPin);S
            // image = ImgPin.toString();
            System.out.print(image);
            try {
                SerialClass obj = new SerialClass();
                int c = 0;
                obj.initialize();
                input = SerialClass.input;
                output = SerialClass.output;
                InputStreamReader Ir = new InputStreamReader(System.in);
                BufferedReader Br = new BufferedReader(Ir);

                Thread t = new Thread() {
                    public void run() {
                        obj.writeData(image);
                    }
                    //catch (InterruptedException ie){}
                };

                t.start();
                System.out.println("Started");

                obj.close();
            } catch (Exception e) {
            }
        } catch (ImageReadException ex) {
            Logger.getLogger(ImageInterface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ImageInterface.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}