Thread priority problem
Dear fellow programmers Somebody knows why continues giving an arbitraire execution even setting higher priority to one thread respect to another? From PRO exercise found at https://www.sololearn.com/learning/2185/ Initial code: https://code.sololearn.com/cVKM7v9q4FR7/?ref=app My buggy code: https://code.sololearn.com/cOgK28o6QPIH/?ref=app
1/21/2021 11:27:03 AMDavid Ordás 🇪🇸
5 AnswersNew Answer
I answered this for Minho 🇰🇷 yesterday. See explanation in this code. https://code.sololearn.com/c9ckWvwXiXcZ/?ref=app
Elizabeth Kelly Minho 🇰🇷 thanks! Its something that I supouse. I know that managing resources are blocking operations and depending on the time taken in resolve them, makes appearing arbitraire results. Let me think if I'm right... Maybe how to solve correctly (producer-consumer pattern) .... - JDK1+ shared mutex object calling method wait() in one thread and notify() in the other. - JDK5+ using concurrent/atomic pkg: Semaphore / Countdown Barrier, AtomicInteger... The extrange thing is that an official SoloLearn practice hints you to resolve it using thread priorities 🤔🤔. So...Might be a buggy exercise 😂😂 ???
David Ordás if a producer / consumer solution is required, the recommendation is to use ‘synchronized’ methods for managing access to the shared resource. That is the current standard with JDK11 anyways.
Same problem https://www.sololearn.com/Discuss/2606071/?ref=app Threads depends on OS support and the scheduler in charge to orchetation takes the lastest word independiently of setted priority. Once look at this : https://www.sololearn.com/Discuss/2211828/?ref=app
Elizabeth Kelly Minho 🇰🇷 SOLVED. Shared monitor to the rescue 😎 https://code.sololearn.com/c4pDN8XFIaxw/?ref=app