• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread --Extend vs implements

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have simple doubt about the thread class. I will explian my question with the follwoing porgrams.

class Job implements Runnable{
public void run(){
doJob
}
public void doJob(){
System.out.println(Thread.currentThread().getName()+" is doing the job now);
}
}

class Worker{
Job job1=new Job();
Job job2=new Job();
Thread t1=new Thread(job1,"Worker1");Thread t2=new Thread(job1,"Worker2");
Thread t3= new Thread(job2,Worker3");Thread t4=new Thread(job2,"Worker4");
t1.start(); t2.start();t3.start(); t4.start();
}

In the above program i have created two job objects and 2 workers for each job and if i make the doJob method as synchronized no two worker from the same job can invoke the doJob method.
Since i have implemted the interface here i can create as many jobs as i want and assigned as many worker to same or different jobs.

Is it possible to do the same by extending the thread class?
Also thread class already implemented the Runnable interface,so as soon as i create new thread object new job will also be get created right?
if i understood correctly, it is not possible to make more than one worker do the same job if we extend the thread class right?
For simplicity i used the term workers for Thread and job for the one thread will be doing ( Run method).
Please explain
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:Hi

I have simple doubt about the thread class. I will explian my question with the follwoing porgrams.



In the above program i have created two job objects and 2 workers for each job and if i make the doJob method as synchronized no two worker from the same job can invoke the doJob method.
Since i have implemted the interface here i can create as many jobs as i want and assigned as many worker to same or different jobs.

Is it possible to do the same by extending the thread class?


Yes, but don't. See below.

Also thread class already implemented the Runnable interface,so as soon as i create new thread object new job will also be get created right?
if i understood correctly, it is not possible to make more than one worker do the same job if we extend the thread class right?
For simplicity i used the term workers for Thread and job for the one thread will be doing ( Run method).
Please explain


First, let's not re-invent the language - we won't call a run() method a 'job' or a Thread a 'worker' (doing so only complicates things). So let's re-phrase your question using the correct terms:

Also Thread class already implemented the Runnable interface,so as soon as i create new thread object new run() method will also be created right?
if i understood correctly, it is not possible to make more than one Thread use the same run() method if we extend the Thread class right?


1) Yes, Thread does implement the Runnable interface, so they do have a run() method. When you subclass a Thread you typically override the run() method with the task you want to perform, rather than Thread's default implementation.

2) Technically it is possible to have more than one Thread use a Thread's run() method. You can treat the Thread as if it were a runnable. Example:


However, this is poor (very poor) practice, you should not do this, even if you can. If you want a Runnable, use the Runnable interface. Only extend Thread if you need to modify a Thread's behavior (rarely happens). If you want to perform a task, then use Runnable (most common scenario).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!