Thread多线程
线程是程序中的执行线程,Java虚拟机允许应用程序同时运行多个执行线程。
每个线程都有一个优先级。优先级高的线程优先于优先级低的线程执行,每个线程可能或不可能为守护进程。
当某个线程中运行的代码创建一个新的Thread对象时,新线程的优先级初始设置等于创建线程的优先级,
并且当且仅当创建线程是守护线程,它是守护线程。
当Java虚拟机启动时,通常只有一个非守护进程(它通常调用某个指定类名为main的方法)。
Java虚拟机将继续执行线程,知道出现以下任一情况:
- 已经调用了类Runtime.exit()的退出方法,并且安全管理器允许执行退出操作。
- 所有不是守护进程的线程都已死亡,要么从对run方法的调用返回,要么抛出在run方法之外传播的异常。
这两中方法可以创建新的执行线程。一种是将类声明为Thread的子类。这个子类应该覆盖Thread类的run方法。
然后可以分配和启动子类的实例。另一种是通过实现Runnable接口的类。然后该类实现run方法。然后可以在分配类的实例,在创建Thread时作为参数传递,
并启动。
每个线程都有一个用于标识的名称。多线程可以有相同的名称。如果在创建线程时没有指定名称,
则会为其生成一个新名称,除非另有说明,否则将null参数传递给该类中的构造函数或方法当值抛出NullPointerException
1、Thread的前世今生
Thread类实现了Runnable接口,并实现了run方法。该类中有native方法,其中
registerNatives() 在类初始化时在静态代码块调用。
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
private volatile String name;
private int priority;
private Thread threadQ;
private long eetop;
/* Whether or not to single_step this thread. */
private boolean single_step;
/* Whether or not the thread is a daemon thread. */
private boolean daemon = false;
/* JVM state */
private boolean stillborn = false;
/* What will be run. */
private Runnable target;
/* The group of this thread */
private ThreadGroup group;
/* The context ClassLoader for this thread */
private ClassLoader contextClassLoader;
/* The inherited AccessControlContext of this thread */
private AccessControlContext inheritedAccessControlContext;
/* For autonumbering anonymous threads. */
private static int threadInitNumber;
/* ThreadLocal values pertaining to this thread. This map is maintained
* by the ThreadLocal class. */
ThreadLocal.ThreadLocalMap threadLocals = null;
/*
* InheritableThreadLocal values pertaining to this thread. This map is
* maintained by the InheritableThreadLocal class.
*/
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
/*
* The requested stack size for this thread, or 0 if the creator did
* not specify a stack size. It is up to the VM to do whatever it
* likes with this number; some VMs will ignore it.
*/
private long stackSize;
/*
* JVM-private state that persists after native thread termination.
*/
private long nativeParkEventPointer;
/*
* Thread ID
*/
private long tid;
/* For generating thread ID */
private static long threadSeqNumber;
/* Java thread status for tools,
* initialized to indicate thread 'not yet started'
*/
private volatile int threadStatus = 0;
/**
* The argument supplied to the current call to
* java.util.concurrent.locks.LockSupport.park.
* Set by (private) java.util.concurrent.locks.LockSupport.setBlocker
* Accessed using java.util.concurrent.locks.LockSupport.getBlocker
*/
volatile Object parkBlocker;
/* The object in which this thread is blocked in an interruptible I/O
* operation, if any. The blocker's interrupt method should be invoked
* after setting this thread's interrupt status.
*/
private volatile Interruptible blocker;
private final Object blockerLock = new Object();
/**
* The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10;
}
Thread内部维护了Runnable实例,该实例是执行逻辑的实例,同时还维护类线程所在线程组ThreadGroup、
ThreadLocal,其中ThreadLocal并不是Thread类中的类,但是该类可以为多线程中的每个线程提供一个相对独立的数据空间,
而且inheritableThreadLocals能够实现线程间数据共享,但是inheritableThreadLocals实现的数据共享只是数据副本,
2、native方法
Thread中提供了大量的native方法,这些方法都多数是使用c、c++编写的库
/**
* 返回当前正在执行的线程对象的引用.
*
* @return 当前正在执行的线程.
*/
public static native Thread currentThread();
/**
* 给调度器的提示,表明当前线程愿意放弃当前对处理器的使用。调度器可以自由地忽略此提示。
* Yield是一种启发式尝试,旨在改善线程之间的相对进度,否则会过度使用CPU。它的使用应该与详细的分析和基准测试相结合,以确保它确实具有预期的效果。
* 使用这种方法很少是合适的。它可能对调试或测试有用,因为它可能有助于重现由于竞争条件而导致的错误。在设计并发控制构造(如java.util.concurrent.locks包中的那些)时,它可能也很有用
* {@link java.util.concurrent.locks} package.
*/
public static native void yield();
/**
* 使当前执行的线程在指定的毫秒数内休眠(暂时停止执行),这取决于系统计时器和调度器的精度和准确性。线程不会失去任何监视器的所有权
*
* @param 以毫秒为单位的睡眠时间长度
*
* @throws IllegalArgumentException -如果millis的值是负的
*
* @throws InterruptedException -如果任何线程中断了当前线程。抛出此异常时,清除当前线程的中断状态
*/
public static native void sleep(long millis) throws InterruptedException;