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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:43:05+00:00 2026-05-16T16:43:05+00:00

Is it technically possible for a thread in Java to deadlock itself? I was

  • 0

Is it technically possible for a thread in Java to deadlock itself?

I was asked this at an interview a while back and responded that it wasn’t possible but the interviewer told me that it is. Unfortunately I wasn’t able to get his method on how to achieve this deadlock.

This got me thinking and the only situation that I can think of is where you can have this happen is where you have an RMI server process which contained a method that calls itself. The line of code that calls the method is placed in a synchronized block.

Is that even possible or was the interviewer incorrect?

The source code I was thinking about was along these lines (where testDeadlock is running in an RMI server process)

public boolean testDeadlock () throws RemoteException {
    synchronized (this) {
        //Call testDeadlock via RMI loopback            
    }
}
  • 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-16T16:43:06+00:00Added an answer on May 16, 2026 at 4:43 pm

    The JVM only keeps track of the local thread that has the monitor, if the calling class makes an external call back in on itself the incoming call causes the original thread to deadlock itself.

    You should be able to run this code to illustrate the idea

    import java.rmi.*;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.*;
    
    public class DeadlockThreadExample {
    
        public static interface DeadlockClass extends Remote {
            public void execute() throws RemoteException;
        }
    
        public static class DeadlockClassImpl extends UnicastRemoteObject implements DeadlockClass {
            private Object lock = new Object();
    
            public DeadlockClassImpl() throws RemoteException {
                super();
            }
    
            public void execute() throws RemoteException {
                try {
                    System.out.println("execute()::start");
    
                    synchronized (lock) {
                        System.out.println("execute()::Entered Lock");
                        DeadlockClass deadlockClass = (DeadlockClass) Naming.lookup("rmi://localhost/DeadlockClass");
                        deadlockClass.execute();
                    }
                    System.out.println("execute()::Exited Lock");
                } catch (NotBoundException e) {
                    System.out.println(e.getMessage());
                } catch (java.net.MalformedURLException e) {
                    System.out.println(e.getMessage());
                }
                System.out.println("execute()::end");
            }
        }
    
        public static void main(String[] args) throws Exception {
            LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
            DeadlockClassImpl deadlockClassImpl = new DeadlockClassImpl();
            Naming.rebind("DeadlockClass", deadlockClassImpl);
            DeadlockClass deadlockClass = (DeadlockClass) Naming.lookup("rmi://localhost/DeadlockClass");
            deadlockClass.execute();
            System.exit(0);
        }
    }
    

    The output from the program looks like

    execute()::start
    execute()::Entered Lock
    execute()::start
    

    Additionally the thread also dump shows the following

    "main" prio=6 tid=0x00037fb8 nid=0xb80 runnable [0x0007f000..0x0007fc3c]
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
        - locked <0x02fdc568> (a java.io.BufferedInputStream)
        at java.io.DataInputStream.readByte(DataInputStream.java:241)
    
    
    "RMI TCP Connection(4)-172.17.23.165" daemon prio=6 tid=0x0ad83d30 nid=0x1590 waiting for monitor entry [0x0b3cf000..0x0b3cfce8]
        at DeadlockThreadExample$DeadlockClassImpl.execute(DeadlockThreadExample.java:24)
        - waiting to lock <0x0300a848> (a java.lang.Object)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    
    
    "RMI TCP Connection(2)-172.17.23.165" daemon prio=6 tid=0x0ad74008 nid=0x15f0 runnable [0x0b24f000..0x0b24fbe8] 
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
        - locked <0x02ffb6d8> (a java.io.BufferedInputStream)
        at java.io.DataInputStream.readByte(DataInputStream.java:241)
    

    which indicates that the thread has indeed managed to lock itself

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

Sidebar

Related Questions

Although I have a feeling that this isn't technically possible, it's worth asking anyways.
I know that technically all three ways below are valid, but is there any
I don't think this is technically a macro but I don't know what else
this is just a quick probe to see if this is technically possible. I'm
You may find this weird, actually very weird, but is the following possible? just
If I have a class that implements java.lang.Runnable , there is technically nothing stopping
I'm technically savvy but don't have extensive experience with servers/daemons (I'm a Windows guy,
I'm aware technically you can't do this as inline always takes president. Inline element
A post on the FlowPlayer.org forum says: It is technically possible to stream YouTube
Looking to see if something is technically possible before embarking on its development. The

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.