This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Issue regarding Executing Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Issue regarding Executing Thread" Watch "Issue regarding Executing Thread" New topic
Author

Issue regarding Executing Thread

AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
hi,
After executing this code it gives me output :
Ex-B Exception in thread "main" java.lang.IllegalMonitorStateException: current
thread not owner
at java.lang.Object.notifyAll(Native Method)
at tA.main(tA.java:35)
can anybody please explain me why?

please correct my understanding explained below:

1- At commented // 1 main thread creates a new thread and makes it wait at the thread pool
2- At commented // 2
main thread sleeps for 100 milisecond .so, the new thread crated gets its turn and sleeps for 1000 milisec by executing doDelay(1000) inside run method
3-At commented // 3 (i.e after 100 milisecond ) the main thread calls interrupt(); , thus interrupting the sleeping thread .
so Ex-tA should come in output.

thanks in advance




AshutoshP
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Line 21:
this.wait(); must be within a synchronized block (or synchronized method).

The thread must first obtain a monitor on the object before the wait() method is called.
Otherwise exception is thrown "current thread not owner".
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Welcome to threads. Working with them it's been my experience to see
IllegalMonitorState (IMS) just as often as NullPointerException (NPE).

Jim ... ...


BEE MBA PMP SCJP-6
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
@kordal
i got the point why i have got that run time exception.
but, i have mentioned my doubt
why Ex-tA was not in output ?(As i followed up the below mentioned understanding )
Please rectify the same.

1- At commented // 1 main thread creates a new thread and makes it wait at the thread pool
2- At commented // 2
main thread sleeps for 100 milisecond .so, the new thread crated gets its turn and sleeps for 1000 milisec by executing doDelay(1000) inside run method
3-At commented // 3 (i.e after 100 milisecond ) the main thread calls interrupt(); , thus interrupting the sleeping thread .
so Ex-tA should come in output.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

No. You create tA which is a Thread, but which you are using as a Runnable. You need to interrupt t, which is the actual Thread running the commands, not a which is being used like a bloated Runnable with a bunch of unnecessary Thread-specific behavior and methods.

Really, you should also change the declaration of tA from this:

to this:


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Ireneusz Kordal wrote:Line 21:
this.wait(); must be within a synchronized block (or synchronized method).

Not just this.wait - also t.notifyAll.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
@steve thanks
1)
you are correct because i was interrupting a but expecting t to be interrupted(which is the helping thread acts on target)
You need to interrupt t, which is the actual Thread running the commands

2)
public class tA extends Thread

But,
This above statement implicitly means tA is-a Runnable (since ta is-a thread and thread is-a runnable )
so when i am passing to thread constructor Thread(Runnable) which takes an argument which is-a runnable
whether i extends thread (implicitly implements Runnable) or implements Runnable makes no difference.
i hope i am right.please correct me if i am wrong.

thanks All for your help. i will look forward the same in future.
Adwin Lorance
Greenhorn

Joined: May 31, 2010
Posts: 9
That is correct , Thread implements Runnable .. But it is always a good practice to implement the interface Runnable than extending Thread , as it gives you flexibility to inherit from other classes.. Anyways , it wont make a difference if you extend from Thread or implement Runnable ..

Adwin
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
@Adwin
You are correct.
Actually i am preparing for scjp .
i got this as one practice question.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Adwin Lorance wrote:That is correct , Thread implements Runnable .. But it is always a good practice to implement the interface Runnable than extending Thread , as it gives you flexibility to inherit from other classes.. Anyways , it wont make a difference if you extend from Thread or implement Runnable ..

Adwin


Sorry for the late response....

That is correct. Also, in this case by extending Thread you get a lot of Thread specific behavior which may break your code - for example you can get notify()'s called on the Thread object which could break your synchronization scheme (because Thread does some notify() calls internally). You also get a 'heavier' object which confuses the API. For example if the original poster had just implemented Runnable then there would be no a.interrupt() method to call and it would have been more obvious on which object he had to call interrupt(). Note that you gain absolutely nothing from extending Thread either, you still have to implement the run() method when extending Thread.

So the Runnable interface in-particular, and an interface in-general provides a smaller footprint that helps prevent unwanted and unexpected behavior and simpler, less confusing APIs than extending from a class you don't need just because it implements the interface you want.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue regarding Executing Thread
 
Similar Threads
Thread interrupt confusion
Thread Problem
Question from examlab on threads
THREAD
Q 28 of examlab practice test 3