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

  • SEARCH
  • Home
  • 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 7129293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:13:39+00:00 2026-05-28T11:13:39+00:00

This isn’t homework for me , it’s a task given to students from some

  • 0

This isn’t homework for me, it’s a task given to students from some university. I’m interested in the solution out of personal interest.

The task is to create a class (Calc) which holds an integer. The two methods add and mul should add to or multiply this integer.

Two threads are set-up simultaneously. One thread should call c.add(3) ten times, the other one should call c.mul(3) ten times (on the same Calc-object of course).

The Calc class should make sure that the operations are done alternatingly ( add, mul, add, mul, add, mul, ..).

I haven’t worked with concurrency related problems a lot – even less with Java. I’ve come up with the following implementation for Calc:

class Calc{

    private int sum = 0;
    //Is volatile actually needed? Or is bool atomic by default? Or it's read operation, at least.
    private volatile bool b = true;

    public void add(int i){
        while(!b){}
        synchronized(this){
                sum += i;
            b = true;   
        }
    }

    public void mul(int i){
        while(b){}
        synchronized(this){
            sum *= i;
            b = false;  
        }
    }

}

I’d like to know if I’m on the right track here. And there’s surely a more elegant way to the while(b) part.
I’d like to hear your guys’ thoughts.

PS: The methods’ signature mustn’t be changed. Apart from that I’m not restricted.

  • 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-05-28T11:13:39+00:00Added an answer on May 28, 2026 at 11:13 am

    volatile is needed otherwise the optimizer might optimize the loop to if(b)while(true){}

    but you can do this with wait and notify

    public void add(int i){
    
        synchronized(this){
            while(!b){try{wait();}catch(InterruptedException e){}}//swallowing is not recommended log or reset the flag
                sum += i;
            b = true;   
            notify();
        }
    }
    
    public void mul(int i){
        synchronized(this){
            while(b){try{wait();}catch(InterruptedException e){}}
            sum *= i;
            b = false;  
            notify();
        }
    }
    

    however in this case (b checked inside the sync block) volatile is not needed

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

Sidebar

Related Questions

This isn't actually homework, I'm just looking through some questions in a discrete maths
This isn't my code; I am trying to figure out what exactly this does.
this isn't asp.net specific, some of these apps are WinForms, also will be adding
This isn't actually a homework question per se, just a question that keeps nagging
This isn't solved, but I found out why: MySQL View containing UNION does not
This isn't a question of what stress testing tools are out there. I'm afraid
(This isn't a homework question, as it's above and beyond the class just for
This isn't much of a problem, really, it's entirely out of curiosity. For a
This isn't a question on whether it's safe to throw an exception from a
This isn't my code, I'm just troubleshooting it. Some code that has been working

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.