What does this do ?
publicclass TH1{
public TH1(){
}
publicstaticvoid main(String[] args)throws InterruptedException{
Thread td3 =new Thread("td3");
td3.start();
System.out.println(""+td3);
System.out.println("main exiting "+Thread.currentThread());
}
}
i got the output as
Thread[td3,5,main]
main exiting Thread[main,5,main]
what does td3 do here. Is it simply an object calling toString() method?
[965 byte] By [
rajitoora] at [2007-11-27 4:51:22]

# 1
Your program has two threads. Method main() is called in the "main" thread.
You are creating and starting a second thread, called "td3". This thread does nothing; it will terminate as soon as you start it.
Next, you are printing the td3 thread's string representation (implicitly calling td3.toString() ) to the output.
Finally, you are printing the current running thread's string representation to the output. Note that this is the "main" thread.
# 2
but i did nto extend thread class nor implemented runnable ho does it create new threads
# 3
When you create a new Thread instance, and call its start() method, your code is spawning a new thread. The thread executes the code in its run() method; however, you shouldn't call run() directly. Only by calling start() will you start a new thread.
Also, the default Thread class (which you've used) does not have anything inside the run() method...so it doesn't actually do anything. You should subclass Thread and put your code in the run() method.
For example:
class myThread extends Thread
{
public void run()
{
System.out.println( "This code executes in a distinct thread." );
}
}
# 4
> When you create a new Thread instance, and call its
> start() method, your code is spawning a new thread.
> The thread executes the code in its run() method;
> however, you shouldn't call run() directly. Only by
> calling start() will you start a new thread.
i agree withyou on this that i have to call start to call run start a new thhread containing my own custom code.
> Also, the default Thread class (which you've used)
> does not have anything inside the run() method...so
> it doesn't actually do anything. You should subclass
> Thread and put your code in the run() method.
>
> For example:
>
> > class myThread extends Thread
> {
>public void run()
> {
> System.out.println( "This code executes in a
> distinct thread." );
>}
>
>
Here is what is confusing me. as you yourself said i have to extend Thread class. but in my example i am not extending any Thread class so it should be a simple object of Thread class. isn't it.
# 5
>
> Here is what is confusing me. as you yourself said i
> have to extend Thread class.
You don't *have to*. In fact, extending Thread is rare and typically the wrong thing to do. The common thing to do is pass a Runnable to the Thread. The Runnable contains the code you want to execute in a separate Thread.
> but in my example i am
> not extending any Thread class so it should be a
> simple object of Thread class. isn't it.
Sure it is. The toString() method of the Thread class contains this:
return "Thread[" + getName() + "," + getPriority() + "," + group.getName() + "]";
which is exactly the output your code provided.
# 6
yes agreed that implementing runnable is a better is option.
so how can one be sure that an application is executing multiple threads. as in my code if i do this
public class TH1 extends Thread{
on line 1 output remains the same.
also i was doing some experiments with threading
public class TH1 extends Thread{
/** Creates a new instance of TH1 */
public TH1() {
}
/*public void run(){
System.out.println(Thread.currentThread()+" running");
}*/
public static void main(String[] args)throws InterruptedException{
Object o =new Object();
TH2 ht1 = new TH2();
TH2 ht2 = new TH2();
Thread td1 = new Thread(ht1,"ht1");
Thread td2 = new Thread(ht1,"ht2");
Thread td3 = new Thread("td3");
td3.start();
td1.start();
td2.start();
td1.join();
td2.join();
//td3.start();
System.out.println(""+td3);
System.out.println("main exiting "+Thread.currentThread());
}
}
class TH2 implements Runnable{
public TH2(){
}
public void run(){
try{
if(this instanceof TH2)
Thread.currentThread().sleep(500);
}catch(InterruptedException e){
}
}
}
i got the output as
Thread[td3,5,]
main exiting Thread[main,5,main]
here the output in first line why does not mention third field [td3,5,] like it has main in 2nd line [main,5,main].
# 7
> so how can one be sure that an application is> executing multiple threads.If you call Thread.start() anywhere you are executing another thread.
ejpa at 2007-7-12 10:05:05 >

# 8
> If you call Thread.start() anywhere you are executing another thread. even without extending/implementing Thread/Runnable
# 9
I really can't get what's confusing you...
-First of all, I'll start by saying this: a thread is a piece of program that will execute in concurrency with others of it's kind. In other words, it will be running at the same time with the rest of the program. Now, you do realize that one thread HAS to be created, the main thread, the one that your main method runs in? No extending, no implementing, that thread has to, and will, exist. And without making and starting any more threads, it will be the only one.
-Second, you do not have to extend/implement anything thread-related in the class that is used to start other threads. You can start a thread from any class and from any method without implementing/extending anything.
-Third, ONLY when you're CREATING a thread you extend/implement, not when you're starting it. Only if you're trying to start a thread from another thread, the starting class has to extend/implement as it is a thread itself.
-Fourth, yes, a thread is just an object, everything is, but when you call start() on that object, it's run() method starts the execution in a new thread ie. concurrently. In other cases, with other objects, calling a method will not result in creating a new thread, it will just get excuted in the main thread and the next line of code will be executed afterwards.
So, you create a thread (an instance of Thread or it's subclass, an object as any other), and then you start it by calling start()...
Ok, can't go any more detailed than that... If you're still having problems, do some heavy reading first...
# 10
<snip>
>Thread td2 = new Thread(ht1,"ht2");
> Thread td3 = new Thread("td3");
<snip>
> i got the output as
>
> Thread[td3,5,]
> main exiting Thread[main,5,main]
>
> here the output in first line why does not mention
> third field [td3,5,] like it has main in 2nd line
> [main,5,main].
Read the documentation.
============
public Thread(String name)
Allocates a new Thread object. This constructor has the same effect as Thread(null, null, name).
=============