[an error occurred while processing this directive]
[an error occurred while processing this directive]

AC23: Threads & Timers

The purpose of this activity is to experiment with ways to do background processing in graphical user interfaces. You will need to download http://courses.csail.mit.edu/6.831/handouts/ac23/LongRun.java, which is the Java code you'll be using, and build it with Eclipse or javac.

1 Long Task in Event Handler

Run the Java program. You'll see a counter, initially at 0. The job of the long task is to increment this counter by 1000 units (and simulating computation or I/O by spending about 1 millisecond to do each increment). So the correct behavior is that you should see the counter rapidly increase from 0 to 1000.

Press the Event Handler button. What actually happens? Look at the code. Why does it have the effect you saw?

2 Long Task in Thread

Now click the Thread button, and examine the corresponding code. What happens?

Now click the Thread button twice quickly, so that two threads are running at the same time. After both threads are done, you'd expect to see the counter increase by 2000. What actually happens? Why?

3 Using invokeLater()

Now modify the code for the UsingInvokeLater button so that it calls SwingUtilities.invokeLater() to avoid the problem you saw with the Thread button. Click it a few times quickly to make sure you fixed it, and make sure the counter still increases smoothly.

4 Using Timers

Now implement the body of the UsingTimer button so that it uses javax.swing.Timer to do the work in the background.

Look at your console, which displays the actual time that the background task takes. The task should take about 1000 milliseconds; any extra is overhead. Compare the overhead of your InvokeLater with the overhead of your Timer. Can you get them both down around 1000-1100 milliseconds?

[an error occurred while processing this directive]