CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Baurzhan Konurbayev
Level 40
  • 10.04.2021
  • 302views
  • 0comments

the first test case is not passing

Question about the task Determining locking order
Java Multithreading,  Level 7,  Lesson 6
Under discussion

Implement the logic of the isLockOrderNormal method, which should determine:
whether the order of the synchronized blocks in the someMethodWithSynchronizedBlocks method matches the order of the arguments passed to it.
If o1 is synchronized first and then o2, the method should return true.
Otherwise, false.

Requirements:
  • The isLockOrderNormal method must return true if the someMethodWithSynchronizedBlocks method synchronizes first using the o1 object and then o2.
  • The isLockOrderNormal method must return false if the someMethodWithSynchronizedBlocks method synchronizes first using the o2 object and then o1.
  • The isLockOrderNormal method should NOT be private.
  • The Solution class must NOT be declared with the final modifier.
package com.codegym.task.task27.task2707; /* Determining locking order */ public class Solution { public void someMethodWithSynchronizedBlocks(Object obj1, Object obj2) { synchronized (obj2) { synchronized (obj1) { System.out.println(obj1 + " " + obj2); } } } public static boolean isLockOrderNormal(final Solution solution, final Object o1, final Object o2) throws Exception { Thread thread1 = new Thread(new Runnable() { @Override public void run() { synchronized (o1) { while (true) {} } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { solution.someMethodWithSynchronizedBlocks(o1, o2); while (true) {} } }); Thread thread3 = new Thread(new Runnable() { @Override public void run() { synchronized (o2) { while (true) {} } } }); thread1.setDaemon(true); thread1.start(); Thread.sleep(300); thread2.setDaemon(true); thread2.start(); Thread.sleep(300); thread3.setDaemon(true); thread3.start(); Thread.sleep(300); // if thread3 could enter o2, the order was correct return (thread3.getState() == Thread.State.RUNNABLE); } public static void main(String[] args) throws Exception { final Solution solution = new Solution(); final Object o1 = new Object(); final Object o2 = new Object(); System.out.println(isLockOrderNormal(solution, o1, o2)); } }
0
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.