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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:30:12+00:00 2026-05-27T14:30:12+00:00

I am new to Java. Below is a code as an example of threads

  • 0

I am new to Java. Below is a code as an example of threads and synchronization.

public class A implements Runnable{
    public synchronized void run(){

        /*
        some code here
         */

    }
}

public class B {
    public static void main(String[] args){
        A obj1 = new A();
        Thread t = new Thread(obj1);
        A obj2 = obj1;
        Thread t1 = new Thread(obj2);
        t.start();
        t1.start();
    }
}

Now will this two threads block each other for same lock or will they get two different locks?

Thank you!!

  • 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-27T14:30:13+00:00Added an answer on May 27, 2026 at 2:30 pm

    (First, please stick to the Java coding conventions. A class name should always start with a capital letter. No exceptions.)

    Only one of the threads will execute the run() method at a time.

    The A.run() method is an instance method, and it is declared as synchronized. These two facts mean that it will acquire a lock on this (i.e. the instance of A) before entering the method body, and release it on exiting. In short, run() locks this.

    So in your main program you are creating a single A instance and passing it as the target object for two threads. They both need to execute the run() method on the same object, and this cannot happen at the same time … by the reasoning of the previous paragraph.

    This does not necessarily mean that one thread will block the other. It is also possible that the first thread to be started will have completed its run() call before the second thread is ready to try the call. But we can say … definitively … that the two threads’ calls to run() will NOT overlap in time.

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

Sidebar

Related Questions

The below code in Java throws Null pointer exception. public class New{ int i;
Running the following (example) code import java.io.*; public class test { public static void
I am using some threads in java android, I have a class which implements
(the example code below is self-contained and runnable, you can try it, it won't
I can't seem to see the problem with the example code below. For some
I am getting an issue to run the below code.The code gives no error
Edit Downvoter, how is this a bad question? I have provided runnable example code
I'm new to Java but not to programming (I normally code in Ruby). One
Is there a tutorial on how to run Ant from Java? I got some
The code below plots some simple x-y data, but it has two problems that

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.