久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<legend id='FTJBT'><style id='FTJBT'><dir id='FTJBT'><q id='FTJBT'></q></dir></style></legend>

<small id='FTJBT'></small><noframes id='FTJBT'>

      <i id='FTJBT'><tr id='FTJBT'><dt id='FTJBT'><q id='FTJBT'><span id='FTJBT'><b id='FTJBT'><form id='FTJBT'><ins id='FTJBT'></ins><ul id='FTJBT'></ul><sub id='FTJBT'></sub></form><legend id='FTJBT'></legend><bdo id='FTJBT'><pre id='FTJBT'><center id='FTJBT'></center></pre></bdo></b><th id='FTJBT'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='FTJBT'><tfoot id='FTJBT'></tfoot><dl id='FTJBT'><fieldset id='FTJBT'></fieldset></dl></div>
      1. <tfoot id='FTJBT'></tfoot>
          <bdo id='FTJBT'></bdo><ul id='FTJBT'></ul>

      2. SwingWorker 沒有響應

        SwingWorker not responding(SwingWorker 沒有響應)

            <legend id='mjkHP'><style id='mjkHP'><dir id='mjkHP'><q id='mjkHP'></q></dir></style></legend>
            <i id='mjkHP'><tr id='mjkHP'><dt id='mjkHP'><q id='mjkHP'><span id='mjkHP'><b id='mjkHP'><form id='mjkHP'><ins id='mjkHP'></ins><ul id='mjkHP'></ul><sub id='mjkHP'></sub></form><legend id='mjkHP'></legend><bdo id='mjkHP'><pre id='mjkHP'><center id='mjkHP'></center></pre></bdo></b><th id='mjkHP'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='mjkHP'><tfoot id='mjkHP'></tfoot><dl id='mjkHP'><fieldset id='mjkHP'></fieldset></dl></div>
                  <tbody id='mjkHP'></tbody>
              • <tfoot id='mjkHP'></tfoot>

                <small id='mjkHP'></small><noframes id='mjkHP'>

                • <bdo id='mjkHP'></bdo><ul id='mjkHP'></ul>
                  本文介紹了SwingWorker 沒有響應的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想做什么?

                  點擊Start JButtonSwingWorker就會執行.在 doInBackground() 方法內部,我將 arrNames 的每個索引傳遞給 publish() 方法,以便在內部顯示JTextArea.

                  At the click of the Start JButton, the SwingWorker will execute. Inside the doInBackground() method, I am passing each index of arrNames, to the publish() method, so that it can be displayed inside the JTextArea.

                  發生了什么?

                  如果我保留行 System.out.format("Counter : %d%n", counter); 作為評論,在我的 SwingWorkerdoInBackground() 方法中,SwingWorker 正在按預期工作.雖然如果我將其注釋掉,那么 SwingWorker 停止響應.

                  If I do not keep line System.out.format("Counter : %d%n", counter); as a comment, in my doInBackground() method of the SwingWorker, then the SwingWorker is working as expected. Though if I comment it out, then the SwingWorker stops responding.

                  我做錯了嗎?

                  java version "1.7.0_25"
                  Java(TM) SE Runtime Environment (build 1.7.0_25-b16)
                  Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)
                  

                  這是我正在使用的代碼:

                  Here is the code I am using :

                  import java.awt.*;
                  import java.awt.event.*;
                  import javax.swing.*;
                  
                  public class SwingWorkerExample1
                  {
                      private JLabel statusLabel;
                      private JTextArea tArea;
                      private JButton startButton;
                      private JButton stopButton;
                  
                      private BackgroundTask backgroundTask;
                  
                      private ActionListener buttonActions =
                                              new ActionListener()
                      {
                          @Override
                          public void actionPerformed(ActionEvent ae)
                          {
                              JButton source = (JButton) ae.getSource();
                              if (source == startButton)
                              {
                                  startButton.setEnabled(false);
                                  stopButton.setEnabled(true);
                                  backgroundTask = new BackgroundTask();
                                  backgroundTask.execute();
                              }
                              else if (source == stopButton)
                              {
                                  backgroundTask.cancel(true);
                                  stopButton.setEnabled(false);
                                  startButton.setEnabled(true);
                              }
                          }
                      };
                  
                      private void displayGUI()
                      {
                          JFrame frame = new JFrame("Swing Worker Example");
                          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                  
                          JPanel contentPane = new JPanel();
                          contentPane.setBorder(
                              BorderFactory.createEmptyBorder(5, 5, 5, 5));
                          contentPane.setLayout(new BorderLayout(5, 5));
                  
                          statusLabel = new JLabel("Status Bar", JLabel.CENTER);
                  
                          tArea = new JTextArea(20, 20);
                          tArea.setWrapStyleWord(true);
                          tArea.setLineWrap(true);        
                          JScrollPane textScroller = new JScrollPane();
                          textScroller.setBorder(
                              BorderFactory.createTitledBorder("Textual OUTPUT : "));
                          textScroller.setViewportView(tArea);
                  
                          startButton = new JButton("Start");
                          startButton.addActionListener(buttonActions);
                          stopButton = new JButton("Stop");
                          stopButton.setEnabled(false);
                          stopButton.addActionListener(buttonActions);
                          JPanel buttonPanel = new JPanel();
                          buttonPanel.add(startButton);
                          buttonPanel.add(stopButton);
                  
                          contentPane.add(statusLabel, BorderLayout.PAGE_START);
                          contentPane.add(textScroller, BorderLayout.CENTER);
                          contentPane.add(buttonPanel, BorderLayout.PAGE_END);
                  
                          frame.setContentPane(contentPane);
                          frame.pack();
                          frame.setLocationByPlatform(true);
                          frame.setVisible(true);
                      }
                  
                      private class BackgroundTask extends SwingWorker<Void, String>
                      {
                          private int counter = 0;
                  
                          private String[] arrNames = { "US Rates Strategy Cash",
                              "Pavan Wadhwa(1-212) 844-4597", "Srini Ramaswamy(1-212) 844-4983",
                              "Meera Chandan(1-212) 855-4555", "Kimberly Harano(1-212) 823-4996",
                              "Feng Deng(1-212) 855-2555", "US Rates Strategy Derivatives",
                              "Srini Ramaswamy(1-212) 811-4999",
                              "Alberto Iglesias(1-212) 898-5442",
                              "Praveen Korapaty(1-212) 812-3444", "Feng Deng(1-212) 812-2456",
                              "US Rates Strategy Derivatives", "Srini Ramaswamy(1-212) 822-4999",
                              "Alberto Iglesias(1-212) 822-5098",
                              "Praveen Korapaty(1-212) 812-3655", "Feng Deng(1-212) 899-2222" };
                  
                          public BackgroundTask()
                          {
                              statusLabel.setText((this.getState()).toString());
                              System.out.println(this.getState());
                          }
                  
                          @Override
                          protected Void doInBackground()
                          {
                              statusLabel.setText((this.getState()).toString());
                              System.out.println(this.getState());
                              while (!isCancelled())
                              {
                                  counter %= arrNames.length;
                                  //System.out.format("Counter : %d%n", counter);
                                  publish(arrNames[counter]);
                                  counter++;
                              }
                              statusLabel.setText((this.getState()).toString());
                              System.out.println(this.getState());
                              return null;
                          }
                  
                          @Override
                          protected void process(java.util.List<String> messages)
                          {
                              for (String message : messages)
                                  tArea.append(String.format(message + "%n"));
                          }
                      }
                  
                      public static void main(String[] args)
                      {
                          Runnable runnable = new Runnable()
                          {
                              @Override
                              public void run()
                              {
                                  new SwingWorkerExample1().displayGUI();
                              }
                          };
                          EventQueue.invokeLater(runnable);
                      }
                  }
                  

                  編輯 1:

                  按照建議,如果我添加 Thread.sleep(...),它確實可以工作,但它會拋出一個 InterruptedException,如下所示.所以這個技巧奏效了.但這是合法的執行方式嗎?

                  As suggested, if I add Thread.sleep(...), it does work, though, it throws a InterruptedException as shown below. So the trick works. But is this the legitimate way of performing it ?

                  C:MineJAVAJ2SEclasses>java SwingWorkerExample1
                  PENDING
                  STARTED
                  java.lang.InterruptedException: sleep interrupted
                          at java.lang.Thread.sleep(Native Method)
                          at SwingWorkerExample1$BackgroundTask.doInBackground(SwingWorkerExample1.java:108)
                          at SwingWorkerExample1$BackgroundTask.doInBackground(SwingWorkerExample1.java:76)
                          at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
                          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
                          at java.util.concurrent.FutureTask.run(FutureTask.java:166)
                          at javax.swing.SwingWorker.run(SwingWorker.java:335)
                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
                          at java.lang.Thread.run(Thread.java:724)
                  DONE
                  

                  <小時>

                  編輯 2:

                  只有 doInBackground() 發生了變化,引發了上述異常:


                  EDIT 2 :

                  Only doInBackground() is changed, that raised the above exception :

                  @Override
                  protected Void doInBackground()
                  {
                      Runnable runnable = new Runnable()
                      {
                          @Override
                          public void run()
                          {
                              statusLabel.setText((BackgroundTask.this.getState()).toString());
                          }
                      };
                      EventQueue.invokeLater(runnable);
                  
                      System.out.println(this.getState());
                      while (!isCancelled())
                      {
                          counter %= arrNames.length;             
                          //System.out.format("Counter : %d%n", counter);
                          publish(arrNames[counter]);
                          try
                          {Thread.sleep(30);}
                          catch(InterruptedException ie)
                          {ie.printStackTrace();}
                          counter++;
                      }
                      runnable = new Runnable()
                      {
                          @Override
                          public void run()
                          {
                              statusLabel.setText((BackgroundTask.this.getState()).toString());
                          }
                      };
                      EventQueue.invokeLater(runnable);
                      System.out.println(this.getState());
                      return null;
                  }
                  

                  推薦答案

                  如果我添加 Thread.sleep(...),它確實有效,但它會拋出一個中斷異常

                  if I add Thread.sleep(...), it does work, though, it throws a InterruptedException

                  顯然產生異常的代碼(復制自 OP 的編輯):

                  The code that apparently produces the exception (copied from OP's edit):

                  while (!isCancelled()) {
                      counter %= arrNames.length;
                      // System.out.format("Counter : %d%n", counter);
                      publish(arrNames[counter]);
                      try {
                          Thread.sleep(30); // throws
                      } catch (InterruptedException ie) {
                          ie.printStackTrace();
                      }
                      counter++;
                  }
                  

                  不過,原因是取消工作人員的代碼(在 actionListener 中):

                  The reason, though, is the code that cancels the worker (in the actionListener):

                  backgroundTask.cancel(true);
                  

                  它明確告訴工作人員通過 .. 中斷線程來取消.從它的 api 文檔:

                  which explicitly tells the worker to cancel by .. interrupting the thread. From its api doc:

                  mayInterruptIfRunning - 如果執行此任務的線程應該被中斷,則為 true;否則,允許完成正在進行的任務

                  mayInterruptIfRunning - true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete

                  順便說一句:捕獲異常并且什么都不做(從而有效地忽略中斷)并不是最好的想法.在這種情況下可能不會造成太大的破壞 - 由于檢查了取消的狀態.典型的工作實現要么在需要時進行一些內部清理之后捕獲并返回,要么根本不處理它.

                  As an aside: catching the exception and doing nothing (thereby effectively ignoring the interrupt) is not the best of ideas. Probably not too damaging in this case - due to checking the cancelled status. Typical worker implementations either catch and return, after doing some internal cleanup if needed, or don't handle it at all.

                  這篇關于SwingWorker 沒有響應的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                • <small id='oHhBJ'></small><noframes id='oHhBJ'>

                  <legend id='oHhBJ'><style id='oHhBJ'><dir id='oHhBJ'><q id='oHhBJ'></q></dir></style></legend>

                        <bdo id='oHhBJ'></bdo><ul id='oHhBJ'></ul>
                        • <i id='oHhBJ'><tr id='oHhBJ'><dt id='oHhBJ'><q id='oHhBJ'><span id='oHhBJ'><b id='oHhBJ'><form id='oHhBJ'><ins id='oHhBJ'></ins><ul id='oHhBJ'></ul><sub id='oHhBJ'></sub></form><legend id='oHhBJ'></legend><bdo id='oHhBJ'><pre id='oHhBJ'><center id='oHhBJ'></center></pre></bdo></b><th id='oHhBJ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='oHhBJ'><tfoot id='oHhBJ'></tfoot><dl id='oHhBJ'><fieldset id='oHhBJ'></fieldset></dl></div>

                              <tbody id='oHhBJ'></tbody>
                          • <tfoot id='oHhBJ'></tfoot>
                            主站蜘蛛池模板: 国产精品欧美一区二区三区不卡 | 丁香久久 | 毛片网站在线观看视频 | 国产人成精品一区二区三 | 国产乱人伦精品一区二区 | 久产久精国产品 | 成人高清在线视频 | 一级黄色短片 | 亚洲一一在线 | 成人在线观看免费爱爱 | 日韩精品av一区二区三区 | 久久精品视频网站 | 4hu最新网址 | 亚洲电影专区 | 不卡的av电影 | 激情a| 亚洲精品久久久一区二区三区 | 国产精品久久久免费 | 欧美精品欧美精品系列 | 精品不卡 | 99久视频| 国产一级影片 | 成人免费小视频 | 成人午夜黄色 | 午夜欧美一区二区三区在线播放 | 手机av在线| 日韩图区 | 日韩在线精品 | 久久精品久久久久久 | 神马久久春色视频 | 国产在线视频一区二区 | 天天干天天草 | 久久久91 | 草久在线| 久久aⅴ乱码一区二区三区 91综合网 | 干狠狠| 狠狠躁夜夜躁人人爽天天高潮 | 久久专区| 国产第一页在线观看 | 亚洲精品一区中文字幕乱码 | 婷婷综合激情 |