From 8c63bec955fd1f56d1a34d08c95e86cf296ef75b Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Wed, 7 May 2025 20:53:39 +0200 Subject: [PATCH] add readme --- u04-1/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 u04-1/README.md diff --git a/u04-1/README.md b/u04-1/README.md new file mode 100644 index 0000000..2c54536 --- /dev/null +++ b/u04-1/README.md @@ -0,0 +1,32 @@ +Implement a class `Exchanger` that is usable to exchange an object +of type `T` between exactly two threads: + +```java +class Exchanger { + // your task + T exchange(T s) { + // your task + } +} +``` + +Every thread invokes method `exchange()` with the object it wants to pass to the other thread and +receives as the result of that invocation the object that the other thread has passed. Make sure that +your code works even when both threads happen to pass the same object. + +Initially, your Exchanger should be usable for a single exchange only. Then, add functionality to make +it usable repeatedly by the same two threads. In a third step, extend your class to make it usable by +any number of threads: Each time, the "next two" threads perform an exchange among each other; +note that in this scenario it may happen that a thread uses an exchanger where the previous exchange +has not yet finished. + +In the source code folder on the server you'll find a class `ExchangerTester` that tests your +`Exchanger` (only for a single exchange; you can extend it yourself so as to test a series of exchanges). +The output of that program should look as follows: + +``` + Main thread received World in exchange for Hello + 2nd thread received Hello in exchange for World + 2nd thread received 2337 in exchange for 2337 + Main thread received 2337 in exchange for 2337 +``` \ No newline at end of file