Q:

Unable to set name of a thread in java

I am learning Thread in Java and have a small program like this. I create one thread and use it to create another thread. However I can not change the name of the second thread. Can anyone explain why it is like that ? Also, is it correct that Thread.sleep(100) means the main thread will sleep for 100 msec. Thanks. 
class Thread1 extends Thread{
   public void run() {      
      for(int i=0;i<5;i++){
        System.out.println(getName()+" is running. Time is "+i);
      }
   }
}

public class Program{   
    public static void main(String[] args) throws InterruptedException {        
       Thread t1 = new Thread1();
       t1.setName("Thread 1");
       Thread t2 = new Thread(t1);
       t2.setName("Thread 2");
       t1.start();
       Thread.sleep(100);       
       t2.start();      
    }       
}

The results from this program are:
Thread 1 is running. Time is 0
Thread 1 is running. Time is 1
Thread 1 is running. Time is 0
Thread 1 is running. Time is 1

Edit: If I change getName() to Thread.currentThread().getName() then everything work as expected. What is the differences between them ?

A:

   Thread t1 = new Thread1();
   t1.setName("Thread 1");
   Thread t2 = new Thread(t1);   <--- See this.

You are passing the same previously created thread instance (t1) to the next thread instance(t2). That is why same name is coming.
This should have been like this:
   Thread t2 = new Thread1();
   t2.setName("Thread 2");

Output:
Thread 1 is running. Time is 0
Thread 1 is running. Time is 1
Thread 1 is running. Time is 2
Thread 1 is running. Time is 3
Thread 1 is running. Time is 4
Thread 2 is running. Time is 0
Thread 2 is running. Time is 1
Thread 2 is running. Time is 2
Thread 2 is running. Time is 3
Thread 2 is running. Time is 4

Also, is it correct that Thread.sleep(100) means the main thread will sleep for 100 msec.

Yes. That is correct. Thread.sleep(milliseconds) takes the parameter in milliseconds.

When you pass a Thread instance while creating a Thread object. This new thread will use the passed instance for execution (as a Runnable instance). That is why you are getting the Thread 1 and where as the running thread is different, newly created thread instance. So the Thread.currentThread().getName() will give you the name you have set.
public class Thread implements Runnable {
....
....

/* What will be run. */
private Runnable target;       // <-- Runnable is defined here for the thread.

public Thread(Runnable target) {  //<-- constructor you are calling.
    init(null, target, "Thread-" + nextThreadNum(), 0); //<-- internal init call.
}

private void init(ThreadGroup g, Runnable target, String name,
                  long stackSize) {
    init(g, target, name, stackSize, null);  //<-- internal init call.
}

private void init(ThreadGroup g, Runnable target, String name,
                  long stackSize, AccessControlContext acc) {
.....
.....
this.target = target;     // <-- Set the Runnable for the thread.
.....
}
....
....
}

