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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:39:39+00:00 2026-05-23T10:39:39+00:00

I have class with 2 synchronized methods: class Service { public synchronized void calc1();

  • 0

I have class with 2 synchronized methods:

class Service {

 public synchronized void calc1();

 public synchronized void calc2();

}

Both takes considerable time to execute. The question is would execution of these methods blocks each other. I.e. can both methods be executed in parallel in different threads?

  • 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-23T10:39:39+00:00Added an answer on May 23, 2026 at 10:39 am

    No they can’t be executed in parallel on the same service – both methods share the same monitor (i.e. this), and so if thread A is executing calc1, thread B won’t be able to obtain the monitor and so won’t be able to run calc2. (Note that thread B could call either method on a different instance of Service though, as it will be trying to acquire a different, unheld monitor, since the this in question would be different.)

    The simplest solution (assuming you want them to run independently) would be to do something like the following using explicit monitors:

    class Service {
       private final Object calc1Lock = new Object();
       private final Object calc2Lock = new Object();
    
       public void calc1() {
           synchronized(calc1Lock) {
               // ... method body
           }
       }
    
       public void calc2() {
           synchronized(calc2Lock) {
               // ... method body
           }
       }
    
    }
    

    The “locks” in question don’t need to have any special abilities other than being Objects, and thus having a specific monitor. If you have more complex requirements that might involve trying to lock and falling back immediately, or querying who holds a lock, you can use the actual Lock objects, but for the basic case these simple Object locks are fine.

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

Sidebar

Related Questions

Case 1 I have 2 synchronized methods as shown below: class A { public
I have 2 synchronized methods in a class say method1() and method2(). A thread
If I have 2 synchronized methods in the same class, but each accessing different
I have the following code: @Name(myService) @Scope(ScopeType.APPLICATION) @Stateless public class MyService { private Service
Say I have a class with synchronized methods. It's actually a class that uses
I have the following class for a Router's table with synchronised methods: public class
I'm putting together a custom SynchronizedCollection<T> class so that I can have a synchronized
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with a member function that takes a default argument. struct Class
I have class A: public B { ...} vector<A*> v; I want to do

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.