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