这里我们先来看看一些概念:
1、任务:任务可以简单的理解为一段代码,一个类的一个方法或者多个方法去做一件和多件事情,这就是任务,这段代码,方法你怎么写,写在哪里都可以。
package test3;
public class MainTest {
public static void main(String[] args) {
System.out.println("输出任务");
}
}
任务就是输出“”输出任务“我把这个任务写在了main方法中,所以说任务时按照你的具体需求写的,怎么写,写在哪里都无所谓。而为了让java的多线程机制能识别并调用执行你编写的任务,所以规定了任务类要继承Thread类或者实现Runnable接口并且任务的具体实现要么写在run()方法中要么在run()方法中调用具体任务方法。
”
2、线程:线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点点在运行中必不可少的资源(线程的运行内存),但他可以与同属一个进程的其它线程共享进程所拥有的全部资源。
3、线程和任务的关系:java的线程是用来驱动任务执行的,也就是说你得吧一个任务附着在一个线程上,这样该线程才能驱动执行你定义的任务。
4、定义线程:显示的定义线程的过程就是将任务附着到线程的过程,线程Thread自身并不执行任何操作,它只是用来被java多线程机制调用,并驱动赋予他的任务!
定义任务,并将任务附着在线程上:
(1)、任务定义
package test3;
public class ImplRunnable implements Runnable{
@Override
public void run() {
System.out.println("按照java规范定义任务,即任务要吗在run方法中要吗在run方法中调用");
}
}
(2)、声明线程并将任务附着到线程上
package test3;
public class MainTest {
public static void main(String[] args) {
//任务对象
ImplRunnable runnable= new ImplRunnable();
//声明线程并将任务附着到线程上
Thread thread= new Thread(runnable);
}
}
(3)、当我们声明了线程并赋予任务后,线程自身是不会做任何事情的,java的线程机制也不会调用该线程并运行,还需要特殊的调用才能实现多线程的执行。Thread类的start()方法就是触发java线程机制,使得java多线程机制能都调用执行该线程,线程驱动任务,这样才能完成任务。
package test3;
public class MainTest {
public static void main(String[] args) {
//任务对象
ImplRunnable runnable= new ImplRunnable();
//声明线程并将任务附着到线程上
Thread thread= new Thread(runnable);
//启动这个自定义的线程
thread.start();
}
}
ok 这样后这个被赋予任务的线程才会被java多线程机制调用执行。其实这里的start()方法就是告诉java多线程机制我已准备好了,我可以随时运行,这样java多线程机制就知道了,这个线程已经准备好了,所以在合适的时候会被调用执行,否则java多线程机制也不知道你这个线程是否存在,是否准备好,所以也不可能执行!
总结:
1、任务和线程是不同的,注意在应用开发中我们写的比较多的就是任务。有的是直接写在了run方法而有的是在其他方法或类中编写的任务而在run方法中调用任务。
2、继承Runnable接口的目的是为了让java多线程机制能够调用执行我们的任务,也就是规范了任务的调用入口即run()方法。
3、线程的定义Thread thread= new Thread( new Runnable()); 显示的定义线程的过程就是将任务附着到线程的过程。
4、声明线程、定义线程后线程调度机制还是没法调用执行我们定义的这个线程,只有调用了Thread的start()方法后java多线程机制才能调用执行我们的任务。
5、那这个start()方法到底做了什么,他就是将这个线程添加到了线程数组,
额外补充:
当声明线程后要调用start()才能被java的多线程机制来调用执行任务也就是run()方法,那start()方法到底做了什么?
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
*导致该线程开始执行;java虚拟机调用该线程的run方法。
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
*结果是两个线程同时运行:当前线程(从线程返回到启动方法)和另一个线程(执行线程run方法)。
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* 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();
/* 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();
/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
//这里的target就是构建Thread对象时构造函数中的Runnable对象所以target.run()方法就是我们写任务的那个方法!
if (target != null) {
target.run();
}
}
从上面的代码中我们看到,start()方法中只要用了一个本地方法start0()这个方法。而没有调用执行我们任务的方法run()方法,因为jvm调用了该线程的run方法来执行我们的任务。
在从代码和翻译来看,调用start()方法导致该线程开始执行;java虚拟机调用该线程的run方法。结果是两个线程同时运行:当前线程(从线程返回到启动方法即start()方法)和另一个线程(执行线程run方法)
ok 到这里就明白了,调用start()方法启动了这个线程,来执行java的一个本地方法start0()和另一个线程来执行这个线程的run方法。