0

In java a thread can be stopped in two ways either by user (by calling sleep or wait) or by a synchronized block. The thread stops working or is blocked (waiting for a lock to be released by some other thread) when it tries to enter the synchronized block(if some other thread has acquired the same lock).

So I wanted to know if there is any difference in the states of the threads (by state I mean thread variables, context etc) blocked by synchronized block and the user methods like sleep in java.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • What are you trying to do? If there is a meaningful difference depends on that. – Thilo Nov 20 '12 at 04:39
  • http://stackoverflow.com/questions/1036754/difference-between-wait-and-sleep – Robert Peters Nov 20 '12 at 04:45
  • @Thilo: Actually I was reading this article http://www.ibm.com/developerworks/java/tutorials/j-threads/section5.html and thought that a sleeping thread can be re-invoked by an interrupt or by completing the sleep time. So was just curious about if there is any difference in these approaches or are just same internally. – me_digvijay Nov 20 '12 at 04:47
  • So the question is if a thread that blocks on a monitor (synchronized keyword) can be interrupted? – Thilo Nov 20 '12 at 04:49
  • @Thilo: Actually the question that you suggested also came in my mind, but I think it can restrict some of the information. I mean if I know the state of the thread I can use that information instead of just interrupting it. – me_digvijay Nov 20 '12 at 04:52

1 Answers1

0

- When a thread is doing IO operations or it executing a sychronized block, then it canNot be interrupted.

- When a thread tries to access a synchronized block of an object whose key is already being taken by some other thread, then this thread trying to access the synchronized block will enter the block state.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75