繁体   English   中英

线程同步来自 Swing 组件

[英]Thread synchronization from Swing Component

在尝试将我的主要任务与其子任务(线程)同步之后,我失败了很多次。 从 swing 组件调用时,我没有找到任何示例。

我创建了一个白色面板,在上面显示剪辑影片。 这很好用。 但是,一旦 Thread 终止,我想重新控制主任务。 似乎在“setVisible(true)”之后没有执行下一条指令。我还尝试了一个循环发出 sleep 1 秒,但它没有被执行。这是我最后的代码。

     /* Auteur: Gérard MARTINELLI  */
     import gegeutil.Gegetools;
     import javax.swing.JFrame;
     import javax.swing.JOptionPane;
     import java.io.BufferedReader;
     import java.io.InputStreamReader;
     import java.util.Vector;
     import java.util.concurrent.CountDownLatch;
     import java.awt.Color;
     
     public class JouerClip extends javax.swing.JDialog   
     
      
     /*    Variables et constantes  */
        
     public static final long serialVersionUID = 1401213293925293574L;
     
     public     static String  titre       =   "",
                            film         =   "";
     protected  Thread      th           =   new Thread();
     protected  boolean     inService    =   false,
                           internal     =   false;
     public static Process process     =    null;
     public static  String videoplayer  =   "E:/PotPlayer/PotPlayerMini64.exe";
     public static  String pathFilm     =   "F:/Films";
     
        public JouerClip(JFrame owner, String  filmName, boolean inter) // constructeur
     { 
        super(owner,true);
        if (Gegetools.isEmpty(filmName)) setVisible(false); 
        film =   filmName;
        internal = inter;
         initialize();
     }
      
     public void initialize() 
     {    
        setSize(1136, 873);
        getContentPane().setLayout(null);
        getContentPane().setBackground(Color.WHITE);
        setLocationRelativeTo(null); 
        th = new Thread() 
        {
              public void run() 
              {
                   System.out.println("Thread  running");
                    
               jouer(pathFilm+"/" + film); 
                   System.out.println("Thread stopped");
                   inService= false;
              }
          };
          inService= true;
          th.start();
          setVisible(true);
          System.out.println("I continue the main task");
          try 
          {
               CountDownLatch latch = new CountDownLatch(3);
               latch.await(); // Wait for countdown
               this.setVisible(false);
               if (internal) System.exit(0); else this.setVisible(false);
              } 
         catch (InterruptedException e) 
         {
                 e.printStackTrace();
             }
     }
     
     public static void jouer( String path) 
     {
        //  This works pretty well. 
        // When the clip ends, the videoplayer has  gone. 
     }
     
     public static void main(String[] agrs) 
     {
             new JouerClip(new JFrame(), "Seven.mp4", true);
    }
    } 

我想我已经解决了我的顾虑。 在 JDialog ContenPane 中,我创建了一个带有 actionListener 的 JButton,然后在离开 run 方法之前,我发出了一个 doClic()。 这执行我想要的(例如返回给调用者)。 它工作得很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM