2

如果我有一个JFrame实现Observer接口的方法,该update()方法会在事件调度线程上执行吗?

public class MyFrame extends JFrame implements Observer{
    @Override
    public void update(Observable o, Object arg) {
        //do gui stuff  
    }   
}

.

public class MyTask extends Observable implements Runnable{
    @Override
    public void run() {
        setChanged();
        notifyObservers("Update!");
    }
}
4

2 回答 2

2

正如我自己发现的那样:

不,它将在Thread调用该notifyObservers()方法的对象上执行。

从源码中可以看出,该notifyObservers()方法只是调用update()了观察者的方法,没有将任务传递给另一个线程。

public void notifyObservers(Object arg) {
    /*
    * a temporary array buffer, used as a snapshot of the state of
    * current Observers.
    */
    Object[] arrLocal;

    synchronized (this) {
        if (!changed)
            return;
        arrLocal = obs.toArray();
        clearChanged();
    }

    for (int i = arrLocal.length-1; i>=0; i--)
        ((Observer)arrLocal[i]).update(this, arg);
    }
}
于 2013-09-09T14:17:51.393 回答
0

如果你想做我认为你想做的事情,你可以使用 SwingWorker。它有发布方法和处理方法。publish 方法会将消息发送到 process 方法。与 SwingWorker 的其余部分不同,process 方法在 EDT 上运行。

import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;

import javax.swing.SwingWorker;


public class MyFrame extends JFrame {

    private static JTextField txtFld;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
                new UpdateSwingWorker().execute();
            }
        });    
    }

    private static void createAndShowGUI() {
        MyFrame frame = new MyFrame();

        JPanel panel = new JPanel(new BorderLayout());
        txtFld = new JTextField();
        panel.add(txtFld, BorderLayout.CENTER);

        frame.setSize(new Dimension(300,200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(300,200));
        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);     
    }

    private static class UpdateSwingWorker extends SwingWorker<Void, String> {

        @Override
        protected Void doInBackground() throws Exception {
            // Do your work...
            System.out.println("Thread in doInBackground: " + Thread.currentThread());
            publish("Updated");
            return null;
        }

        @Override
        protected void process(List<String> chunks) {
            System.out.println("Thread in process: " + Thread.currentThread());
            txtFld.setText(chunks != null ? chunks.get(0) : "");
        }

    }

}

或者,如果您真的想使用观察者模式,您可以将 notifyObservers 调用包装在 EventQueue 的 invokeLater 调用中。此方法导致 Runnable 在 EDT 中发生其运行方法。

框架:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyFrame2 extends JFrame implements Observer {
    private static JTextField txtFld;

    private static MyTask task = new MyTask();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
                new Thread(task).start();
            }
        });    
    }

    private static void createAndShowGUI() {
        MyFrame2 frame = new MyFrame2();
        task.addObserver(frame);

        JPanel panel = new JPanel(new BorderLayout());
        txtFld = new JTextField();
        panel.add(txtFld, BorderLayout.CENTER);

        frame.setSize(new Dimension(300,200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(300,200));
        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);     
    }

    @Override
    public void update(Observable o, Object arg) {

        System.out.println("Thread in update: " + Thread.currentThread());
        txtFld.setText((String) arg);
    }

}

任务:

import java.awt.EventQueue;
import java.util.Observable;

public class MyTask extends Observable implements Runnable {

    @Override
    public void run() {
        // Do work
        System.out.println("Thread in run: " + Thread.currentThread());
        setChanged();

        // Notify the observers on the EDT.
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                System.out.println("Thread in invokeLater: " + Thread.currentThread());
                notifyObservers("Update!");
            }
        });
    }

}
于 2013-09-10T03:25:12.240 回答