This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes monitors / owners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "monitors / owners" Watch "monitors / owners" New topic
Author

monitors / owners

Dale Harvey
Greenhorn

Joined: Nov 22, 2004
Posts: 5
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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

Joined: Nov 22, 2004
Posts: 5
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

Joined: Nov 22, 2004
Posts: 5
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
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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.
 
 
subject: monitors / owners