从事件调度线程中分离逻辑线程

时间:2014-05-19 17:47:25

标签: java multithreading oop thread-safety logic

这是我项目中最小的可运行的SSCCE ,我可以实现这一点。

  • 我已经读过从Event Dispacth线程中调用游戏逻辑是一种不好的做法,我怎么能把它们分开呢,因为你可以看到 {{ 1}}和update()与循环相关 如何以一种漂亮的方式分离代码,我遇到了麻烦,试图找出如何做到这一点。

  • 我发布了一个类似的问题,我得到了一个答案,说要使用Swing Timer,但我有很大的任务要做,因为我读了repaint() isn&这个场景非常理想。这就是问题:

主要课程

Swing timer

MyPanel类

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;

    public class Main {

        private static final Main mainFrame = new Main();
        private final JFrame frame;

        private Main() {
        frame = new JFrame();
        frame.setUndecorated(true);
        frame.add(new MyPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        }

        public static Main getMainFrameInstance() {
        return mainFrame;
        }


        public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            Main.getMainFrameInstance();
            }
        });
        }

    }

1 个答案:

答案 0 :(得分:4)

这就是它的样子。您需要在EDT或Swing Timer中的某处调用以下代码。我假设您的“巨大”任务需要更新文本字段,但它也可以是任何其他UI控件。所有这些,只是为了展示一个想法。不要将其视为经过测试的代码。

//javax.swing.JTextField jfield; The field that needs to be updated. Take it from your Panel
String text = ""; // just a place holder
Object params [] = new Object []{jfield, text}; 
HugeTaskRunner ht = new HugeTaskRunner(params, new CallBack());

HugeTaskRunner派生自AbstractTaskRunner,如下所示:

public abstract class AbstractTaskRunner extends Thread {


CallBack callBack = null;
Object [] params = new Object[0];

public AbstractTaskRunner (Object [] params, CallBack callBack) {
    this.params = params;
    this.callBack = callBack;

}
public abstract void doTask ();
@Override
public void run() {
    doTask();
    if (callBack != null) {
        callBack.doCall(new Object[]{"DONE"});
    }
}

}

HugeTaskRunner:

public class HugeTaskRunner extends AbstractTaskRunner {

public HugeTaskRunner(Object[] params, CallBack callBack) {
    super(params, callBack);
    // TODO Auto-generated constructor stub
}

@Override
public void doTask() {
    // HERE YOU'LL HAVE TO DO SOME HUGE TASK ACTIONS
    // THEN YOU'LL NEED TO CALL callBack.doCall(params) to update GUI 
    String newText = "Image #1 has been loaded";
    params[params.length -1] = newText; // assuming that the last param is for updated text
    callBack.doCall(params);

}

}

CallBack类:

public class CallBack {
public void doCall (Object [] params) {
    javax.swing.SwingUtilities.invokeLater(new GUIUpdater(params, null));
}
}

GUIUpdater类:

public class GUIUpdater extends AbstractTaskRunner {

public GUIUpdater(Object[] params, CallBack callBack) {
    super(params, callBack);
}

@Override
public void doTask() {
    // UPDATE YOUR GUI HERE TAKING Swing UI objects from params, e.g.
    if (params.length == 1 && params[0].equals("DONE")) {
        // HUGE TASK IS COMPLETED, DO SOMETHING IF YOU NEED TO
    }
    else if (params.length == 2) { // It's a request to update GUI
        javax.swing.JTextField txt = (javax.swing.JTextField) this.params[0];
        txt.setText((String)this.params[1]);
    }
    else {
        // UNKNOWN REQUEST
    }

}

}