Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java线程连接问题_Java_Multithreading_Swt_Eclipse Rcp - Fatal编程技术网

Java线程连接问题

Java线程连接问题,java,multithreading,swt,eclipse-rcp,Java,Multithreading,Swt,Eclipse Rcp,线程: public void setGifImage(InputStream inputStream) { checkWidget(); if (thread != null) { thread.stopRunning(); try { thread.join(); } catch (InterruptedException e) {

线程:

public void setGifImage(InputStream inputStream) {
        checkWidget();
        if (thread != null) {
            thread.stopRunning();
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        ImageLoader loader = new ImageLoader();

        try {
            loader.load(inputStream);
        } catch (Exception e) {
            this.image = null;
            return;
        }

        if (loader.data[0] != null){
            System.out.println("set to new picture");
            this.image = new Image(this.getDisplay(), loader.data[0]);
        }

        if (loader.data.length > 1) {
            System.out.println("start animation");
            thread = new GifThread(loader);
            thread.start();
        }else{
            System.out.println("paint static picture");
        }

        redraw();
    }
输出:

private class GifThread extends Thread {

        private int imageNumber = 0;
        private ImageLoader loader = null;
        private boolean run = true;

        public GifThread(ImageLoader loader) {
            this.loader = loader;
        }

        public void run() {
            while (run) {
                int delayTime = loader.data[imageNumber].delayTime;
                try {
                    Thread.sleep(delayTime * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (!GifCLabel.this.isDisposed()) {
                    // if a asynchronous thread is running, this new runnable will be queued
                    GifCLabel.this.getDisplay().asyncExec(new Runnable() {
                        public void run() {
                            if (!GifCLabel.this.isDisposed()) {
                                imageNumber = imageNumber == loader.data.length - 1 ? 0 : imageNumber + 1;
                                if (!GifCLabel.this.image.isDisposed())
                                    GifCLabel.this.image.dispose();
                                ImageData nextFrameData = loader.data[imageNumber];
                                System.out.println("set to frame " + imageNumber);
                                GifCLabel.this.image = new Image(GifCLabel.this.getDisplay(), nextFrameData);
                                GifCLabel.this.redraw();
                            } else
                                stopRunning();
                        }
                    });
                } else
                    stopRunning();
            }
        }

        public void stopRunning() {
            run = false;
        }
    }

我想知道为什么线程仍然在thread.join()之后运行?正如我所知,thread.join()等待线程死亡,但您可以看到输出中的最后一行,线程在死亡后运行

这里的问题是线程调用
getDisplay().asyncExec()
,它基本上向UI线程发送事件,以执行打印
“设置为帧”
的Runnable

最好使用
syncExec()
并在Runnable中检查
run
变量状态

另一个提示:

  • 捕获
    InterruptedException
    而什么也不做不是一个好主意
  • 调用
    interrupt
    内部
    stopRunnable()
  • 睡眠
    结束后,检查
    运行
    变量状态
  • 干杯,
    Max

    您确定要加入的线程与从中查看日志的线程相同吗?换句话说,没有创建额外的线程?(您应该使该方法同步)谢谢您的关注。要同步的方法是什么?作为Max的答案,这也能解决我的问题吗?虽然我的问题已经解决,但我想了解更多关于线程的知识:DMax和我都认为您加入的线程不是执行工作的线程。我的想法是,可能您的setGifImage方法正在被并发访问;如果一个线程连接了两次,并且创建了一个未连接的线程,那么这是真的。如果您确信您的方法没有被并发调用,那么不需要更改任何内容;在其他地方,您可以使其同步。
    2011-08-10 03:44:24 DEBUG  - 日志服务Ready!
    2011-08-10 03:44:28 DEBUG  - current is null
    2011-08-10 03:44:28 DEBUG  - found : amarsoft.dbmp.function.ui.FunctionListPage@1079ff
    2011-08-10 03:44:28 DEBUG  - can go back ? false
    2011-08-10 03:44:28 DEBUG  - can go forward ? false
    set to new picture
    start animation
    2011-08-10 03:44:28 DEBUG  - 尝试连接至 - jdbc:mysql://localhost:3306/credit 驱动配置:MySQL 驱动类:com.mysql.jdbc.Driver@1f7abae 连接属性:{user=root, password=root}
    set to frame 1
    set to frame 2
    set to frame 3
    set to frame 4
    [amarsoft.dbmp.function.ui.FunctionListPage$1@29f93b, amarsoft.dbmp.function.ui.FunctionView$1@1a998c7]
    set to new picture
    paint static picture
    set to frame 5