源码仓库Gitee · GitHub背景进程和线程的区别进程的内存大小为堆内存线程数量*栈内存即线程数量 最大地址空间[MaxProcessMemory] - JVM堆内存 - 系统保留内存[ReservedOsMemory] / ThreadStackSize(XSS)从中可以看出线程的数量随栈内存的增多而减少。线程是程序执行的一个路径每一个线程都有自己的局部变量表、程序计数器(指向正在执行的指令指针)以及各自的生命周期。当启动了一个Java虚拟机(JVM)时从操作系统开始就会创建一个新的进程(JVM进程)JVM进程将会派生或者创建很多线程。一个线程的创建肯定是由另一个线程完成的被创建线程的父线程是创建它的线程线程会带来额外的开销如CPU调度时间、并发控制开销等每个线程在自己的工作内存交互加载和存储主内存控制不当会造成数据不一致。一.线程创建方式构造Thread类实现线程的执行单元run有两种方式分别是下面继承Thread重写run方法Thread实现了Runnable接口使用start开启线程start开启后线程会加入调度器然后调用run方法start会调用start0本地方法跟OS进行交互运行下面是start源码解析/** * Causes this thread to begin execution; the Java Virtual Machine * calls the coderun/code method of this thread. * 开启线程JVM会调用run方法【start使用了模板方法】 * p * It is never legal to start a thread more than once. * 不能两次启动线程否则报IllegalThreadStateException异常 * In particular, a thread may not be restarted once it has completed * execution. * 一个线程生命周期结束也就是到了TERMINATED状态再次调用start方法是不允许的 * 也就是TERMINATED状态没法回到RUNNABLE/RUNNING状态。 * * 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. * 线程被构造后的new状态threadStatus的属性值是0 */ 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 groups list of threads * and the groups 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 */ } } }void add(Thread t) { synchronized (this) { if (destroyed) {//线程组状态校验 throw new IllegalThreadStateException(); } if (threads null) { threads new Thread[4];//初始化长度为4的线程组 } else if (nthreads threads.length) { threads Arrays.copyOf(threads, nthreads * 2);//数组满了就扩容2倍 } threads[nthreads] t;//当前线程添加到线程组中 // This is done last so it doesnt matter in case the // thread is killed nthreads;//线程数1 // The thread is now a fully fledged member of the group, even // though it may, or may not, have been started yet. It will prevent // the group from being destroyed so the unstarted Threads count is // decremented. nUnstartedThreads--;//未启动线程数-1 } }private native void start0();//本地方法调用重写的run方法void threadStartFailed(Thread t) { synchronized(this) { remove(t);//移除当前线程 nUnstartedThreads;//没有启动的线程数量1 } } //测试Thread t new Thread(){ Override public void run(){ try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }; t.start(); t.start();//不能两次启动第二次启动是不允许的报IllegalThreadStateException此时该线程是处于运行状态 //测试 Thread t new Thread(){ Override public void run(){ try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }; t.start(); TimeUnit.SECONDS.sleep(10);//设置休眠时间上面的线程的生命周期已经终止下面再次启动报IllegalThreadStateException t.start();实现Runnable接口重写run方法并且将Runnable实例用作构造Thread的参数【单继承有局限性推荐使用接口】将线程的控制(start)和业务逻辑(run)的运行彻底分离开来使用的是策略模式Thread的run方法是不能共享的但Runnbale的run方法可以共享使用同一个Runnable的实例构造不同的Thread实例把实现类对象(实现Runnable接口的类的实例化)放入代理类对象(Thread构造方法)中使用的是代理模式下面是静态代理的代码解释public class StaticProxy { public static void main(String[] args) { new Weeding(new Me()).happyMarry(); // new Thread(对象).start();类似 } } interface Marry { void happyMarry(); } //真实角色 class Me implements Marry { Override public void happyMarry() { System.out.println(me will marry!); } } //代理对象 class Weeding implements Marry{ //真实角色 private Marry marry; public Weeding(Marry marry){ this.marrymarry; } Override public void happyMarry() { System.out.println(start); marry.happyMarry(); System.out.println(end); } }实现Callable接口重写call方法Future获取返回值Callable能接受一个泛型然后在call方法中返回一个指定类型的值public interface CallableV { V call() throws Exception; }//线程池队列开启线程不会产生脏读数据 //使用步骤//1.创建目标对象new //2.创建执行服务线程池 //3.提交执行submit //4.获取结构get //5.关闭服务shutdownNow public class MyThread implements Callable { private static int count 20; public static void main(String[] args) throws ExecutionException, InterruptedException { MyThread m1 new MyThread(); MyThread m2 new MyThread(); MyThread m3 new MyThread(); MyThread m4 new MyThread(); ScheduledExecutorService service new ScheduledThreadPoolExecutor(2, new BasicThreadFactory.Builder().namingPattern(schedule-pool-%d).daemon(true).build()); Future submit service.submit(m1); Future submit1 service.submit(m2); Future submit2 service.submit(m3); Future submit3 service.submit(m4); System.out.println(submit.get()); System.out.println(submit1.get()); System.out.println(submit2.get()); System.out.println(submit3.get()); service.shutdown(); } Override public Object call() throws Exception { count--; return count; } }匿名内部类new Thread(){//相当于继承Thread的方式 public void run(){ System.out.println(thread1 start ... ); } }.start(); new Thread(new Runnable() {//相当于实现Runnable接口的方式 Override public void run() { System.out.println(thread2 start .... ); } }).start();定时器(Timer)Timer timer new Timer();//创建时间器 timer.schedule(new TimerTask() {//使用schedule参数为定时器任务并重写run方法 Override public void run() { System.out.println(timer task is run); } }, 0, 1000);线程池(内部使用队列所以加入线程池的线程是顺序执行)使用execute和重写Runnbale的run方法ScheduledExecutorService service new ScheduledThreadPoolExecutor(2, new BasicThreadFactory.Builder().namingPattern(schedule-pool-%d).daemon(true).build()); service.execute(new Runnable() { Override public void run() { System.out.println(run test); } });lambda表达式new Thread(()- { for(int i 1 ; i10 ; i){ System.out.println(It is a lambda function!); } }).start();Spring方式(Async注解)Test public void test() { run(); } Async public void run(){ System.out.println(Async Test); }二.线程生命周期new新生状态当用new创建一个Thread对象时此时它并不处于执行状态因为没有调用star启动该线程那么线程的状态为new状态也就是说它只是Thread对象的状态在没有start之前该线程是不存在的runnable就绪状态线程对象进入runnable就绪状态必须调用start方法那么此时才是真正地在JVM进程中创建了一个线程就绪状态不会直接进入阻塞状态和死亡状态即使是在线程的执行逻辑中调用wait、sleep或其他block的IO操作等也必须先获得CPU的调度执行权才可以严格来说就绪状态的线程只能意外终止或进入运行状态running运行状态一旦CPU通过轮询或其他方式从任务可执行队列中选中了线程此时它才能真正地执行自己的逻辑代码一个正在running状态的线程事实上也是一个runnable的但是反过来则不成立sleep使当前线程进入指定毫秒级的休眠暂停执行但不会放弃monitor锁的所有权即不会释放锁资源使用TimeUnit来替代Thread.sleep省去了时间单位的换算步骤yield属于一种启发式的方法其会提醒调度器我愿意放弃当前的CPU资源如果CPU的资源不紧张则会忽略这种提醒yield只是一个提示(hint)CPU调度器并不会担保每次都能满足yield提示sleep和yield的区别sleep会导致当前线程暂停指定的时间没有CPU时间片的消耗yield只是对CPU调度器的一个提示如果CPU调度器没有忽略这个提示它会导致线程上下文的切换sleep会使线程短暂block会在给定的时间内释放CPU资源yield会使running状态的线程进入runnable状态如果CPU调度器没有忽略这个提示的话sleep几乎百分之百地完成了给定时间的休眠但yield的提示并不能一定担保一个线程sleep另一个线程interrupt会捕获到中断信号而yield则不会joinjoin某个线程A会使当前线程B进入等待直到线程A结束生命周期可以使用join来达到线程顺序执行的效果wait表示线程一直等待直到其他线程通知与sleep不同的是它会释放锁调用wait会加入wait set中notify会随机唤醒一个notifyAll会弹出所有线程notify唤醒一个处于等待状态的线程notifyAll唤醒同一个对象上所有调用wait方法的线程优先级高的线程优先调度synchronized同步内置锁、互斥锁、可重入锁锁定共享资源(共享资源对象不能为null使用static修饰保持对象引用地址只有一份)依赖JVMJVM指令是monitor enter和monitor exitsynchronized的指令严格遵守java happens-before规则一个monitor exit指令之前必定要有一个monitor enter不可中断锁适合竞争不激烈可读性好锁信息存在对象头中Mark Word线程idEpoch对象的分代年龄信息是否是偏向锁锁标志位Class Metadata Address使用范围修饰代码块大括号括起来的代码作用于调用的对象Override public void run() { synchronized (this) {//类A实现了Runnable重写了run方法实例化对象a,b分别加入到Thread构造方法中并开启线程this是两个不同的对象 System.out.println(this.hashCode()); } } MyThread thread new MyThread(); MyThread thread2 new MyThread(); Thread t1 new Thread(thread, t1); Thread t2 new Thread(thread2, t2); Thread t3 new Thread(thread, t3); Thread t4 new Thread(thread, t4); t1.start(); t2.start(); t3.start(); t4.start(); 结果 693024158 1259146238 1259146238 1259146238修饰方法整个方法作用于调用的对象Override public synchronized void run() {//类A实现了Runnable重写了run方法实例化对象a,b分别加入到Thread构造方法中并开启线程this是两个不同的对象 System.out.println(this.hashCode()); } MyThread thread new MyThread(); MyThread thread2 new MyThread(); Thread t1 new Thread(thread, t1); Thread t2 new Thread(thread2, t2); Thread t3 new Thread(thread, t3); Thread t4 new Thread(thread, t4); t1.start(); t2.start(); t3.start(); t4.start(); 结果 487590100 697138600 697138600 697138600修饰静态方法整个静态方法作用于所有对象Override public void run() { test1(); } public static synchronized void test1(){ System.out.println(MyThread.class.hashCode()); } MyThread thread new MyThread(); MyThread thread2 new MyThread(); Thread t1 new Thread(thread, t1); Thread t2 new Thread(thread2, t2); Thread t3 new Thread(thread, t3); Thread t4 new Thread(thread, t4); t1.start(); t2.start(); t3.start(); t4.start(); 结果 6566818 6566818 6566818 6566818修饰类括号括起来的部分作用于所有对象Override public void run() { test1(); } public static void test1(){ synchronized (MyThread.class) { System.out.println(MyThread.class.hashCode()); } } MyThread thread new MyThread(); MyThread thread2 new MyThread(); Thread t1 new Thread(thread, t1); Thread t2 new Thread(thread2, t2); Thread t3 new Thread(thread, t3); Thread t4 new Thread(thread, t4); t1.start(); t2.start(); t3.start(); t4.start(); 结果 6566818 6566818 6566818 6566818Lock显示锁依赖特殊的CPU指令可中断锁多样化同步竞争激烈时能维持常态public class MyLock implements Lock {//自定义Lock private boolean isLocked false; Override public void lock() { while (isLocked){//已经获得锁 try { wait();//等待 } catch (InterruptedException e) { e.printStackTrace(); } } isLockedtrue;//获锁成功 } Override public void unlock() { isLocked false;//释放锁 notify();//唤醒等待线程 } }