• 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

Runnable object use

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original question:
https://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 ]
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops... doens't seem to create two threads anymore... quite sure it did just now. Will get back to you.
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW don't you want a notify() or notifyAll() in
there somewhere?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
(wish I had more time to study... back at work today)
 
reply
    Bookmark Topic Watch Topic
  • New Topic