版本:JDK1.8
thread的两种使用方式:
//继承Thread类
NewThread thread = newNewThread();//调用默认的构造方法,父类也会调用默认的
thread.start();class NewThread extendsThread{
@Overridepublic voidrun(){
System.out.println("newThread running==============>");
}
}//使用Runnable对象
Thread thread = new Thread(newRunnableDemo());thread.start();
class RunnableDemo implementsRunnable{ @Overridepublic voidrun(){ System.out.println("new Runnable running======>"); } }
两种方式对应不同的Thread的构造方法:给target赋值
/*** Allocates a new {@codeThread} object. This constructor has the same
* effect as {@linkplain#Thread(ThreadGroup,Runnable,String) Thread}
* {@code(null, null, gname)}, where {@codegname} is a newly generated
* name. Automatically generated names are of the form
* {@code"Thread-"+}n, where n is an integer.*/
publicThread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}publicThread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}/*** Initializes a Thread with the current AccessControlContext. *@see#init(ThreadGroup,Runnable,String,long,AccessControlContext,boolean)*/
private void init(ThreadGroup g, Runnable target, String name, long stackSize) {
init(g, target, name, stackSize, null, true);
}
Thread的start()方法:注释上说明此方法执行时JVM会调用此线程的run方法
/*** 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.
*
*@exceptionIllegalThreadStateException if the thread was already
* started.
*@see#run()
*@see#stop()*/
public synchronized voidstart() {/*** This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".*/
if (threadStatus != 0)throw new IllegalThreadStateException();//一个thread重复调用start的时候会报错
/*Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented.*/group.add(this);boolean started = false;try{
start0();
started= true;
}finally{try{if (!started) {
group.threadStartFailed(this);
}
}catch(Throwable ignore) {/*do nothing. If start0 threw a Throwable then
it will be passed up the call stack*/}
}
}private native void start0();
查看run方法的定义:如果target!=null,调用target.run(),也就是传入的Runnable实例的run方法;如果是用的Thread的子类,则会调用override之后的run方法。
/*** If this thread was constructed using a separate
* Runnable
run object, then that
* Runnable
object's run
method is called;
* otherwise, this method does nothing and returns.
*
* Subclasses of Thread
should override this method.
*
*@see#start()
*@see#stop()
*@see#Thread(ThreadGroup, Runnable, String)*/@Overridepublic voidrun() {if (target != null) {
target.run();
}
}
总结:
start方法是生成新线程的方法,start后jvm会调用run方法。如果只是调用run方法,不会生成新的线程。