获取并设置线程中的Flag变量

时间:2013-07-09 12:32:09

标签: java multithreading

我正在与允许我使用指纹扫描仪的JNI连接。我编写的代码将扫描的ByteBuffer通过JNI解析回它,并将其转换为BufferedImage进行保存。

我无法弄清楚如何在我的GUI上的jlabel图标尝试更新之前等待扫描线程完成。最简单的方法是什么?

我还需要添加什么?

编辑:

//Scanner class
Thread thread = new Thread() {
        public void run() {
            // [...] get ByteBuffer and Create Image code
            try {
                File out = new File("C:\\Users\\Desktop\\print.png");
                ImageIO.write(padded, "png", out);
                // [???] set flag here
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
thread.start();
return true;

//Gui class
private void btnScanPrintActionPerformed(java.awt.event.ActionEvent evt) {
    Scanner scanPrint = new Scanner();
    boolean x = scanPrint.initDevice();
    //Wait for the scanning thread to finish the Update the jLabel here to show
    //the fingerprint
} 

2 个答案:

答案 0 :(得分:3)

不确定您是使用Swing还是Android for UI,但是您想要通知主事件派发线程(在摇摆中它被称为)。您将运行扫描线程,然后在完成时向EDT发送“消息”,并执行您想要对该按钮执行的操作。

Thread thread = new Thread(new Runnable(){
     public void run(){
         //scan
         SwingUtiltilies.invokeLater(new Runnable(){
              //here you can update the the jlabel icon
              public void run(){
                  jlabel.setText("Completed");
              }
         });
     } 
});

在UI开发中,无需等待操作完成,因为您始终希望EDT能够做出响应。

答案 1 :(得分:0)

在扫描线程结束时,使用SwingUtilities.invokeLater()更新gui和扫描结果。

相关问题