CodeGym /Java Course /Java Multithreading /Practice using yield and surrendering time slices

Practice using yield and surrendering time slices

Java Multithreading
Level 5 , Lesson 11
Available
Practice using yield and surrendering time slices - 1

"Hi, Amigo! Keep practicing! Here are a couple of tasks for you."

7
Task
Java Multithreading, level 5, lesson 11
Locked
Ensuring important operations aren't interrupted
Take a look at the moveMoney() method. If RANDOM.nextInt(5000) is greater than THRESHOLD_VALUE, then ensure the time slice is surrendered (yield the right of way to the current thread). Add this code to the only acceptable place.
7
Task
Java Multithreading, level 5, lesson 11
Locked
Finagle's first law: if an experiment succeeds, then something went wrong...
Ensure the time slice is surrendered (yield the right of way to the current thread) for consistent console output.
Comments (6)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
catalin1989 Level 37, Romania
25 January 2024
I really don't see the logic of the first exercise, with Thread.yeld. If Random is greater than 500, you pause the tread and give the time of the CPU to another thread, it executes and then it comes back and executes this thread?! I think I unterstand the method, Thread.yeld, but I the example, for me, doesn't make sense. The second exercise is straight forward. If you make some examples and play with the yeld method you see the results.
Alex Buyanov Level 47, Netherlands
28 October 2022
I do not get it. Why is "correct place" specifically after first statement? Why it cannot be before first statement? That would seem more logical to me
4 April 2021
Didn't get the tasks purpose, but managed to complete.
Jurij Thmsn Level 29, Flensburg, Germany
8 November 2021
I didn't even get the requirements. What do they mean by "Ensure the time slice is surrendered"?
Dan Level 26, Clarksville, United States Expert
22 February 2022
Question: "What do they mean by "Ensure the time slice is surrendered"?" Answer: They are saying to make the thread yield
matemate123 Level 50, Kraków, Poland
6 May 2023
The term "time slice is surrendered" typically refers to a situation where a process in a multitasking operating system voluntarily relinquishes its share of the CPU's processing time, allowing other processes to use it instead. In a multitasking operating system, the CPU is divided into time slices, or small units of time, which are allocated to each running process in turn. When a process has completed its task or is waiting for input/output operations to complete, it may voluntarily surrender its time slice so that another process can use the CPU. This helps to ensure that all processes get a fair share of the CPU's processing time and that the system remains responsive. The act of surrendering a time slice may be initiated by the process itself, or it may be triggered by the operating system, which may interrupt the process and schedule another process to run in its place.