Seminar 10 - Threads
In this seminar, you will learn about thread safety in java, principles of synchronization, and bad practices to avoid.
Task 1: The Deadlock Empire
- Enter The DeadLock Empire and solve as many tasks as possible for the given time period.
- You can see that the code is written in C# but it will serve well for the purposes of this seminar. However, you may find a functionality that is C# specific.
- Examples:
Monitor.Enter(mutex)
andMonitor.Exit(mutex)
is similar to synchronized block or lock() and unlock().Monitor.TryEnter(mutex)
is similar to tryLock().- Others can be found at the Concurrent documentation.
Task 2: Thread Quiz
- Look at the code, discuss it with your partner and answer these questions
- Is this code thread-safe?
- Is the scope of the synchronization appropriate?
- If the answer to any question above is no, explain why.
- Discuss your answers with tutors.
Question A:
public final class QuestionA {
private static int id = 0;
public synchronized int getId() {
return ++id;
}
}
Question B:
public final class QuestionB {
private static final Object LOCK = new Object();
private int id = 0;
public int getId() {
synchronized (LOCK) {
return ++id;
}
}
}
Question C:
public final class QuestionC {
private String text;
public void addLine(String line) {
synchronized (text) {
text += line;
}
}
}
Question D:
public final class QuestionD {
private final Set<Integer> set = Collections.synchronizedSet(new HashSet<Integer>());
public boolean addValue(int value) {
return set.add(value);
}
public int count() {
int value = 0;
for (int i : set) {
value += i;
}
return value;
}
}
Task 3: 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 3.1: Fork the repository and make it private
- Fork the Counter 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 3.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 Seminars page.
Task 3.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 seminar10-pair1
:
git switch -c seminar10-pair1
Pair 2 creates a new branch seminar10-pair2
:
git switch -c seminar10-pair2
Task 4: Multi-thread counter
Task 4.1: Implement a 3-thread Counter
-
Fill in the implementation of
Main
class to spawn 3 threads. These threads cooperate on generating unique Fibonacci numbers from 0 to 40 and printing them on standard output (not necessarily in the correct order) in the following format:Thread-0 0: 0
Thread-1 1: 1
Thread-2 2: 1
Thread-0 3: 2
…
Thread-2 39: 63245986
Thread-0 40: 102334155
Hint: take a look at the Thread documentation.
Task 4.2: Commit and push your implementation
Once you are finished, please commit your implementation and push it to the remote repository.