Institute of Computer Science
  1. Courses
  2. 2023/24 fall
  3. Concurrent Programming Languages (LTAT.06.022)
ET
Log in

Concurrent Programming Languages 2023/24 fall

  • Pealeht
  • Loengud
  • Labs
  • Viited
  • Homework

Executor service and semaphores/bounded buffers

In order to be prepared for COVID times, a shop wants to make it an maintain a regime where only 5 people can be in the shop at a time. You are asked to model this as a Java program.

In order to do, model the visitors of the shop as threads. You can create as many as you like, but something like 10 should be good for testing purposes. The visitors will try to enter the shop after a random amount of seconds from 0 to 5 and then leave after a time between 10 and 15 seconds. If there are already 5 visitors in the shop, a visitor waits till its his turn. Use an ExecutorService to run the threads. The shop should be modelled as a class.

Potential solutions for this are the use of a bounded buffer or a semaphore. Try one of the solutions. If you have time, feel free to attempt the other solution as well. Put some output into your program which shows what happens during the run of the program.

Threads doing real work

Most of this is from lab 3, the description is repeated for convenience.

We want to find the number in a range of positive numbers which has the maximum number of divisors. A divisor is a number which divides another number with remainder 0. For example, 9 has two divisors, namely 3 and 1, whereas 8 has three, namely 1,2, and 4 (if the 1 is included or not does not matter for the maximum). Using the int data type should be enough for this exercise.

As a first step, write a program which finds the divisors for a number.

Then extend this to find the maximum number of divisors for a range in a sequential fashion. Try this out for eg the range from 1 to 1000 and 1 to 100,000 and 1 to 10,000,000. Record how long it takes for 1 to 10,000,000.

Finally, implement a concurrent version using the fork/join framework. Make sure there is appropriate divide and merge steps in your code. How can you influence the number of tasks created?

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment