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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:09:43+00:00 2026-06-05T11:09:43+00:00

i heard, sleep() will lock the current sync method/block But here, when i call

  • 0

i heard, sleep() will lock the current sync method/block
But here, when i call sleep() on thread 1, thread 2 is able to access the same block? Can anyone Explain?

Main.java

public class Main {     
    public static void main(String args[])
    {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        System.out.println("going to start t1");
        t1.start();
        System.out.println("going to start t2");
        t2.start();

    }

}

=====================================================================

Thread1.java

public class Thread1 extends Thread{

    public void run() { 
        Syncc s1 = new Syncc();
        s1.me("T1:");
    }   

}

=====================================================================

Thread2.java

public class Thread2 extends Thread{

    public void run() { 
        Syncc s2 = new Syncc();
        s2.me("T2:");
    }   
}

=====================================================================

Syncc.java

public class Syncc{

    public void me(String s){
        synchronized(this){
        for(int i=0; i<=5; i++)
        {
            System.out.println(s+" "+" "+i);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {              
                e.printStackTrace();
            }
        }
    }
}
}

==========================================

Output:

going to start t1
going to start t2
T2:  0
T1:  0
T2:  1
T1:  1
T1:  2
T2:  2
T1:  3
T2:  3
T1:  4
T2:  4
T2:  5
T1:  5

BUT according to sleep() method, it should not unlock the current synchronization block right? if so the out put should be..

going to start t1
going to start t2

T1:  0
T1:  1
T1:  2
T1:  3
T1:  4
T1:  5
T2:  0
T2:  1
T2:  2
T2:  3
T2:  4
T2:  5

i mean after thread 1 execution only thread 2 should start right?
Whats the issue?

  • 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-05T11:09:45+00:00Added an answer on June 5, 2026 at 11:09 am

    This is because you have two different instances of Syncc in play here. Each thread has its own copy of Syncc.

    Try doing the same with a single instance. You could also synchronize on the static context and try.

    To simulate, modify Thread1 and Thread2 to accept an instance of Syncc.

    public class Thread1 extends Thread {
        private Syncc syncc;
    
        public Thread1(Syncc syncc) {
            this.syncc = syncc;
        }
    
        public void run() { 
            this.syncc.me("T1:");
        }   
    }
    

    You can then start them as so:

    public static void main(String args[]) {
        Syncc syncc = new Syncc();
    
        Thread1 t1 = new Thread1(syncc);
        Thread2 t2 = new Thread2(syncc);
    
        System.out.println("going to start t1");
        t1.start();
        System.out.println("going to start t2");
        t2.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I heard that JNBridge will translate C# code to native Java code.( I haven't
I heard this term many times when I came across some protocol implementations, but
I need a better design for sleeping Task than Thread.Sleep, I'm using the Task
Sorry but I'm having a major brain fart here (guess that's what a few
I've been at this for hours -- I think I need sleep... but no
How can I put a hard disk drive in stand-by or sleep mode in
I heard from a relatively trustworty source that gettext doesn't require system locales for
I heard on Windows x64 architecture, in order to support to run both x86
I heard Kevin Skoglund (lynda.com) say that it is good practice to get in
I heard somewhere that declaring an Index on a date column is bad for

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.