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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:15:32+00:00 2026-06-04T05:15:32+00:00

I am trying to set the difference between synchronized and unsynchronized methods.. I have

  • 0

I am trying to set the difference between synchronized and unsynchronized methods.. I have tried following code

 class Counter {
    private int counter = 10;
    public int getCounter() {
        return counter;
    }

    public synchronized void doIncrementAndDecrement() {
        counter++;
        keepBusy(500);
        counter--;
    }

    public void keepBusy(int howLong) { // (D)
        long curr = System.currentTimeMillis();
        while (System.currentTimeMillis() < curr + howLong)
            ;
    }
}

class MyCounterThread extends Thread {

    Counter c;

    public MyCounterThread(Counter c, String name) {
        // TODO Auto-generated constructor stub
        super(name);
        this.c = c;
        start();
    }

    @Override
    public void run() {
        for (;;) {
            c.doIncrementAndDecrement();
            sleepForSometime();
            System.out.println(c.getCounter());
        }
    }

    public void sleepForSometime() { // (D)
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class UnSynchronizedExapmle {

    public static void main(String[] args) throws InterruptedException {
        Counter c = new Counter();
        MyCounterThread t1 = new MyCounterThread(c, "A");
        MyCounterThread t2 = new MyCounterThread(c, "B");
        MyCounterThread t3 = new MyCounterThread(c, "C");
    }
}

So above i have doIncrementAndDecrement() synchronized method..

So i expected the value of counter should be 10 every time. But this wont happen i have the output is like

10
10
11
10
10
10
10
11
10
10
11
10
11
11
10
10
11
10
11
10
10
10
11
10
10
11
10

So please help me why this happens.. Or any blog/article for explaining difference between synchronized and asynchronized methods

  • 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-04T05:15:34+00:00Added an answer on June 4, 2026 at 5:15 am

    Your getCounter() method is not synchronized. So even though one thread might be locking the method, another thread can still access and print your counter variable

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

Sidebar

Related Questions

I am trying to find the difference between the following two sets: A =
I have been trying to find a conclusive article to show the difference between
When trying to set DoMatchParen I get the following error: E492: Not an editor
I was trying to understand the difference between early and late binding, and in
I'm trying to understand the difference between regular Memcache and Doctrine's MemcacheCache. In my
I am trying to test the difference between no,even and odd parity - always
I'm trying to point out the difference between serial and parallel program with OpenMP.
I was trying to test the performance difference between inserting into a table directly
I have been trying to find info on the web about the differences between
Possible Duplicate: how to calculate difference between two dates using java I'm trying something

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.