import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.EmptyBorder; public class LongRun extends JFrame { // This counter displays the work done in the long-running task. // The task does 1000 units of work, so it increments the counter by 1000. JLabel counter; public LongRun() { super("LongRun"); getContentPane().setLayout(new GridLayout(6, 1)); ((JComponent)getContentPane()).setBorder(new EmptyBorder(5,5,5,5)); add(counter = new JLabel("0", SwingConstants.RIGHT)); counter.setFont(new Font("sans-serif", Font.PLAIN, 20)); add(makeButton("Event Handler", new UsingEventHandler())); add(makeButton("Thread", new UsingThread())); add(makeButton("InvokeLater", new UsingInvokeLater())); add(makeButton("Timer", new UsingTimer())); add(makeButton("Reset", new ActionListener() { public void actionPerformed(ActionEvent event) { counter.setText("0"); } })); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run () { new LongRunSolution().setVisible(true); } }); } // Make one of the buttons. private JButton makeButton(String label, ActionListener action) { JButton button = new JButton(label); button.addActionListener(action); return button; } // Simulate doing a little work by just sleeping. private void doSomeWork(int amount) { try { Thread.sleep(amount); } catch (InterruptedException e) {} } // Show the work we did by incrementing the counter display. private void showWork(int amount) { int value = Integer.valueOf(counter.getText()); value += amount; counter.setText(String.valueOf(value)); } // Measure how long (in wall clock time) the task took to run. long startTime; private void startTiming() { startTime = System.currentTimeMillis(); } private void stopTiming() { long endTime = System.currentTimeMillis(); System.out.println((endTime - startTime) + " ms"); } // // Running the long task directly from an event handler. // private class UsingEventHandler implements ActionListener { public void actionPerformed(ActionEvent e1) { startTiming(); for (int i = 0; i < 1000; ++i) { doSomeWork(1); showWork(1); } stopTiming(); } } // // Running the long task in a thread. // private class UsingThread implements ActionListener { public void actionPerformed(ActionEvent e1) { startTiming(); Thread thread = new Thread(new Runnable() { public void run() { for (int i = 0; i < 1000; ++i) { doSomeWork(1); showWork(1); } stopTiming(); } }); thread.start(); // don't forget to start the thread! } } // // Running the long task in a thread, using invokeLater() // to communicate with the model and GUI objects. // private class UsingInvokeLater implements ActionListener { public void actionPerformed(ActionEvent e1) { // // FIX THIS CODE to use invokeLater() // startTiming(); Thread thread = new Thread(new Runnable() { public void run() { for (int i = 0; i < 1000; ++i) { doSomeWork(1); showWork(1); } stopTiming(); } }); thread.start(); // don't forget to start the thread! } } // // Running the long task with a timer. // private class UsingTimer implements ActionListener { public void actionPerformed(ActionEvent e1) { // // YOUR CODE HERE // } } }