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 7582493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:26:52+00:00 2026-05-30T18:26:52+00:00

I start learning some java concurrency concept ans put in use. But one of

  • 0

I start learning some java concurrency concept ans put in use.
But one of this piece of code is beyond my understanding.

public class Count { 
    private int count = 0; 
    public synchronized void setCount(int count) { 
        this.count = count; 
    } 
    public synchronized int getCount() { 
        return count; 
    } 
} 
class CountRunner extends Thread { 
    Count count; 
    public CountRunner(Count count) { 
        this.count = count; 
    } 
    public void run() { 
        for (int i = 1; i <= 1000; i++) { 
            count.setCount(count.getCount() + 1); 
        } 
    } 
} 
class TestCount { 
    public static void main(String[] args) throws Exception { 
        Count count = new Count(); 
        CountRunner runnerA = new CountRunner(count); 
        CountRunner runnerB = new CountRunner(count); 
        runnerA.start(); 
        runnerB.start();         
        runnerA.join(); //join statement here 
        runnerB.join(); 
        System.out.println("count.getCount = " + count.getCount()); 
    } 
} 
Question:
1. The result is a little less than 2000 for many times, why ?
2. if delete 2 join() statement, why count.getCount = 451,even less ?
3. i think there will be no effect of deleting the join() statements,
because i already have Synchronized method to lock one object to one thread each time ? 
So, What's the point of using Synchronized and join() ?
  • 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-30T18:26:53+00:00Added an answer on May 30, 2026 at 6:26 pm

    It is pretty simple. You call the setCount method by calling the getCount + 1. Before entering the method, the runtime evaluates the getCount (synchronized), but you don’t hold the lock when leaving getCount and entering setCount and other threads can enter an call getCount. So every now and then two (or more depending on how many threads you create) threads will have the same value in the getCount. Imagine thread A enters and receives the value 1 in getCount. The runtime yields it’s execution to tread B which calls getCount and receives the same value 1. Thread B sets the value to 1 and makes another 50 runs, so your count would be 50 at that stage. The runtime yields the execution to thread A which calls setCount with 1 (remember it didn’t manage to call setCount and yielded it’s exec). Now A sets the value to 1 (which is wrong).

    Change you run implementation like this:

    public void run() { 
        for (int i = 1; i <= 1000; i++) {
          synchronized(count){ 
            count.setCount(count.getCount() + 1); 
          }
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which one should I install when I want to start learning Java? I'm going
Would some one suggest me the best method to start learning the fundas of
I came across this during learning Java in the beginning, but I am learning
I'm just learning how to do things, and want to start using some sort
I plan to start learning a Java web framework (I love the Java API)
Sorry for asking this question, but I searched all Java-related questions, but I got
I've spent the last year learning and using Java in university but we didn't
I'm learning Java programming and right now I'm exploring the use of objects in
I have a Java book I'm learning from and in one of the examples,
I'm a student, want to star some small Java projects, for fun and learning.

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.