Reputation: 3373
When writing a Thread in Java (for example for handling different clients in Java):
1) What should be the reasons for doing it by extending Thread class, or by implementing Runnable interface?
2) How is it possible to give the newly created thread a name for accessing it in the program?
3) How to avoid situation that some threads have the same name?
Upvotes: 0
Views: 161
Reputation: 386
Some words about 1):
Implementing Runnable makes your class more flexible.
A class that implements Runnable is not a thread and just a class. For a Runnable to become a Thread, You need to create an instance of Thread and passing itself in as the target.
By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance.
In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
About 2)
The name of the Thread below is [myThread]
Thread myThread =new Thread(new Runnable(){
public void run(){
//code
}});
myThread.start(); //start it
About 3)
About names... If you dont care to access the Thread by name like the example above you can just a Thread and into it add code that has a behavor that you want.For example you can have a flag and making it false the Thread just finish it's work and that's it Thread finished.
new Thread(new Runnable(){
public void run(){
//example code
while(flag==true){
System.out.println("Yeah i am true ");
//Thread.sleep(200); //Makes The Thread sleep
}
}}).start(); //create and start by default
Here
public class Server{
public Server(){ //constructor
Thread client = ClientMethod("Alex");
System.out.println("i am Client "+client.getName());
}
//Make a Thread for a specific Client
public Thread ClientMethod(String clientName){
Thread client = new Thread(new Runnable(){
public void run(){
//do someWork
//Then finish
}//end of run method
});
client.setName(clientName); //set the NameOfThread to clientName
client.start(); //start it
return client; //return the Thread of this specific Client(as Object)
}
//Main Method
public static void main(String[] args){
new Server();
}
}//End of Class [Server]
Upvotes: 2
Reputation: 3373
2) the overloaded c'tor:
Thread (null, target, gname)
starts a runnable and assign it the name gname
unless specificly naming the thread the Automatic name will be: "Thread-"+n, where n is an integer.
Upvotes: 0
Reputation: 884
the documentation page here describes it best
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started.
The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.
Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
To address the naming: the API clearly shows that the constructor for thread:
Thread(String name)
has an argument that enables you to name the thread.
as for the difference between the two: Thread implements runnable. Classes that are inteded to run code while they are active can choose to implement either runnable or extend thread. Extending thread gives you the added features that come with Thread while runnable does not.
Upvotes: 1