(简单代码)SwingWorker死锁

时间:2014-04-19 03:49:32

标签: java multithreading swing deadlock swingworker

我正在创建一个java程序,它将打开一个启动图像,然后每15分钟显示一个(新的或旧的)图像从函数CallMinutesInternational加载...(在doInBackground中)

为简单起见,此图像每隔几个间隔更改一次。

现在我有一个非常奇怪的错误....下面的代码只会在VISTA机器上死锁,但不会在我的机器上崩溃,也不会在Microsoft Server 2008 SP1机器上死锁。

这让我相信有些东西我没有得到。

我已在流程中排除了所有挥杆调用和创建()。但是,在20-50个间隔之后,我的电脑工作将陷入僵局(Vista)

如果有人能解释为什么会这样,我将不胜感激。我花了很多天时间试图解决这个简单的问题...一个错误的麻烦,但无法理解它。

有什么建议吗?

public class GUIThreadremaster extends JComponent {



static JFrame f = new JFrame();
static JLabel label = new JLabel();
static final String version = "v.72";
static final String splashUrl = "http://s3.postimg.org/48nx9vkur/image.jpg";




public static void main(String[] args) {



            SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                new GUIThreadremaster();
                n.execute();


            }});

   // A.GUIThread();

}



public static SwingWorker<Void, BufferedImage> n = new SwingWorker<Void,BufferedImage>()
        {
        //Semaphore block = new Semaphore(0);
        boolean b = false;


            @Override
            protected Void doInBackground()
                    throws Exception {
                // TODO Auto-generated method stub


                 URL url = null;
                 BufferedImage image = null;
                 String s;
                 int i = 1;

                 Thread.sleep(15000);
                while(!b)
                {


                     System.out.println(Thread.currentThread() + " " + i);
                     i++;


               s =  CallMinInternational.updateGraph(CallMinInternational.queryResultsLive(false));
                    //System.out.println("test");
                //  s = splashUrl;
                url = new URL(s);
                try {
                    image = ImageIO.read(url);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                //System.out.println("UPDATED : " );

                publish(image);
                 Thread.sleep(15000);
                }


                return null;

            }

            @Override
            protected void process(List<BufferedImage> chunks) 
            {
                ImageIcon tmp = new ImageIcon();
                tmp.setImage(chunks.get(chunks.size() - 1));

                JLabel labelnew = new JLabel();
                labelnew.setIcon(tmp);
                //System.out.println(Thread.currentThread());
                //System.out.println("test");
                f.getContentPane().removeAll();
                f.getContentPane().add(labelnew);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();

                f.setLocation(f.getX(), f.getY());



            }

        };

public GUIThreadremaster() {


                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                           try {
                                f.getContentPane().removeAll();


                               //String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l";
                               // System.out.println("Get Image from " + path);
                               // URL url = new URL(path);
                                //BufferedImage image = ImageIO.read(url);
                                URL url = new URL(splashUrl);
                                BufferedImage image = ImageIO.read(url);

                                label = new JLabel(new ImageIcon(image));
                                //JFrame f = new JFrame();
                                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                f.getContentPane().add(label);
                                f.pack();
                                f.setLocation(f.getX(), f.getY());
                                f.setVisible(true);
                                f.setResizable(false);
                                f.getContentPane().repaint();
                                f.setTitle("I.O.M.S - 360 Communications " + version);

                            } catch (Exception exp) {
                                exp.printStackTrace();
                            } 




        //   n.execute();



 }
}

新代码:(TrashGod)

public class GUIThreadremaster extends JComponent {



static JFrame f = new JFrame();
static JLabel label = new JLabel();
static final String version = "v.75";
static final String splashUrl = "http://s3.postimg.org/48nx9vkur/image.jpg";




public static void main(String[] args) {



            SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                new GUIThreadremaster();
                n.execute();


            }});

   // A.GUIThread();

}



public static SwingWorker<Void, BufferedImage> n = new SwingWorker<Void,BufferedImage>()
        {
        //Semaphore block = new Semaphore(0);
        boolean b = false;


            @Override
            protected Void doInBackground()
                    throws Exception {
                // TODO Auto-generated method stub


                 URL url = null;
                 BufferedImage image = null;
                 String s;
                 int i = 1;

                 Thread.sleep(15000);
                while(!b)
                {


                     System.out.println(Thread.currentThread() + " " + i);
                     i++;


                 s =  CallMinInternational.updateGraph(CallMinInternational.queryResultsLive(false));
                    //System.out.println("test");
                //  s = splashUrl;
                url = new URL(s);
                try {
                    image = ImageIO.read(url);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                //System.out.println("UPDATED : " );

                publish(image);
                 Thread.sleep(15000);
                }


                return null;

            }

            @Override
            protected void process(List<BufferedImage> chunks) 
            {
                ImageIcon tmp = new ImageIcon();
                tmp.setImage(chunks.get(chunks.size() - 1));


                label.setIcon(tmp);
                //System.out.println(Thread.currentThread());
                //System.out.println("test");
                f.pack();




            }

        };

public GUIThreadremaster() {


                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                           try {
                                f.getContentPane().removeAll();


                               //String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l";
                               // System.out.println("Get Image from " + path);
                               // URL url = new URL(path);
                                //BufferedImage image = ImageIO.read(url);
                                URL url = new URL(splashUrl);
                                BufferedImage image = ImageIO.read(url);

                                label = new JLabel(new ImageIcon(image));
                                //JFrame f = new JFrame();
                                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                f.getContentPane().add(label);
                                f.pack();
                                f.setLocation(f.getX(), f.getY());
                                f.setVisible(true);
                                f.setResizable(false);
                                f.getContentPane().repaint();
                                f.setTitle("I.O.M.S - 360 Communications " + version);

                            } catch (Exception exp) {
                                exp.printStackTrace();
                            } 




        //   n.execute();



}
}

0 个答案:

没有答案