• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Issue regarding Executing Thread

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to threads. Working with them it's been my experience to see
IllegalMonitorState (IMS) just as often as NullPointerException (NPE).

Jim ... ...
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Not just this.wait - also t.notifyAll.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Adwin
You are correct.
Actually i am preparing for scjp .
i got this as one practice question.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic