Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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/3/arrays/13.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 用线程填充JList_Java_Arrays_Multithreading_Jlist_Defaultlistmodel - Fatal编程技术网

Java 用线程填充JList

Java 用线程填充JList,java,arrays,multithreading,jlist,defaultlistmodel,Java,Arrays,Multithreading,Jlist,Defaultlistmodel,我希望用多个线程填充JList。 我尝试了这种方法,但jlist是空的。 如果jlist能够实时更新就好了 有两个线程,另一个线程加载到另一个方向 new Thread(new Runnable() { @Override public void run() { for(i=0; i<cells.size()/2; i++){ System.out

我希望用多个线程填充JList。 我尝试了这种方法,但jlist是空的。 如果jlist能够实时更新就好了 有两个线程,另一个线程加载到另一个方向

            new Thread(new Runnable() {
            @Override
            public void run() {
                for(i=0; i<cells.size()/2; i++){
                    System.out.println("thread");

                    try{
                        HtmlPage p = client.getPage("https://tbilisi.embassytools.com/en/slotsReserve?slot="+cells.get(i).getAttribute("data-slotid"));
                        pages.add(p);
                        if(!p.getUrl().toString().contains("slotsReserve"))
                            model.add(i,p.getUrl().toString());
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        });
list1.setModel(model)
new线程(new Runnable()){
@凌驾
公开募捐{
对于(i=0;iSwing is),您应该使用SwingUtilities运行多线程更新Swing

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      doWhateverYouWant();
    }
});

Swing是您应该使用SwingUtilities来运行多线程更新Swing

javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      doWhateverYouWant();
    }
});

Swing是一个单线程框架,也就是说,对UI的所有更新和修改都应该在事件调度线程的上下文中完成

同样,您不应该在EDT中执行任何可能阻止或以其他方式阻止它处理事件队列的操作(如从web下载内容)

这带来了一个难题。无法在EDT之外更新UI,需要使用某种后台进程来执行耗时/阻塞任务

只要项目的顺序不重要,您就可以使用多个
SwingWorker
s来代替
线程的o,例如

DefaultListModel model = new DefaultListModel();

/*...*/

LoadWorker worker = new LoadWorker(model);
worker.execute();    

/*...*/

public class LoaderWorker extends SwingWorker<List<URL>, String> {

    private DefaultListModel model;

    public LoaderWorker(DefaultListModel model) {
        this.model = model;
    }

    protected void process(List<String> pages) {
        for (String page : pages) {
            model.add(page);
        }
    }

    protected List<URL> doInBackground() throws Exception {
        List<URL> urls = new ArrayList<URL>(25);
        for(i=0; i<cells.size()/2; i++){
            try{
                HtmlPage p = client.getPage("https://tbilisi.embassytools.com/en/slotsReserve?slot="+cells.get(i).getAttribute("data-slotid"));
                pages.add(p);
                if(!p.getUrl().toString().contains("slotsReserve")) {
                    publish(p.getUrl().toString());
                    urls.add(p.getUrl());
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }        
        }
        return urls;
    }
} 
DefaultListModel=newDefaultListModel();
/*...*/
LoadWorker=新LoadWorker(型号);
worker.execute();
/*...*/
公共类LoaderWorker扩展SwingWorker{
私有模型;
公共装载机工人(DefaultListModel模型){
this.model=模型;
}
受保护的无效进程(列表页){
用于(字符串页:页){
模型。添加(第页);
}
}
受保护列表doInBackground()引发异常{
列表URL=新的ArrayList(25);

对于(i=0;iSwing是一个单线程框架,也就是说,对UI的所有更新和修改都应该在事件调度线程的上下文中完成

同样,您不应该在EDT中执行任何可能阻止或以其他方式阻止它处理事件队列的操作(如从web下载内容)

这带来了一个难题。无法在EDT之外更新UI,需要使用某种后台进程来执行耗时/阻塞任务

只要项目的顺序不重要,您就可以使用多个
SwingWorker
s来代替
线程的o,例如

DefaultListModel model = new DefaultListModel();

/*...*/

LoadWorker worker = new LoadWorker(model);
worker.execute();    

/*...*/

public class LoaderWorker extends SwingWorker<List<URL>, String> {

    private DefaultListModel model;

    public LoaderWorker(DefaultListModel model) {
        this.model = model;
    }

    protected void process(List<String> pages) {
        for (String page : pages) {
            model.add(page);
        }
    }

    protected List<URL> doInBackground() throws Exception {
        List<URL> urls = new ArrayList<URL>(25);
        for(i=0; i<cells.size()/2; i++){
            try{
                HtmlPage p = client.getPage("https://tbilisi.embassytools.com/en/slotsReserve?slot="+cells.get(i).getAttribute("data-slotid"));
                pages.add(p);
                if(!p.getUrl().toString().contains("slotsReserve")) {
                    publish(p.getUrl().toString());
                    urls.add(p.getUrl());
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }        
        }
        return urls;
    }
} 
DefaultListModel=newDefaultListModel();
/*...*/
LoadWorker=新LoadWorker(型号);
worker.execute();
/*...*/
公共类LoaderWorker扩展SwingWorker{
私有模型;
公共装载机工人(DefaultListModel模型){
this.model=模型;
}
受保护的无效进程(列表页){
用于(字符串页:页){
模型。添加(第页);
}
}
受保护列表doInBackground()引发异常{
列表URL=新的ArrayList(25);
对于(i=0;i)你不应该
start()
你的线程。你不应该这样做,因为Swing不是线程安全的。阅读“SwingWorker”你不
start()
你的线程。你不应该这样做,因为Swing不是线程安全的。阅读“SwingWorker”