Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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_Swing_Swingworker - Fatal编程技术网

Java 需要更新变量、主线程还是不同的线程?

Java 需要更新变量、主线程还是不同的线程?,java,multithreading,swing,swingworker,Java,Multithreading,Swing,Swingworker,抱歉,如果这个问题听起来很愚蠢,我已经没有太长时间编程了,所以我有这样的新手问题 我有一个JFrame(来自netBeans的JFrame表单)和一些变量(arrayList,int…)。经过一个过程后,这些变量会发生变化。这个过程是一个通过JCDB驱动程序的mySQL查询,一些arrayList数据更新,我用它来填充Jtable。。。等等 起初(可怜的我)我做了一个荡秋千的工人。通过SwingWorker构造函数,我传递这些变量(大约6个变量左右),并使用它们进行处理和填充表格 我想我可以在重

抱歉,如果这个问题听起来很愚蠢,我已经没有太长时间编程了,所以我有这样的新手问题

我有一个JFrame(来自netBeans的JFrame表单)和一些变量(arrayList,int…)。经过一个过程后,这些变量会发生变化。这个过程是一个通过JCDB驱动程序的mySQL查询,一些arrayList数据更新,我用它来填充Jtable。。。等等

起初(可怜的我)我做了一个荡秋千的工人。通过SwingWorker构造函数,我传递这些变量(大约6个变量左右),并使用它们进行处理和填充表格

我想我可以在重写的Done()方法中更新这些变量的值(我又可怜了),而不仅仅是GUI组件

我从这次失败中学到了很多东西: 1) 尽管我通过构造函数传递了变量,但这并不意味着它们会在它们来自的地方得到更新。 2) SwingWorker只能返回1个变量,并修改GUI组件

所以,这是我的主要观点,我怎样才能做我想做的事?我知道SwingWorker课程是做不到的,但是,怎么做呢

我不想像那样将代码放在鼠标点击事件中,因为它会阻止我的EDT,而不会通知用户发生了什么

我想这样做:将代码放在鼠标单击事件中,并显示一个对话框,以便用户知道此时正在进行一个过程

private void jButton_calcular_rutaMousePressed(java.awt.event.MouseEvent evt) {                                                   
        // BOTON CALCULAR RUTA

        int index = jL_empGeo.getSelectedIndex();

        JOptionPane optionPane = new JOptionPane("Descargando ruta, espere por favor.", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
        JDialog dialog = new JDialog();
        dialog.setTitle("Descarga");
        dialog.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);

        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        dialog.setLocation(dim.width / 2 - dialog.getSize().width / 2, dim.height / 2 - dialog.getSize().height / 2);
        dialog.setContentPane(optionPane);

        dialog.pack();

        dialog.setLocationRelativeTo(this);

        dialog.setVisible(true);

        //
        // THE PROCESSING TAKES PLACE HERE
        //
        //
        // mySQL query
        //
        // update arrayLists and variables
        //
        // update GUI components
        //
        //

        dialog.setVisible(false);
    }
这有什么意义吗?有没有办法在不同的线程中进行处理,然后将ArrayList和变量返回到主线程中

提前感谢你的帮助

编辑

SwingWorker类

package descargas;

import clases.Empleados;
import clases.InfoPuntoRuta;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Dimension;
import java.awt.Image;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
import maps.java.Geocoding;
import maps.java.Route;
import maps.java.StaticMaps;

public class ActualizarRuta2 extends SwingWorker<Void, Void> {

    int index;
    ArrayList<Empleados> arrayEmpleados;
    ArrayList<InfoPuntoRuta> listaPuntosRuta;
    DefaultTableModel modeloTablaRutas;
    JDialog dialog;
    ArrayList<String> listaIntermedios;
    JLabel jLab_RutaMAP;
    ImageIcon icono;
    int origenInt;
    int destinoInt;
    int destinoMax;

    public ActualizarRuta2(int index, int origenInt, int destinoInt, int destinoMax, ArrayList<Empleados> arrayEmpleados, ArrayList<InfoPuntoRuta> listaPuntosRuta, DefaultTableModel modeloTablaRutas, JDialog dialog, JLabel jLab_RutaMAP) {
        this.index = index;
        this.arrayEmpleados = arrayEmpleados;
        this.listaPuntosRuta = listaPuntosRuta;
        this.modeloTablaRutas = modeloTablaRutas;
        this.dialog = dialog;
        this.jLab_RutaMAP = jLab_RutaMAP;
        this.origenInt = origenInt;
        this.destinoInt = destinoInt;
        this.destinoMax = destinoMax;
    }

    @Override
    protected Void doInBackground() throws Exception {

        listaIntermedios = new ArrayList<String>();

        // recogemos la fecha actual
        DateFormat formatoFecha = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        String dateMIN = formatoFecha.format(cal.getTime()) + " 00:00:01";
        String dateMAX = formatoFecha.format(cal.getTime()) + " 23:59:59";

        // GENERAR MAPA DE LA RUTA DEL DIA
        Connection conexion;
        conexion = conexiondb.ConexionDB.getConnection();
        if (conexion != null) {
            try {
                Statement st;
                ResultSet rs;

                st = (Statement) conexion.createStatement();
                rs = st.executeQuery("SELECT * \n"
                        + "FROM position\n"
                        + "WHERE nombre =  '" + arrayEmpleados.get(index) + "'\n"
                        + "AND position_date BETWEEN '" + dateMIN + "' AND '" + dateMAX + "'\n"
                        + "ORDER BY position_date;");

                // vaciamos la lista
                //listaPuntosRuta.clear();
                //System.out.println(rs.);
                // rellenamos la lista
                rs.beforeFirst();

                while (rs.next()) {
                    InfoPuntoRuta punto = new InfoPuntoRuta(rs.getString("nombre"),
                            rs.getString("position_date"), rs.getDouble("latitud"),
                            rs.getDouble("longitud"));

                    Geocoding ObjGeocod = new Geocoding();
                    ArrayList<String> resultadoCI = ObjGeocod.getAddress(punto.getLatitud(), punto.getLongitud());
                    String direccion = resultadoCI.get(0);

                    punto.setDireccion(direccion);

                    listaIntermedios.add(direccion);
                    listaPuntosRuta.add(punto);
                }

                //
                // DESCARGAR MAPA RUTA
                //
                int posicionUltimo = (listaIntermedios.size()) - 1;
                String origen, destino;

                ArrayList<String> waypoints = new ArrayList<String>();
                if (listaIntermedios.size() < 10) {
                    origen = listaIntermedios.get(0);
                    destino = listaIntermedios.get(posicionUltimo);
                    for (int i = 1; i < posicionUltimo; i++) {
                        waypoints.add(listaIntermedios.get(i));
                    }
                } else {
                    origen = listaIntermedios.get(0);
                    destino = listaIntermedios.get(9);
                    for (int i = 1; i < 9; i++) {
                        waypoints.add(listaIntermedios.get(i));
                    }
                }

//                ArrayList<String> prueba = new ArrayList<String>();
//                prueba.add("coruña avenida finisterre 65");
//                prueba.add("coruña ronda de outeiro 125");
//                prueba.add("coruña avenida del ejercito 20");
                Route ObjRout = new Route();

                String[][] resultadoRuta = ObjRout.getRoute(origen, destino, waypoints, Boolean.TRUE, Route.mode.driving, Route.avoids.nothing);
                //String[][] resultadoRuta = ObjRout.getRoute("Madrid", "Barcelona", prueba, Boolean.TRUE, Route.mode.driving, Route.avoids.nothing);
//                String[][] resultadoRuta = ObjRout.getRoute("coruña virrey ossorio 25", "pla y cancela, 16, 15005 la coruña, españa", prueba, Boolean.TRUE, Route.mode.driving, Route.avoids.nothing);
                String polylinea = ObjRout.getGeneralPolyline();
                StaticMaps ObjStatMap = new StaticMaps();
                Image resultadoMapa = ObjStatMap.getStaticMapRoute(new Dimension(585, 405), 1, StaticMaps.Format.png, StaticMaps.Maptype.hybrid, polylinea);
                icono = new ImageIcon(resultadoMapa);

                // RELLENAR TABLA DETALLES DE LA RUTA
                modeloTablaRutas.setRowCount(0);

            } catch (SQLException | UnsupportedEncodingException | MalformedURLException ex) {
                Logger.getLogger(ActualizarRuta2.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    conexion.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ActualizarRuta2.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return null;
    }

    @Override
    protected void done() {

        dialog.dispose();

        // RELLENAR TABLA DETALLES DE LA RUTA
        for (InfoPuntoRuta p : listaPuntosRuta) {
            modeloTablaRutas.addRow(new Object[]{
                p.getNombre(), p.getFecha(), p.getDireccion()});
        }

        // ponemos la imagen de la ruta
        jLab_RutaMAP.setIcon(icono);

        //
        listaIntermedios.clear();

//        listaPuntosRuta.clear();
        origenInt = 0;
        destinoMax = listaPuntosRuta.size() - 1;
        if (destinoMax < 9) {
            destinoInt = destinoMax;
        } else {
            destinoInt = 9;
        }

    }

}
有没有办法在不同的线程中进行处理,然后将ArrayList和变量返回到主线程中

是的,有。您可以(也可能应该)在
dialog.setVisible(true)
之后使用

new Thread() {
    @Override
    public void run() {
        getNewValues();
    }
}.start();
完成此任务后,您可以从“worker”线程调用

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        //overwrite existing variables, update GUI elements, trigger repaints, etc.
        dialog.setVisible(false);
    }
};
但是,您应该始终尝试将数据与视图分开。这意味着:您可能不应该从工作线程调用这些操作,而是应该实现以通知GUI数据已更改并刷新自身

有没有办法在不同的线程中进行处理,然后将ArrayList和变量返回到主线程中

是的,有。您可以(也可能应该)在
dialog.setVisible(true)
之后使用

new Thread() {
    @Override
    public void run() {
        getNewValues();
    }
}.start();
完成此任务后,您可以从“worker”线程调用

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        //overwrite existing variables, update GUI elements, trigger repaints, etc.
        dialog.setVisible(false);
    }
};
但是,您应该始终尝试将数据与视图分开。这意味着:您可能不应该从工作线程调用这些操作,而是应该实现以通知GUI数据已更改并刷新自身

有没有办法在不同的线程中进行处理,然后将ArrayList和变量返回到主线程中

是的,有。您可以(也可能应该)在
dialog.setVisible(true)
之后使用

new Thread() {
    @Override
    public void run() {
        getNewValues();
    }
}.start();
完成此任务后,您可以从“worker”线程调用

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        //overwrite existing variables, update GUI elements, trigger repaints, etc.
        dialog.setVisible(false);
    }
};
但是,您应该始终尝试将数据与视图分开。这意味着:您可能不应该从工作线程调用这些操作,而是应该实现以通知GUI数据已更改并刷新自身

有没有办法在不同的线程中进行处理,然后将ArrayList和变量返回到主线程中

是的,有。您可以(也可能应该)在
dialog.setVisible(true)
之后使用

new Thread() {
    @Override
    public void run() {
        getNewValues();
    }
}.start();
完成此任务后,您可以从“worker”线程调用

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        //overwrite existing variables, update GUI elements, trigger repaints, etc.
        dialog.setVisible(false);
    }
};

但是,您应该始终尝试将数据与视图分开。这意味着:您可能不应该从工作线程调用这些操作,而应该实现以通知GUI数据已更改并刷新自身。

您需要与“SwingWorker”进行通信,返回GUI。Java参数是按值传递的——因此,您需要某种形式的值持有者

使“数据bean”从可观察状态下降可能是最简单的方法

public class QueryResults extends Observable {
    protected List<SomeItem> itemList;
    // getters and setters.
}
公共类查询结果扩展了可观察{
受保护清单项目清单;
//能手和二传手。
}
从SwingWorker,您需要存储结果并通知观察者。通知必须在事件调度线程上完成,javax.swing.SwingWorker提供了内置功能,可以在EDT上调用SwingWorker.done()

我使用了'Void'类型作为“结果”,因为数据持有者已经存在&我们只是将数据加载到其中。UI应该已经绑定到(并观察)它。大概是这样的:

public class QueryWorker<Void,Void> extends SwingWorker {
    protected QueryResults holder;
    public QueryWorker (QueryResults holder) {
        this.holder = holder;  // keep the holder & put data into it.
    }

    @Override
    protected Void doInBackground() {
        // run the query..
    }
    @Override
    protected void done() {
        holder.notifyObservers();
    }
}
公共类QueryWorker扩展SwingWorker{
受保护的查询结果持有者;
公共查询工作人员(查询结果持有者){
this.holder=holder;//保留holder并将数据放入其中。
}
@凌驾
受保护的Void doInBackground(){
//运行查询。。
}
@凌驾
受保护的void done(){
holder.notifyObservers();
}
}

这是一个简化的例子。它忽略了在加载过程中想要读取结果HODLE(用于显示刷新)的UI的可能性。您的备选方案是将数据bean与bean的可观察引用分开。

您需要与“SwingWorker”进行通信,然后返回GUI。Java参数是按值传递的——因此,您需要某种形式的值持有者

使“数据bean”从可观察状态下降可能是最简单的方法

public class QueryResults extends Observable {
    protected List<SomeItem> itemList;
    // getters and setters.
}
公共类查询结果扩展了可观察{
受保护清单项目清单;
//能手和二传手。
}
从SwingWorker那里,您需要存储结果&否