• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread names??

 
Sagar Sharma
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
In the JDK documentation of the Thread class the following lines are mentioned:
"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 id generated for it."
My question is:
1) How can two threads have the same name? for e.g.

Thread t1=new Thread();
Thread t1=new Thread();
the above lines will not compile as t1 is repeated. If this is not the name they are talking about then what is it?
2) If I create a thread as follows
new Thread(runnable_class).start();
then what is the name given to it by the system. Is this name unique or can be the same???

Please clear my doubts.
Sagar Sharma

 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There is a difference between the reference of an Object and the name of a thread.
a reference to an object is a pointer like
Thread t=new Thread();
in the above line t is a reference to the thread and not its name.
The name of a thread can be set using the setName() method of the thread class or by using the overloaded constructor of the Thread class which takes a String as argument.
The following program demonstrates the setting of names of 100 threads. The thread names are not unique and more than one thread can have the same name.


coming to the second part of your question

The program above does not assign any name to the threads created. I get the following output. What is yours??
Thread Name is : Thread-0
Thread Name is : Thread-1
Thread Name is : Thread-2
Thread Name is : Thread-3
Thread Name is : Thread-4
Thread Name is : Thread-5
Thread Name is : Thread-6
Thread Name is : Thread-7
Thread Name is : Thread-8
Thread Name is : Thread-9
Thread Name is : Thread-10
Thread Name is : Thread-11
Thread Name is : Thread-12
Thread Name is : Thread-13
Thread Name is : Thread-14
Thread Name is : Thread-15
Thread Name is : Thread-16
Thread Name is : Thread-17
Thread Name is : Thread-18
Thread Name is : Thread-19
Thread Name is : Thread-20
Thread Name is : Thread-21
Thread Name is : Thread-22
Thread Name is : Thread-23
Thread Name is : Thread-24
Thread Name is : Thread-25
Thread Name is : Thread-26
Thread Name is : Thread-27
Thread Name is : Thread-28
Thread Name is : Thread-29
Thread Name is : Thread-30
Thread Name is : Thread-31
Thread Name is : Thread-32
Thread Name is : Thread-33
Thread Name is : Thread-34
Thread Name is : Thread-35
Thread Name is : Thread-36
Thread Name is : Thread-37
Thread Name is : Thread-38
Thread Name is : Thread-39
Thread Name is : Thread-40
Thread Name is : Thread-41
Thread Name is : Thread-42
Thread Name is : Thread-43
Thread Name is : Thread-44
Thread Name is : Thread-45
Thread Name is : Thread-46
Thread Name is : Thread-47
Thread Name is : Thread-48
Thread Name is : Thread-49
Thread Name is : Thread-50
Thread Name is : Thread-51
Thread Name is : Thread-52
Thread Name is : Thread-54
Thread Name is : Thread-55
Thread Name is : Thread-57
Thread Name is : Thread-59
Thread Name is : Thread-61
Thread Name is : Thread-63
Thread Name is : Thread-65
Thread Name is : Thread-67
Thread Name is : Thread-69
Thread Name is : Thread-71
Thread Name is : Thread-73
Thread Name is : Thread-75
Thread Name is : Thread-77
Thread Name is : Thread-79
Thread Name is : Thread-81
Thread Name is : Thread-53
Thread Name is : Thread-56
Thread Name is : Thread-58
Thread Name is : Thread-60
Thread Name is : Thread-62
Thread Name is : Thread-64
Thread Name is : Thread-66
Thread Name is : Thread-68
Thread Name is : Thread-70
Thread Name is : Thread-72
Thread Name is : Thread-74
Thread Name is : Thread-76
Thread Name is : Thread-78
Thread Name is : Thread-80
Thread Name is : Thread-82
Thread Name is : Thread-83
Thread Name is : Thread-84
Thread Name is : Thread-85
Thread Name is : Thread-86
Thread Name is : Thread-88
Thread Name is : Thread-90
Thread Name is : Thread-92
Thread Name is : Thread-94
Thread Name is : Thread-96
Thread Name is : Thread-98
Thread Name is : Thread-87
Thread Name is : Thread-91
Thread Name is : Thread-95
Thread Name is : Thread-99
Thread Name is : Thread-93
Thread Name is : Thread-89
Thread Name is : Thread-97

------------------
Regds.
Rahul P. Mahindrakar

[This message has been edited by Rahul Mahindrakar (edited October 24, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic