I don't understand why the order has to be this way? Please explain.
I don't understand why the order has to be this way? Please explain.
Under discussion
Comments (12)
- Popular
- New
- Old
You must be signed in to leave a comment
Justin Smith
20 January 2022, 21:16
I had a lot of confusion about this task as well. Don't I need to know why I'm adding a yield() in order to know where to put it? And why would you use a random variable with a low probability to determine if the yield happens or not? When I did this, I thought the aim was to allow other threads to run before any of the lines in the method run, so I put Thread.yield() first.
0
Yiheng Niu
15 April 2020, 11:04
This is what I got to pass the verification:
yield does not take or release locks, it simply pauses the current thread execution. So yielding in the synchronized block will not let the current thread to release lock and let the other methods to enter the synchronized block. wait/notify method should be used to release the lock.
So in my opposition (it might not be right), this task wants us to know that a Thread.yield() in the middle of those two operations won't impact the result we get. Because yield() won't release from, in and amount, which will be needed for the to.setBalance() operation. +6
Henrique
4 August 2020, 18:09
Thanks. I copied yours and passed. But this task still does not make sense to me.
0
klesk
24 October 2020, 17:26
I think the task can be accomplished a little shorter and easier. Just think about when one task takes too long, skip his turn it and let the other task run. The method can be accomplished with 4 lines of code in total. But the code above it not wrong, just a little bit to long, for what is needed.
0
Andrei
28 April 2021, 14:20
I did it with 2 lines of code and got accepted:
0
Andrei
28 April 2021, 14:21
Also, then I checked the mentor's solution and it was the same like I did.
0
Brandon Horvatic
10 March 2020, 17:48
https://www.geeksforgeeks.org/java-concurrency-yield-sleep-and-join-methods/
0
Johannes
19 May 2020, 10:09
Thanks Brandon, but since no timeout was specified in milliseconds: how long does it yield in this instance ? Will it go into an infinite loop ?
0
Andrei
28 April 2021, 14:25
Yield skips the current turn of the thread, it doesn't pause anything for an amount of time. In the lesson previous to these exercises, Ellie says the following:
"2) yield() – the current thread 'skips its turn'. The thread goes from the running state to the ready state, and the JVM proceeds to the next thread. The running and ready states are sub-states of the RUNNABLE state."
0
Brandon Horvatic
10 March 2020, 17:45
yield(): Suppose there are three threads t1, t2, and t3. Thread t1 gets the processor and starts its execution and thread t2 and t3 are in Ready/Runnable state. Completion time for thread t1 is 5 hour and completion time for t2 is 5 minutes. Since t1 will complete its execution after 5 hours, t2 has to wait for 5 hours to just finish 5 minutes job. In such scenarios where one thread is taking too much time to complete its execution, we need a way to prevent execution of a thread in between if something important is pending. yeild() helps us in doing so.
yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should run. Otherwise, the current thread will continue to run.
+1
Yiheng Niu
14 April 2020, 11:14
I don't get it...
Can you explain what should I do with my code, please?
+1
Justin Smith
20 January 2022, 20:52
"Since t1 will complete its execution after 5 hours, t2 has to wait for 5 hours to just finish 5 minutes job."
I don't think this is correct. The point of threads is to enable the JVM to run tasks concurrently. It should jump back and forth between processing for t1 and t2. t2 shouldn't have to wait for t1 to complete unless you have explicitly told it to (such as using .join(), for example).
+1