aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What will this program do and why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What will this program do and why?" Watch "What will this program do and why?" New topic
Author

What will this program do and why?

Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
public class Class implements Runnable{
protected int i1;
protected int i2;
/** Creates new Class */
public Class (int val1, int val2)
{
i1=val1;
i2=val2;
}

public static void main(String args[])
{

Class test1=new Class(3,4);
Thread t=new Thread(test1);
t.start();
t.start();
t.start();


}
public void run()
{
System.out.println("The first value is " +(++i1));
System.out.println("The second value is " +(++i2));
}

}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Have you tried compiling it yourself? What happens when you do? What happens when you run it?
I bet you get an IllegalThreadStateException.


Rob
SCJP 1.4
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Yes it does, but I cannot understand why. Could you pleasee elaborate.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Look at the Thread class javadocs.
Basically, you can only call start() on a Thread object one time. Once the Thread is started, you can't call start() again. This implies you can't reuse a Thread object. Once it completes its task, you must instantiate a new THread object if you want to run it again.

In your program, if you really want 3 seperate threads running, you need to create 3 different Thread objects, and call start() on each of the Thread objects.
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Cool thanks....
kittu anu
Greenhorn

Joined: Jan 18, 2002
Posts: 28
Good explaination Rob!! thanks
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Hi Rob!
This statement is not true :
"This implies you can't reuse a Thread object. "
You can re-use a Thread object but you cannot re-run the thread, i.e invoke Thread.start() again.
You can invoke Thread.run() or other methods in the class derived from the thread.
Brian
[ February 11, 2002: Message edited by: Brian Lugo ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Brian,
what you are trying to say is that we can USE (not re-use) a Thread object once it has run. What Rob was saying is that a Thread object will only be able to run once and that's it. After its death, you can only invoke its methods (but not start() again). Pay attention to the terminology you use, it is very important to be 200% accurate when claiming things...
So I would modify your statement

You can re-use a Thread object but you cannot re-run the thread, i.e invoke Thread.start() again.

as follows:
You can use a Thread object but you cannot re-start the thread, i.e. invoke start() on the thread instance again. (start() is not a static method)

I know you know the concept, so no hard feelings. (I know I'm one hell of a picky guy )
[ February 11, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
Don't get me started about those stupid light bulbs.
 
subject: What will this program do and why?