• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

monitors / owners

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey

I have 1 class (an applet) that runs in its own thread, I have it set up a threadgroup, with itself as the parent, that spawns 5 new threads, I want the threads to run, and immediately wait on invokation from the parent thread.

The problem is I cant seem to find any way to make the parent the owner of the waiting threads ( IllegalMonitorStateException seems to say the thread needs to be an owner of the object but that confuses me, how can an object wait? it doesnt run )

boolean p = Thread.currentThread().getThreadGroup().parentOf(cars);

where cars is the 5 threads in the group, and is called from the parent thread, is returning true, where as

boolean p = Thread.currentThread().holdsLock(engines[1]);

where engines[] is an array of the threads held in the group returns false

the javadoc says that a thread can take ownership of an object by

# By executing a synchronized instance method of that object.
# By executing the body of a synchronized statement that synchronizes on the object.
# For objects of type Class, by executing a synchronized static method of that class.

but I cant find any examples anywhere of a parent thread, doing this
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether a thread is in a threadgroup that is a parent of another threadgroup, and whether a thread owns a lock/monitor, are two different things. They are not related in any way.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I forgot to answer your question:

First, I don't it is possible to change the parent/child relationship of threagroups once they have been instantiated.

Second, to take ownership of a lock/monitor...



Of course, this probably doesn't help you since you have a mis-understanding of the concept.

Henry
 
Dale Harvey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its cool i wasnt looking for a full explanation, just a pointer to an article or a brief overview, as the javadoc wasnt doing much to help my misunderstanding.

here had a decent explanation, I still dont fully understand it, keep to think about it like processes in c but its completely different

anyway, thanks
 
Dale Harvey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and i wasnt changing the ownership, i was setting up a threadgroup, defining it as parent of that group, and adding 5 threads to that group

although its useless in as It doesnt help gain ownership, its still needed to notify all threads concurrently (i hope)
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dale Harvey:
although its useless in as It doesnt help gain ownership, its still needed to notify all threads concurrently (i hope)



You don't need to be in a separate threadgroup to be notified together. You just have to make sure that all the threads are waiting on the same object.



As already mentioned, threadgroups and lock/monitor have little to do with each other.

Hope this helps,
Henry
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dale Harvey:
its cool i wasnt looking for a full explanation, just a pointer to an article or a brief overview, as the javadoc wasnt doing much to help my misunderstanding.

here had a decent explanation, I still dont fully understand it, keep to think about it like processes in c but its completely different

anyway, thanks



A process and a thread are hardware level things. There is nothing special or different about c. It is likely that many JVMs are written in c or c++ so when you are running a Java program you are actually running a c/c++ program in one way of thinking.

A thread is basically a lighter process. A thread is owned by a process, and gets all its resources from its parent process.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic