質問

  

可能な重複:
   Javaコードの最適化を開始する方法? -CPUは100%です

isReset()メソッドがあり、それをクレイジーに実行しています。

   public boolean isReset() { return reset; 
   }

別のクラス。以下のクラスは、このコードを使用する唯一のクラスです。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;


// TimerQueue

public class SkaTest {
   public static final int WIDTH = 500;
   public static final int HEIGHT = 500;
   public static final int CANVAS_X = 100;
   public static final int CANVAS_Y = 100;
   public static final int CANVAS_FRAME_WIDTH = WIDTH+100;
   public static final int CANVAS_FRAME_HEIGHT = HEIGHT + 100;
   public static final int EXEC_WIDTH = 550;
   public static final int EXEC_HEIGHT = 400;

   static VizDSList dsList = new VizDSList();
   static SkaCanvas canvas = new SkaCanvas(dsList);
   static JFrame canvasFrame = new JFrame("Data Structure Canvas");
   static JMenuBar menuBar = new JMenuBar();
   static JMenu algorithmMenu = new JMenu("Algorithm");
   static JMenu dsMenu = new JMenu("Create");
   static JMenu helpMenu = new JMenu ("Help");
   static JLabel status = new JLabel(" ");   

   static SkaProgram[] alg;
   static JFrame execFrame[];
   static SkaExecutionPanel execPanel[];


   public static void setupFrames(int nAlgs) {
      int i; 

      for (i=0; i < nAlgs; i++) {
         // execFrame[i] = new JFrame("Execution Control Panel "+(i+1));
         execFrame[i] = new JFrame();
         execPanel[i] = new SkaExecutionPanel(execFrame[i]);
      }

      canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));
      canvasFrame.setSize(CANVAS_FRAME_WIDTH, CANVAS_FRAME_WIDTH);
      canvasFrame.getContentPane().setLayout(new BorderLayout(10,7));
      // canvasFrame.getContentPane().setPreferredSize(new Dimension(WIDTH, HEIGHT));
      canvasFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      // canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));

      for (i=0; i < nAlgs; i++) {
         execFrame[i].setSize(EXEC_WIDTH, EXEC_HEIGHT);
         // execFrame[i].getContentPane().setLayout(new BorderLayout(10,7));
         execFrame[i].addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
         });
         execPanel[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED));
         // execFrame[i].setContentPane(execPanel[i]);
         execFrame[i].getContentPane().add("Center", execPanel[i]);
         // execFrame[i].setLocation(CANVAS_X +CANVAS_FRAME_WIDTH, CANVAS_Y + i*EXEC_HEIGHT);
         execFrame[i].setLocation(CANVAS_X +CANVAS_FRAME_WIDTH + i*30, CANVAS_Y + i*50);
      }

      canvas.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
      canvasFrame.getContentPane().add("Center", new JScrollPane(canvas) );
      // canvasFrame.getContentPane().add("Center", new JScrollPane(canvas, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) );
      canvasFrame.getContentPane().add("South", status);
      canvasFrame.setLocation(CANVAS_X, CANVAS_Y);

      JMenu fileMenu = new JMenu("File");
      JMenuItem quitItem = new JMenuItem("Quit");
      //TODO Add quit listener
      quitItem.addActionListener(new ActionListener ()
      {


        public void actionPerformed(ActionEvent arg0) {
            //System.exit(0);

            int again = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit system", "Exiting", JOptionPane.YES_NO_OPTION);
            if (again == JOptionPane.YES_OPTION)
            {
                System.exit(0);
            }


        }

      }
      );
      fileMenu.add(quitItem);
      menuBar.add(fileMenu);
      menuBar.add(algorithmMenu);
     // menuBar.add(dsMenu);
      menuBar.add(helpMenu);
      JMenuItem help = new JMenuItem ("Help Contents");
      //help.setMnemonic(KeyEvent.VK_H);
      //TODO Fix this method
      help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.CTRL_MASK));
      help.addActionListener(new ActionListener()
      {
/*
        @Override
        public void actionPerformed(ActionEvent arg0) {
            JOptionPane.showMessageDialog(null, "Alot of the functionality have not yet been included in this version\nCurrently working on the automation features now!", "SKA 0.2 Beta", JOptionPane.WARNING_MESSAGE);

        }
*/

        public void actionPerformed(ActionEvent arg0) {
            try {
                Runtime.getRuntime().exec("hh.exe C:/ska.chm");
            } catch (IOException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "File not found", "Error", JOptionPane.ERROR_MESSAGE);
            }


        }

      });
      JMenuItem about = new JMenuItem ("About SKA");
      about.addActionListener(new ActionListener(){


        public void actionPerformed(ActionEvent arg0) {
        JOptionPane.showMessageDialog(null, "SKA Version 0.1 Beta");

        }
    });
      helpMenu.add(help);
      helpMenu.add(about);
      canvasFrame.setJMenuBar(menuBar);
   }

   /** The create menu item  */
   public static void createProgram(int i) {
      JMenuItem algItem;
      switch (i) {
      case 0 :
          alg[0] = new RedBlackValidate(canvas, execPanel[0]);
          execFrame[0].setTitle("Validate Algorithm");
          System.out.println("Validate Algorithm");
          algItem = new JMenuItem("Validate Algorithm");
          algorithmMenu.add(algItem);
          break;
      default:
         break;         
      }
   }


   public static void main(String args[]) {
      int i, nAlgs = 1; //nAlgs = 7;

      alg = new SkaProgram[nAlgs];
      execPanel = new SkaExecutionPanel[nAlgs];
      execFrame = new JFrame[nAlgs];

      // canvas.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
      setupFrames(nAlgs);
      canvasFrame.setVisible(true);


      for (i=0; i < alg.length; i++) {
         createProgram(i);
         execFrame[i].setVisible(true);
         alg[i].start();
         alg[i].displayAlgorithm();
      }

      while (true) {
         for (i=0; i < alg.length; i++) 
            if (execPanel[i].isReset()) {
               alg[i].terminate();
               createProgram(i);
               alg[i].start();
               execPanel[i].unreset();
            }         
      }
   }
}  // End class SkaTest
役に立ちましたか?

