Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to allocate a thread to an object  RSS feed

 
SANJAY KUMAR
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am creating 10 thread and keeping it in a hashtable and now i want to get one by one thread and allocate to diiferant object to do differant work. As I producer and consumer objects. I want my one thread should work for producer object and other for consumer object. I don't know how to allocate an existing thread to a object.
Thanks in avdance. Please let me know if I am doing some thing silly.
I am new to thread programming
-Viku
 
Mehul Sanghvi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek,
I hope this is what u were looking for.
public interface Producer
{
public void produce();
}
class MyProducer implements Producer
{
public void produce()
{
//Code that is to be given to
// thread for execution
}
}
class MyThread extends Thread
{
Producer p
public MyThread(Producer p)
{
this.p = p;
this.start();
}
public void run()
{
p.produce();
}
}
Regards,
Mehul K. Sanghvi
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mehul Sanghvi:
When you do this kind of thing -- extend Thread and start it in the constructor -- YOU MUST MAKE YOUR CLASS FINAL. Because if anyone were to subclass your class, the Thread might start before the subclass constructor had fully executed, and operate upon an incompletely initialised object. This can give rise to really subtle, spurious bugs.
I know it's probably not very relevant in this case, but making such classes final is a good habit to get into.
- Peter
[ January 23, 2003: Message edited by: Peter den Haan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if such a class is final, I think it's a bad idea. The new class does not behave as a Thread is expected to behave - it violates the Thread API. Someone who reads the Thread API and sees that your class extends Thread would reasonably expect that they could run it like this (since MyThread "is a" Thread):

This will of course cause confusion when the start() method throws an IllegalStateException since the thread is already started. I realize it's tempting to streamline the behavior of the class to it can be created and started in one step - but changing the behavior of existing classes unnecessarily can easily cause more problems than it solves.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Even if such a class is final, I think it's a bad idea. The new class does not behave as a Thread is expected to behave - it violates the Thread API.
I would agree with you. Funny thing is, Sun did exactly this in their WorkerThread class which figured heavily in their Swing tutorial code. The idea was that you would subclass the WorkerThread adding your business code to it... and thus trigger the bug that I mentioned above.
It took them a fair while to figure out the cause of the bug reports they were getting
- Peter
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I know I've seen it elsewhere as well in books and such. Doesn't make it right of course.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!