有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程Java并行线程

public class Signal2NoiseRatio
{
    public ImagePlus SingleSNR(ImagePlus imagePlus) throws InterruptedException
    {

        new Thread()
        { 
          @Override public void run() 
          { 
              JFrame imageFrame = new JFrame("ROI");
              Container imageFrame_Container = imageFrame.getContentPane();
              IIImagePanel imagePanel = new IIImagePanel();
              imageFrame_Container.add(imagePanel);
              imagePanel.setImage(imagePlus.getImage());
              imagePanel.getDisplayedImage();
              imageFrame.setVisible(true);
              final SNRSingleImageListener sNRSingleListener = new SNRSingleImageListener(imagePanel);  
              imagePanel.addMouseListener(sNRSingleListener);
              imagePanel.addMouseMotionListener(sNRSingleListener);
          }
        }.start();


        new Thread() 
    { 
      @Override public void run() 
      { 

          for (int i = 0; i <= 2000; i++)
          { 
             System.out.println("schleife "+i);
                     // ask if useractions are done ..
          }

          synchronized( Signal2NoiseRatio.this ) 
          { 

             Signal2NoiseRatio.this.notifyAll(); 

          }
      }
    }.start();


        synchronized (this) 
        {
        this.wait();
            // if userinteractions are done, go on
        }


        return imagePlusToProcess;
    }
}

第一个new Thread()执行一个帧,该帧显示其中的图像。我的意图是在一个新线程中呈现图像,以等待用户在图像上进行一些交互。但是代码会将帧引导到白色窗口,图像不可见,帧不可用

在第二个线程中,我希望在短时间内询问用户操作是否完成

这不是一个很好的解决方案,但有可能吗?这里怎么了

谢谢你


共 (2) 个答案

  1. # 1 楼答案

    我可以在这里看到一些问题:

    一,。如果这

    synchronized (this) 
    {
        this.wait();
        // if userinteractions are done, go on
    }
    

    发生在UI线程上,则您将阻止它接收用户输入(或执行任何其他操作),直到对象收到信号为止

    二,。这一部分似乎过于复杂:

        new Thread() {
            @Override
            public void run() {
    
                for (int i = 0; i <= 2000; i++) {
                    System.out.println("schleife " + i);
                    // ask if useractions are done ..
                }
    
                synchronized (Signal2NoiseRatio.this) {
    
                    Signal2NoiseRatio.this.notifyAll();
    
                }
            }
        }.start();
    
        synchronized (this) {
            this.wait();
            // if userinteractions are done, go on
        }
    

    只需使用:

        Thread t = new Thread() {
            @Override
            public void run() {
    
                for (int i = 0; i <= 2000; i++) {
                    System.out.println("schleife " + i);
                    // ask if useractions are done ..
                }
    
            }
        }.start();
    
        t.join();
    

    除非你发出的信号比上面所说的要多。但同样,这是多余的,因为启动一个线程只是等待它完成并没有多大意义

  2. # 2 楼答案

    问题解决了

    该方法的调用程序是一个AWT线程。我为调用程序创建了一个新线程,因此AWT线程没有被阻塞,然后帧和图像可以正确显示

    谢谢大家的帮助