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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:55:42+00:00 2026-06-01T07:55:42+00:00

I am new to mutli-threading in java and trying to understand the keyword synchronization

  • 0

I am new to mutli-threading in java and trying to understand the keyword synchronization
I have following code:

class LockObject {
    int threadsGoingThrough = 0;

    synchronized void locked() {
        threadsGoingThrough++;

        try {
            wait(1000);
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }

        if (threadsGoingThrough > 1) {
            System.out.print("  !! ouups !! "); 
        }
        ;
        threadsGoingThrough--;
    }
}

The Thread-class:

class LockTester extends Thread {
    LockObject myLock;
    String message;

    public void run() {
        while (true) {
            myLock.locked();
            System.out.print(message);
        }
    }
}

My main looks like this:

LockObject theLock = new LockObject();
LockTester threadA = new LockTester();
threadA.myLock = theLock;
threadA.message = "A";

LockTester threadB = new LockTester();
threadB.myLock = theLock;
threadB.message = "B";

threadA.start();
threadB.start();

If I run this code now in two threads than it gives the following ouput:

!! ouups !! A !! ouups !! B !! ouups !! A !! ouups !! B !! ouups !! A !! ouups !! B

The !! ouups !! should only occurs when two threads are in the synchronized method at the same time. How can this happen?

  • 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-01T07:55:43+00:00Added an answer on June 1, 2026 at 7:55 am

    You’re calling wait(), which will release the lock of the monitor it’s called on. It will then reacquire the lock when it wakes up (either due to timeout or the monitor being notified). If you were just trying to sleep for a second, use Thread.sleep(1000) which won’t release any locks.

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

Sidebar

Related Questions

I'm new to the Java threading and have only recently started to read up
I hava a Java mulithreading question. I have the following worker class: public class
I'm loosely following a tutorial on Java NIO to create my first multi-threading, networking
I'm experimenting Java Multi-Threading using synchronization on method comparing with Atomic variables (java.util.concurrent.atomic package).
I am trying to implement multi-threading in my Java Mandelbrot application: This is what
So I have a boosted queue class that helps multi-threading described here . In
I have following situation. I have a class for accounts, each account has a
I'm new to multi-threading and I have been doing a Proof-of-Concept, I've also 'discovered'
I have extended threading.Thread - my idea is to do something like this: class
I am new to Multi-threading in JAVA and am facing a problem. I am

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.