解決

その isReset に問題はありませんが、このループは何度も何度も呼び出すことはありませんか?

 while (true) {
     for (i=0; i < alg.length; i++) 
        if (execPanel[i].isReset()) {
           alg[i].terminate();
           createProgram(i);
           alg[i].start();
           execPanel[i].unreset();
        }         
  }

他に何もなければ、 Thread.yield()を平手打ちして、スレッドがCPUを独占しないように定期的にタイムスライスを放棄させることができます。

より良いのは、ポーリングを捨てて、代わりにリスナーベースのアプローチを使用することです。 execPanel が、リセットされたときにリスナーに繰り返し通知するのではなく、リセットされたときにリスナーに通知することを許可します。このコードは&quot;まだありますか?まだありますか?まだありますか?&quot;

観測者と呼ばれるリスナーが表示されることもあります。このデザインパターンについての詳細を調べてみると、多くの良い情報が得られます。両方の用語を使用して表示します。

リスナーの概念を使用するようにコードを書き換える方法を次に示します。このコードのエラーをおaびします。コンパイルしていません。コードにエラーがある可能性があるにもかかわらず、アイデアが明確であることを願っています。

コードがすぐに意味をなさない場合は、上記のリンクをチェックしてください。または、 EventListener をいじることができます。 s 。これは、Swing GUIプログラミングを行うときに常に使用します。

public class SkaExecutionPanel {
    // Anybody who wants to be told when a panel is reset will implement
    // this interface and call addListener().
    public interface Listener {
        void panelReset(SkaExecutionPanel panel);
    }

    // Each panel needs to maintain an internal list of listeners.
    private List<Listener> listeners = new ArrayList<Listener>();

    public void addListener(Listener listener) {
        listeners.add(listener);
    }

    public void removeListener(Listener listener) {
        listeners.remove(listener);
    }

    public void reset() {
        /* Do your thing. */

        // When something happens that the listeners will care about, such as a
        // call to reset() in this case, you iterate through the list and tell
        // each one what's happened.
        for (Listener listener: listeners) {
            listener.panelReset(this);
        }
    }
}

public class SkaTest {
    public static void main(String args[]) {
        /* Snippety snip. */

        for (i=0; i < alg.length; i++) {
            // Here we need to tell the execPanels that we want to do something
            // whenever somebody calls reset() on them. We will add a listener
            // to each panel that does the terminate/createProgram/start dance.
            // This way we don't have to ask the panels when they are reset; 
            // instead they will tell us when that happens.
            execPanel[i].addListener(new SkaExecutionPanel.Listener() {
                final int index = i;

                public void panelReset(SkaExecutionPanel panel) {
                    alg[index].terminate();
                    createProgram(index);
                    alg[index].start();
                    execPanel[index].unreset();
                }
            });
        }
    }
}

他のヒント

これは、この情報を追加した後の最初の質問に対する私の回答の編集からです。

&quot; EDIT:whileループは無限ループであり、forループが実行され、配列内のすべての項目がリセットされたかどうかが確認されます。これをオブザーバーパターンに置き換えることができます。オブザーバーパターンは、オブジェクトがリセットされると、オブザーバーオブジェクトに通知し、オブザーバーはその一連のステップを実行します。この方法では、無限ループは発生せず、.isReset()の使用を削減します。&quot;

編集:他の答えの問題は、私の意見では無限ループを使用するのは少しコードが臭いということです。

編集2:ウィキペディアの記事と例を示します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top