Thread是Java中的类,它描述了线程,它本身并不是线程。线程和Thread是两个不同纬度的东西,不要混为一谈。(具体可以这样想:线程是运行的CPU,Thread对象只是内存中的数据)
常常研究各种开源框架与技术,不知道大家有没有看过我们常用的Thread类,虽然是一个很基础的类,但是有很多本质的东西值得我们去学习,从而能提高我们日常编码的心得。(类似还有Integer、String等等,你们看了吗,可能我们认为很简单,其实并非如此)
我们先简单介绍一下Thread类,然后再去分析它和线程的关系。
Java中的Thread类是线程的抽象表示,它定义了线程的基本行为和属性,并不是线程本身。
对于老生常谈的Thread状态转换什么的就不说了,下面主要聊一聊涉及本文的重要方法。
Thread的构造方法最终都会调用init方法执行,我们直接看init
/**
* Initializes a Thread.
*
* @param g the Thread group
* @param target the object whose run() method gets called
* @param name the name of the new Thread
* @param stackSize the desired stack size for the new thread, or
* zero to indicate that this parameter is to be ignored.
* @param acc the AccessControlContext to inherit, or
* AccessController.getContext() if null
* @param inheritThreadLocals if {@code true}, inherit initial values for
* inheritable thread-locals from the constructing thread
*/
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
g = security.getThreadGroup();
}
/* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
}
/* checkAccess regardless of whether or not threadgroup is
explicitly passed in. */
g.checkAccess();
/*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}
g.addUnstarted();
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext =
acc != null ? acc : AccessController.getContext();
this.target = target;
setPriority(priority);
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize;
/* Set thread ID */
tid = nextThreadID();
}
大致看一下这一段代码,其实也就是给当前Thread对象的一些属性进行了初始化,并没有做其他的。
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <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).
* <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.
*
* 使此线程开始执行;Java虚拟机调用该线程的run方法。
* 结果是两个线程同时运行:当前线程(从对启动方法的调用返回)和另一个线程(执行其运行方法)。
*
* @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 */
}
}
}
// 运行jvm的本地方法启动操作系统线程
private native void start0();
从run方法的解释我们可以看到运行过start方法后,结果是两个线程同时运行:当前线程(从对启动方法的调用返回)和另一个线程(执行其运行方法)。这里才真正启动了一个新的线程,这个新的线程可以通过“Thread对象进行操作”。
/**
* Returns a reference to the currently executing thread object.
*
* @return the currently executing thread.
*/
public static native Thread currentThread();
仍然是native方法,这里获取的当前线程,是执行run方法的线程,下面来看run方法。
/**
* 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() {
if (target != null) {
target.run();
}
}
这个方法就是线程会运行到的代码块,也是线程运行的入口,线程运行的代码内容都由他展开。
你会看到这个方法是没有地方调用的,但是start过后就会运行run方法,是因为JVM虚拟机底层的C++代码会去调用这里的run的。
【记住】:Thread本身只是一个类,Thread对象会去对应一个Java线程,它定义了线程的基本行为和属性,通过Thread对象,其他人可以去从操控线程。
线程=进程-共享资源
线程的优点:
线程的缺点:
线程和进程比较