java Thread class

Å

å…‰ ç½²

hi all, I have a Thread named r1 , why I can not write "
r1.start();r1.start();". when I write "r1.start(); r1.start();" there
was an error that "IllegalThreadStateException".

r1.run(){
public void run(){
System.out.println("name" + super.getName() + "---->" +
super.getI());
}
}
 
K

Knute Johnson

hi all, I have a Thread named r1 , why I can not write "
r1.start();r1.start();". when I write "r1.start(); r1.start();" there
was an error that "IllegalThreadStateException".

r1.run(){
public void run(){
System.out.println("name" + super.getName() + "---->" +
super.getI());
}
}

start

public void start()

Causes this thread to begin execution; the Java Virtual Machine
calls the run method of this thread.

The result is that two threads are running concurrently: the
current thread (which returns from the call to the start method) and the
other thread (which executes its run method).

It is never legal to start a thread more than once. In particular,
a thread may not be restarted once it has completed execution.

Throws:
IllegalThreadStateException - if the thread was already started.
See Also:
run(), stop()
 
A

Arne Vajhøj

In general, it is better to use a Runnable rather than extending Thread.
You cannot start a Thread instance more than once. You can create
several Thread instances using the same Runnable, and start each Thread
once.

Implementing Runnable is more flexible than extending Thread.

But instantiating and calling start of a FoobarThread
that extends Thread may be more intuitive.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top