Yes. I can't see why that wouldn't work. The synchronized keyword is quite orthogonal to the interrupt method.
(Note that contrary to await and notify you're not required to own the objects monitor when calling interrupt . ).
Up vote 2 down vote favorite share g+ share fb share tw.
I'm synchronizing on the object of the thread like this: synchronized(threadObject){ try{ threadObject.interrupt(); }catch(Exception ex){ //catch error here. }finally{ threadObject.notifyAll(); } } Now, my questions are: It is possible to interrupt a thread inside a synchronized block whose object that was synchronized was the thread to be interrupted? Like in the sample code.
Can I still notify other threads holding the interrupted thread's object? Like in the sample code. Java multithreading link|improve this question asked Mar 1 '11 at 15:37Richeve Bebedor1599 42% accept rate.
Like in the sample code. Yes. I can't see why that wouldn't work.
The synchronized keyword is quite orthogonal to the interrupt method. (Note that contrary to await and notify, you're not required to own the objects monitor when calling interrupt. ) Can I still notify other threads holding the interrupted thread's object?
Like in the sample code. Yes, you can call notifyAll on any object as long as you own the objects monitor. Again, the wait/notify-mechanism is quite orthogonal to the interrupt method.
Your question seem to indicate that you've misunderstood the use of synchronized. The usual use-case is to synchronize on an object representing some resource which you like to avoid concurrent access to. The thread itself rarely represent such resource.
The object works as it normally does. The only stipulation is that other threads that synchronize on threadObject's monitor will block until you're complete with your thread. So yes, you can do both of those.
Yes: But you don't really need to have the lock before calling interrupt. Yes.
The answer to both questions is yes. However, there is something a bit strange about your example. I've never come across a case where you would use a Thread as a primitive lock.
And it what you are doing in the example doesn't seem to achieve anything. If threadObject is the same as Thread.currentThread(), then the call to interrupt() will just set this thread's interrupted flag ... which be noticed in that code fragment. If threadObject is some other Thread object then that thread will be interrupted.
But we can't see (here) the code that that thread will be executing, and we don't know if it will be waiting on threadObject. If not the interrupt() and notify() well got to different threads ... The bottom line is that you wouldn't normally use a Thread object as a lock, and you wouldn't normally send use an interrupt() as an ersatz notify(). (Maybe this example is not intended to represent a real use-case.
).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.