Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:07:46+00:00 2026-06-18T16:07:46+00:00

I have a homework assignment called The H2O problem where I’m supposed to implement

  • 0

I have a homework assignment called “The H2O problem” where I’m supposed to implement a class called H2OBarrier which has 3 methods.

  • HReady, a method called when a hydrogen atom (thread) is ready
  • OReady, a method called when an oxygen atom (thread) is ready
  • makeWater, a method called when 2 hydrogen atoms and one oxygen atom are ready

I’m supposed to do this using Java Reentrant locks and Conditions.

This is my code so far, and I’m wondering if I’m properly using lock and unlock.

public class H2OBarrier {

int hCount;
int oCount;

Lock lock = new ReentrantLock();
Condition hWait = lock.newCondition();
Condition oWait = lock.newCondition();

public void HReady() {
    lock.lock();
    hCount++;

    try {
        hWait.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

public void OReady(){
    lock.lock();
    oCount++;

    try {
        while(hCount<2 && oCount<1){
            oWait.await();
        }   
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        makeWater();
    }


}

public void makeWater(){
    hCount--;
    hWait.signal();
    lock.unlock();

    hCount--;
    hWait.signal();
    lock.unlock();

    oCount--;
    oWait.signal();
    lock.unlock();

}

} 

Should I be calling unlock() anywhere other than in my makeWater method? The whole flow of the program seems to make pretty logical sense to me, I’d just like to make sure what I’m doing looks correct overall.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:07:47+00:00Added an answer on June 18, 2026 at 4:07 pm

    Your code is producing a deadlock. Imagine 5 o atoms go through first, 5 go into o queue produced by await(). Now it doesn’t matter if 2 h atoms go through, all the h atoms will automatically wait because of your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an app to manage homework, and have a class called Assignment.
I have the following assignment for homework. Requirements Design a class called TokenGiver with
Have a homework assignment in which I'm supposed to create a vector of pointers
Hi I have a homework assignment where I need to implement an intersection of
A recent homework assignment I have received asks us to take expressions which could
Me and my friend having a problem while doing a homework assignment. We have
For a homework assignment I was given a Card class that has enumerated types
I have done a homework assignment, here is the problem statement: Your program should
I have a homework assignment that I am supposed to write a http server
For a homework assignment I wrote some scala code in which I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.