Object-Oriented Programming 2022/23

Exercise 6



Continue working on your project. If possible, present the preliminary version already at this exercise.



  1. Study different ways of creating threads using the examples from the lecture. You can put the classes into one project, but they are actually independent and each one is being executed separately.
    1. What happens when you rename the run() method in the Thread1 class?
    2. What happens when you rename the run() method in the Thread2 class?
    3. Notice the use of a method reference and lambda expression.
    4. Finish the Nit5 class by adding the corresponding anonymous class. For this, consider the Thread2 class.
  2. Experiment with thread synchronization using the Incrementer-Printer example from the lecture.
    1. What happens when you remove the synchronized keyword from one or both places at which it occurs?
    2. Notice that synchronization actually means locking a given object for a critical section (code block). Identify the critical sections and the object in the Incrementer-Printer example. Substitute whole method synchronization by a block code synchronization (supplied as comments). Does anything change?
    3. (Homework.) Redesign the Incrementer-Printer example so that it doesn't use any static elements. What object has to occur in the synchronized() expression if you apply synchronization at the code block level as in the previous task?
  3. Try the division example and do so without exceptions being handled, as well as with exceptions being handled. If you use Eclipse, program arguments can be provided with the Run Configurations... option.
  4. Experiment with the code of the game from the lecture, in which an uneven number of ogres and knights is being treated by introducing and handling the corresponding exception. This is so-called controlled exception: it must be handled. What will happen if you remove its handler (try-catch blok)? There's one more problem that remains: a user doesn't have to provide numbers at all. How would you handle this?
  5. Notice the NoSuchElementException handler in the nextClashButton handler in the ClashWindow class. This is an exception of the RunTimeException type, and such exceptions are uncontrolled, i.e., the compiler doesn't enforce their handling (you had an opportunity to see three other types of uncontrolled exceptions in the division example). Handling exceptions when using the Task<V> class, which is being used to execute a calculation outside the thread in which the GUI (JavaFX) is being executed, is a little bit more complicated because this class doesn't propagate exceptions directly, but it stores them. The nextClashButton listener in the ClashWindow demonstrates two variants of capturing a NullPointerException exception (another uncontrolled exception): by observing the task failure and by observing the object which is used to store exceptions.
  6. Notice the example with an exception handling at an overriding method. An overriding method can't introduce a new exception type. It has to hold to the exception types declared by the method it overrides. In our example, the client code (the C class) could rely on a more general declaration of the m() method in the A class and ignore an exception of the Exception type. What would happen if the B.m() method would declare that it draws MySubexception derived from MyException? Does the exception handler in the client code needs to be update in such a case?
  7. A method to count warriors (Clash.countWarriors()) has been added to the game with ogres and knights. The GUI includes the corresponding controls. Notice the use of the isInstance() method (instead of the instanceof operator) to find out the warrior type and for providing type control using the generics of the Class class. Which class implements the isInstance() method? Take a look at this class in the Java API documentation and see what else can be revealed.


Valentino Vranić
vranic at stuba.sk