In this seminar, you will learn how to work with SwingWorker class to achieve asynchronicity and thread safety.

Task 1: Explore the SwingWorker documentation

In the next seminar you will be working with the SwingWorker class.

  • Go through the Documentation.
  • For the following methods, use the documentation to decide whether the method is executed inside the GUI thread or in the background thread.
    • execute
    • doInBackground
    • done
    • get
    • cancel
    • isCancelled
    • getProgress
    • setProgress
  • Furthermore, decide:
    • which methods you should (or could) implement
    • which method you may call
    • which methods are called by the framework

Task 2: Prepare the repository

Every team needs just a single repository for this seminar.

Pick up a teammate who forks the repository and sets it up for the benefit of the whole team.

Task 2.1: Fork the repository and make it private

  • Fork the Mandelbrot repository into your personal namespace.
  • Make it private, so that it is visible only to explicitly assigned individuals.

    Go to Settings > General and expand Visibility, project features, permissions. Then change the Project visibility and scroll down (to be able) to click on Save changes.

Task 2.2: Make the repository accessible for designated people

  • Add all your team members under the Maintainer role.

    Go to Project information > Members and click on Invite Members. Fill in the FI login names of your teammates and choose Maintainer in Select a role. Finally, click on Invite.

  • Add both your seminar tutors under the Reporter role.

    You can find the FI login names of your seminar group tutors in Organization.

Task 2.3: Clone the repository and create a branch for your work

Each pair clones the repository into a single computer as they are working together as one person during the seminar.

Pair 1 creates a new branch seminar-11-pair1:

git switch -c seminar-11-pair1

Pair 2 creates a new branch seminar-11-pair2:

git switch -c seminar-11-pair2

Task 3: The frozen UI

Play with the application and try to find out, what is wrong with the application. Is it working as intended? Do you see a problem?

Try increasing the resolution to 4000. What about now? What happens if we set the resolution to 7500?

Why is this happening? Try to identify why the code doesn’t work properly.

Task 4: Melting the frozen goods

Task 4.1: Get down to brass tacks

Examine the code and identify the longest operation. Unload the workload to separate thread (hint: we looked at the specific SwingWorker documentation page for a reason).

Text fields should be editable while the image is rendered so that the user can prepare the parameters for next render. Progress bar should be continually updated during the computations. The cancel button should work and stop the computations.

Task 4.2: Unfreeze the UI

Use SwingWorker in the MandelbrotGui class to generate the image in the background.

Task 4.3: Commit and push your implementation

Once you are finished, please commit all of your changes and push them to the remote repository.

Task 5: Improve the performance

Look at the MandelbrotGenerator class. Can you spot what could be improved?

Each pixel is generated sequentially even though the pixels are independent to each other. We can easily improve the performance if the computation is run in parallel. For that we will use Streams.

Task 5.1: Use Stream::parallel

Replace the for-loop by the appropriate Stream. When you call parallel() at the beginning of the chain of operations, the last operation should be a reduction. For example, if a reduction of the parallel execution into a List is wanted, the following approach might be used:

List<Color> pixels = ...
        ...
        .collect(ArrayList::new, List::add, List::addAll);

Also note that you need to transfer the pixels into the image after the reduction.

Task 5.2: Commit and push your implementation

Once you are finished, please commit all of your changes and push them to the remote repository.