1

私は現在、大学で Java スレッドに関するレッスンを受けており、今日の演習は 2 つのスレッドの作成に関するものでした。 スレッド Aはスリープなしで 1 から 9 までの乱数を出力し、スレッド Bは 50 スリープで 1000 から 9999 までの乱数を出力します。これは、両方のスレッドを中断する JButton である停止ボタンを押すことを決定するまで無限ループです。問題は、1 つのボタンでスレッドを停止しようとして問題が発生し、主にそれを解決する方法と、その目的のために actionEvent を作成する方法を見つけようとしているということです。

これは私がこれまでに持っているコードです:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;

public class RandomNumbers extends Thread {
    long time;
    long min;
    long max;
    private JFrame window;
    private JButton stopButton;

    public RandomNumbers(long min, long max, long time) {
        this.min = min;
        this.max = max;
        this.time = time;
        new Window();
    }

    public void run() {
        try {
            while (true) {
                System.out.println((int) ((Math.random() * max) + min));
                Thread.sleep(time);
            }
        } catch (Exception e) {
            System.out.println("I was interrupted!");
        }
    }

    public class Window {
        public Window() {
            window = new JFrame("Stop Button");
            stopButton = new JButton("Stop");
            stopButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    // ThreadA.interrupt(); //problem in here , what to do ? //****
                    // ThreadB.interrupt();

                }
            });

            window.getContentPane().add(stopButton);
            window.setSize(100, 100);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
        }
    }

    public static void main(String[] args) throws InterruptedException {

        Thread threadB = new RandomNumbers(1, 9, 50);
        Thread threadA = new RandomNumbers(1000, 8999, 0);
        threadB.start();
        threadA.start();
    }
}

また、このコードには別の問題があり、コンストラクターではないため、スレッドごとに 1 つずつ、2 つの停止ボタンが作成されます。どんな助けでも大歓迎です、どうもありがとう!

4

1 に答える 1

4

スレッドのインスタンスを GUI に渡していませんが、スレッドごとに 1 つの2 つの個別の GUI を作成していますが、これは少し変わっています。プログラムの個別の部分を、おそらく個別のクラスに分けて保管してください。例えば:

  • Runnable を実装し、コードのスレッド部分を実行する MyRunnable というクラスを作成します。
  • メイン プログラム/GUI 用にそれらの配列を作成します。
  • Runnables の実行を保持するスレッドの配列を作成します。
  • 次に、ActionListener で、スレッドを中断します。

例(GUI部分のみ)、

public class ThreadTest extends JPanel {
   private JButton button = new JButton(new ButtonAction());
   private MyRunnable[] runnables = { 
         new MyRunnable("thread 1", 1, 9, 50), 
         new MyRunnable("thread 2", 1000, 8999, 1) };
   private Thread[] threads = new Thread[runnables.length];

   public ThreadTest() {
      add(button);
      for (int i = 0; i < threads.length; i++) {
         threads[i] = new Thread(runnables[i]);
         threads[i].setName(runnables[i].getName());
         threads[i].start();
      }
   }

   private class ButtonAction extends AbstractAction {
      public ButtonAction() {
         super("Stop Threads");
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         for (Thread thread : threads) {
            if (thread != null && thread.isAlive()) {
               thread.interrupt();
            }
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("ThreadTest");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ThreadTest());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2013-10-09T22:52:29.977 に答える