File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Runnable object use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Runnable object use" Watch "Runnable object use" New topic
Author

Runnable object use

Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Original question:
http://www.coderanch.com/t/231957/threads/java/following-code-valid
I created something similar:


The last question (when I was there last) in the top url is what the effect of creating two 'Threads' with one 'Runnable' would be. Seems to work fine. (with the code commented out as mentioned in next posts)
Why should you need two objects from 'Runnable' classes to create two 'Threads'? The thread objects created are new seperate objects anyway.
Its not as if they contend for the one 'Runnable' object instance that was created, if I understand it correctly.
If that's true the 'Runnable' object is actually useless except for the creation of the 'Thread' objects.

public Thread(Runnable target)
Allocates a new Thread object. This constructor has the same effect as Thread(null, target, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.
Parameters:
target - the object whose run method is called.
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)

[ September 19, 2002: Message edited by: Jim Crawford ]

<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Oops... doens't seem to create two threads anymore... quite sure it did just now. Will get back to you.
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Ok this is what made the difference:

And this would then be the output: (The funny thing being that both threads seem to be created at once, if you look at the output(both 'thread started' messages appear at the same time))

Runner1 thread started. (threadCount = 1)
Runner1: Attempt wait state...
Runner1 thread started. (threadCount = 2)
Runner1: Attempt wait state...
Runner1: Wait state exit.
Runner1: Thread exit.
Runner1 thread started. (threadCount = 2)
Runner1: Attempt wait state...
Runner1: Wait state exit.
Runner1: Thread exit.
Runner1 thread started. (threadCount = 2)
Runner1: Attempt wait state...
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Seems like the next thread is created on the next 'while' cycle in 'run()', if there is any.
This behaviour doesn't make sense. Maybe because its not used correctly, but I thought it was worth exploring.
So the one 'Runnable' object seems to be shared somehow... just can't think how yet.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I may be off the mark here, but I think the sort of
thing you are playing with is like what happens with servlets. You have many threads traversing one servlet instance and that's why you should not try to store session state in a servlet's instance variables.
Could be Something Completely Different of course...
Over.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Seems like the last thread created takes over the 'Runnable' object and puts the previous one in a wait state of some kind.


Runner1 thread started. (runnerCount = 1)
Runner1: Attempt wait state...
Runner1 thread started. (runnerCount = 2)
Runner1: Attempt wait state...
Runner1: Wait state exit.
Runner1: Thread exit.
Runner1 thread started. (runnerCount = 2)
Runner1: Attempt wait state...
Runner1: Wait state exit.
Runner1: Thread exit.
Runner1: Wait state exit.
Runner1: Thread exit.

[ September 19, 2002: Message edited by: Jim Crawford ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
BTW don't you want a notify() or notifyAll() in
there somewhere?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Jim Crawford:
Why should you need two objects from 'Runnable' classes to create two 'Threads'? The thread objects created are new seperate objects anyway.
Its not as if they contend for the one 'Runnable' object instance that was created, if I understand it correctly.

Actually in your original code, you passed the same Runnable object into your two Threads

in which case the Threads will "contend" on the common Runnable object.
Here's a sample code:

Each Strider object is running on its own, well, thread, while two Threads are accessing the single Runner object in a multi-threaded manner.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
If you created multiple Runnable objects and passed different instances to each Thread constructor, then each Thread will "have" its own exclusive Runnable object.

This code behaves like the first one.
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Thanks!
(wish I had more time to study... back at work today)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runnable object use