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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:37:33+00:00 2026-06-13T13:37:33+00:00

Disclaimer: This code is copied from synchronized blocks for static and non-static methods I

  • 0

Disclaimer: This code is copied from synchronized blocks for static and non-static methods

I made some modification to it. I want to know how to make threads call both synchronized static and non-static methods. I can make it work by wrapping the non-static method in a synchronized block. Is there any other way?

public class StaticNonStaticSynch  
{ 
 public static void main(String[] args)  
 { 
      final StaticNonStaticTest staticNonStaticTest = new StaticNonStaticTest(); 
  Runnable runnable1 = new Runnable()  
  { 
   @Override 
   public void run()  
   { 
staticNonStaticTest.nonStaticMethod(); 
   } 
  }; 

  Runnable runnable2 = new Runnable()  
  { 
   @Override 
   public void run()  
   { 
    StaticNonStaticTest.staticMethod(); 
   } 
  }; 

  Thread thread1 = new Thread(runnable1, "First Thread"); 
  Thread thread2 = new Thread(runnable2, "Second Thread"); 

  thread1.start(); 
  thread2.start(); 
 } 
} 

class StaticNonStaticTest 
{ 

 void nonStaticMethod() 
 { 
 //synchronized (StaticNonStaticTest.class){ 
   for(int i=0;i<50;i++) 
   { 
    System.out.println("Non - Static method called by " + Thread.currentThread().getName() +" : = "+i); 
   } 
// } 
 } 
 static synchronized void staticMethod() 
 {  
   for(int i=0;i<50;i++) 
   { 
    System.out.println("Static method called by " + Thread.currentThread().getName() +" : = "+i); 
   } 
 } 
} 
  • 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-13T13:37:35+00:00Added an answer on June 13, 2026 at 1:37 pm

    Remember that this:

    public class MyClass {
    
        public synchronized void doSomething() {
            // Do something
        }
    
        public synchronized static void doSomethingStatic() {
            // Do something static
        }
    }
    

    Essentially compiles to this:

    public class MyClass {
    
        public void doSomething() {
            synchronized(this) {
                // Do something
            }
        }
    
        public static void doSomethingStatic() {
            synchronized(MyClass.class) {
                // Do something static
            }
        }
    }
    

    Notice that they don’t synchronize on the same thing. To fix this, create an object for both of them to lock on (known as a mutually exclusive object, or a “mutex”):

    public class MyClass {
    
        private static final Object MUTEX = new Object();
    
        public void doSomething() {
            synchronized(MUTEX) {
                // Do something
            }
        }
    
        public static void doSomethingStatic() {
            synchronized(MUTEX) {
                // Do something static
            }
        }
    }
    

    That should make it so that only one of these two methods are running at the same time across multiple threads.

    A couple tips:

    • Always use synchronized(variable) on a variable that’s final.
    • The MUTEX doesn’t have to be strictly a mutex, it could be an actual object. See the example below.
    • Remember how the synchronized modifier on methods is effectively implemented. It’s just like a synchronized block on this or MyClass.class.

    Besides having an object that’s strictly a mutex, you can use any field that’s final. For example, to synchronize on a Map during iteration:

    public class MyClass {
    
        private static final Map<String, String> map = new HashMap<String, String>(); // Java 6
        private static final Map<String, String> map = new HashMap<>(); // Java 7
    
        public static void put(String k, String v) {
            synchronized(map) {
                map.put(k, v);
            }
        }
    
        public static void printAll() {
            synchronized(map) {
                for (Entry<String, String> entry : map.entrySet()) {
                    System.out.println(entry.getKey() + ":" + entry.getValue());
                }
            }
        }
    }
    

    This code guarantees that you’ll never get a ConcurrentModificationException

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

Sidebar

Related Questions

Disclaimer : I kept this because some things may be useful to others, however,
Disclaimer: I apologize that this question is so long. I have added code as
Disclaimer: i have searched genericly (Google) and here for some information on this topic,
I will preface this question with the disclaimer that I know there are many
Disclaimer: I know this is a bad way to do things. It is the
Disclaimer: This is homework. I am attempting it and do not expect or want
Disclaimer: I know this type of question has been asked here before, I just
Disclaimer: The author of this post is a C++ beginner. In my Erlang code,
Disclaimer: this question is driven by my personal curiosity more than an actual need
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying

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.