Question
Read the tutorial https://www.cl.cam.ac.uk/teaching/1617/ConcDisSys/con-systems-prac.txt and submit the exercises: 1. Creating and joining threads 1.a. Write a short program that prints Hello world from an additional
Read the tutorial
https://www.cl.cam.ac.uk/teaching/1617/ConcDisSys/con-systems-prac.txt
and submit the exercises:
1. Creating and joining threads
1.a. Write a short program that prints "Hello world" from an additional thread using the Java Thread API.
1.b. Now modify the program to print "Hello world" five times, once from each of five different threads. Ensure that the strings are not interleaved in the output.
1.c. Now modify the printed string to include the thread number; ensure that all threads have a unique thread number.
2. Simple synchronisation
2.a. Write a short program in which two threads both increment a shared integer repeatedly, without proper synchronisation, 1,000,000 times, printing the resulting value at the end of the program. Run the program on a multicore system and attempt to exercise the potential race in the program.
2.b. Now modify the program to use "synchronized" to ensure that increments on the shared variable are atomic.
3. Guarded blocks
3.a. Write a short program in which one thread increments an integer 1,000,000 times, and a second thread prints the integer -- without waiting for it to finish.
3.b. Now modify the program to use a condition variable to signal completion of the addition task by the first thread before the second thread prints the value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started