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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:18:28+00:00 2026-06-16T01:18:28+00:00

I am trying to understand the keyword synchronized from the following example Java Main

  • 0

I am trying to understand the keyword synchronized from the following example

Java Main Method –>

public int methodA(){

  Hello h = new Hello();
  h.callSomeSynchronizedMethod();    

  sysout("Main");
  return 0;    

}

In the Hello Class–>

public synchronized void callSomeSynchronizedMethod(){
   Hi h = new Hi();

   h.someMethod();

   sysout("Hello");

}

In the Hi class

public void someMethod(){

    sysout("Hi");
}

So what would be the list of outputs that i will get;

1.) Is it in the order of Hi, Hello and Main ?

2.) What i understand about the synchronized keyword is that it will only execute 1 method and then execute the other, without multi-threading. Is this correct ?

  • 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-16T01:18:29+00:00Added an answer on June 16, 2026 at 1:18 am

    To really understand what synchronized does you need to run the program twice, once synchronized and once not. Also your program should use multiple threads. So here is an example of such a test.

    public class Synchro {
    
        public static void main(String args[]){
            new Synchro();
        }
    
        public Synchro(){
            final Moo moo = new Moo();
            Thread t = new Thread(new Runnable(){
               public void run(){
                   moo.aMethod("Second");
               }
            });
            t.start();//calling the method in a thread
    
            moo.aMethod("First");//calling the same method from the same object in the main thread
         }
    
     class Moo{     
        public Moo(){            
        }
    
        public void aMethod(String name){
            //this loop just prints slowly so you can see the execution
            for(int i = 1; i <= 100; i++){
                System.out.println(String.format("%s : %d", name, i));
                try{
                    Thread.sleep(50);
                }catch(InterruptedException e){}
            }
        }
      }
    
    }
    

    Now, if you run the above code, noticing that the method is not synchronized, you will see the printout from the two executions of the method interleaved. That is you will see First 1 then Second 1 then First 2 etc.

    Now, add the synchronized keyword to the method making it:

      public synchronized void aMethod(String name){ ....
    

    and run the code again. This time, one execution of the method completes before the other begins.

    The synchronized keyword is only necessary when multiple threads are accessing the very same object.

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

Sidebar

Related Questions

I am new to mutli-threading in java and trying to understand the keyword synchronization
I am just trying to understand the extends keyword in Java Generics. List<? extends
I'm trying to understand exactly what the javascript new keyword means, why it is
I'm learning Java and trying to understand how private , protected and public modifiers
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
While trying to understand how does the google keyword tool is requesting data I
I'm trying to understand synchronization of multiple threads in Java more fully. I understand
I'm trying to understand the yield keyword better and I think I have a
I am trying to understand the difference of using the keyword this or rather

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.