---
title: "Why does it seem to take a long time for this synchronized block to get a lock?"  
description: "Why does it seem to take a long time for this synchronized block to get a lock?"  
author: "Anonymous User"  
published: 2015-11-27  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/33649/why-does-it-seem-to-take-a-long-time-for-this-synchronized-block-to-get-a-lock  
category: "android"  
tags: ["android", "thread"]  
reading_time: 4 minutes  

---

# Why does it seem to take a long time for this synchronized block to get a lock?

I am new to multi-[threading](https://www.mindstick.com/blog/187/threading-in-c-sharp) in [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming), and I have a [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) some might find trivial.\
I have to [debug](https://www.mindstick.com/forum/156/how-to-debug-application-on-the-server) a [third](https://yourviews.mindstick.com/story/4957/the-significance-of-the-third-eye-in-hinduism-more-than-just-a-symbol) [party](https://yourviews.mindstick.com/view/87403/indian-congress-party-secret-relation-with-china) piece of code and I need some [basic](https://www.mindstick.com/forum/161830/what-are-the-security-risks-of-using-basic-authentication) information, to know where to look for the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) because the code is very large.\
When the following code runs:\

```
public void method(){   long startTime = System.currentTimeMillis();   synchronized (obj)   {      log( "time:" + System.currentTimeMillis() - startTime + " ms" );      ...   }}
```

I get:\

```
11:13:12 - time: 3816 ms...11:14:14 - time: 0 ms
```

Why is taking so long (3816 ms) to get the lock for the object? Where should I look? For example, I would imagine a possible answer would be to look for code which acquires the lock for "obj" i.e. [block](https://www.mindstick.com/forum/157599/explain-the-process-control-block-pcb-in-the-operating-system) such as:\

```
synchronized (obj) { ... }
```

Or is it possible that any modification at the object "obj" without "[synchronized](https://www.mindstick.com/interview/2499/what-is-the-purpose-of-synchronized-block)" can also lock the object?

## Replies

### Reply by Aryan Kumar

There are a few reasons why it might seem to take a long time for a synchronized block to get a lock.

One possibility is that the object that the synchronized block is locking is being used by another thread. This can happen if the object is being accessed by a method that is also synchronized. In this case, the thread that is trying to acquire the lock will have to wait until the other thread is finished using the object.

Another possibility is that the object that the synchronized block is locking is in a critical section. A critical section is a part of a program where only one thread can be running at a time. This is usually done to prevent race conditions, which are errors that can happen when two or more threads are trying to access the same data at the same time. In this case, the thread that is trying to acquire the lock will have to wait until the other thread is finished with the critical section.

Finally, it is also possible that the object that the synchronized block is locking is in a deadlock. A deadlock is a situation where two or more threads are waiting for each other to release a lock. In this case, the threads will be stuck in a loop and will never be able to acquire the locks that they need.

If you are experiencing long wait times for synchronized blocks, you can try the following:

- Identify the object that the synchronized block is locking and see if it is being used by another thread. If it is, you can try to make the code that is accessing the object non-synchronized.
- Identify the critical sections in your code and make sure that only one thread can be running in each critical section at a time. You can do this by using synchronized blocks or other synchronization mechanisms.
- Check for deadlocks in your code. You can use a deadlock detection tool to help you identify deadlocks.

If you have tried all of these things and you are still experiencing long wait times for synchronized blocks, you may need to consult with a Java expert for help.

### Reply by Anonymous User

If it takes a thread that long to obtain a lock, it's because someone else is currently holding it.\
You should look for two things:\
Code blocks which synchronize on the same object or on other references to it (known as synchronized statements) :\

```
synchronized (obj) { ... }
```

Synchronized methods within the object itself.\
Say obj is of type MyObject, then you should look for methods such as these:

```
public class MyObject{    public synchronized void myMethod() {     ...    }}
```

Because they are essentially the same as\

```
public void myMethod() {    synchronized (this) {     ...    }}
```

So if a thread is executing obj.myMethod(), threads that want to enter a synchronized (obj) will have to wait since they lock on the same object. This, by the way, is the reason why I strongly recommend never using the synchronized method syntax, and always locking over a private (or protected) class member.\
If another thread is currently executing code within such a block, the current thread will be locked out until the other thread finishes.


---

Original Source: https://www.mindstick.com/forum/33649/why-does-it-seem-to-take-a-long-time-for-this-synchronized-block-to-get-a-lock

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
