1

As far as I know,two threads need at least two objects and a cycle wait situation to face deadlock.But is it possible for two threads to face deadlock centering one object instance?

  • 1
    What do you mean by "with a single object instance"? Yes, a proper deadlock requires at least two lockable resources, so if you mean "synchronizing on a single object instance" then no, you can't have deadlock there. – biziclop Jan 31 '19 at 10:04

2 Answers2

0

Not a deadlock, but you can easily starve threads.

eg.

Object lock=new Object();

Runnable run=new Runnable(){
  public void run(){
          synchronized(lock){
            lock.wait();
         }
       System.out.println("Im out");
   }
}

Thread t1=new Thread(someRun);
Thread t2=new Thread(someRun);

t1.start();
t2.start();

Nothing will be printed and 2 threads will await forever;

You won't deadlock on single shared resource.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

You can't have a deadlock with just one object instance because the two threads will not be waiting for the same object. If the object is in use by one thread, this thread is not waiting for this object, so it is not possible to create a dealock.

Eduardo
  • 319
  • 2
  • 14