1.创建线程的多种方式方法一继承Thread来创建一个类class MyThread extends Thread{ override public void run(){ System.out.println(打个比方) } } public class Demo1{ public static void main(String[] args){ Mythread tnew MyThread(); t.start(); } }方法二实现Runnable接口class MyRunnable inplement Runnable{ override publicn void run(){ System.out.println(打个比方) } } public class Demo2{ public static void main(String[] args){ Thread tnew Thread(new MyRunnable()); t.start(); } }方法三匿名内部类创建thread子类对象Thread tnew Thread(){ override public void run(){ System.out.println(举个例子)}; };方法四匿名内部类创建Runnable子类对象Thread t2 new Thread(new Runnable() { Override public void run() { System.out.println(使用匿名类创建 Runnable 子类对象); } });方法五lambda表达式创建Runnable子类对象Thread t3 new Thread(() - System.out.println(使用匿名类创建 Thread 子类对象)); Thread t4 new Thread(() - { System.out.println(使用匿名类创建 Thread 子类对象); });2.多线程的优势-提升运行速度通过并发执行和单独执行进行比较public class demo1 { private static final long count 1000000000; public static void main(String[] args) throws InterruptedException { Thrm1(); Thrm2(); } private static void Thrm1() throws InterruptedException { long beginSystem.nanoTime(); Thread threadnew Thread(() -{ int a0; for (int i 0; i count; i) { a--; } }); thread.start(); int b0; for (int i 0; i count; i) { b--; } thread.join(); long end System.nanoTime(); double time(end-begin)*1.0/1000/1000; System.out.printf(%f %n,time); } private static void Thrm2() { long beginSystem.nanoTime(); int a0; for (int i 0; i count; i) { a--; } int b0; for (int i 0; i count; i) { b--; } long end System.nanoTime(); double time(end-begin)*1.0/1000/1000; System.out.printf(%f %n,time); } }并发: 399.651856 毫秒 串行: 720.616911 毫秒3.Thread类的常用方法用lambda举个例子public class ThreadLambdaExample { public static void main(String[] args) { // 1. 无参构造空任务 Thread t1 new Thread(() - {}); // 2. 传入Runnable任务 Thread t2 new Thread(() - { System.out.println(线程t2执行中...); }); // 3. 指定线程名空任务 Thread t3 new Thread(() - {}, 这是我的名字); // 4. 传入Runnable任务并指定线程名 Thread t4 new Thread(() - { System.out.println(线程t4执行中...); }, 这是我的名字); // 启动线程 t2.start(); t4.start(); } }3.Thread的几个常见属性属性获取方法idgetId()名称getName()状态优先级getState()是否为后台线程getPriority()是否存活isAlive()是否被中断isInterrupted()ID 是线程的唯一标识不同线程不会重复名称是各种调试工具用到状态表示线程当前所处的一个情况下面我们会进一步说明优先级高的线程理论上来说更容易被调度到关于后台线程需要记住一点JVM会在一个进程的所有非后台线程结束后才会结束运行。是否存活即简单的理解为 run 方法是否运行结束了线程的中断问题下面我们进一步说明public class demo2 { public static void main(String[] args) { Thread threadnew Thread(() - { try { System.out.println(Thread.currentThread().getName()我还活着); Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(Thread.currentThread().getName()我马上死兄弟); }); System.out.println(Thread.currentThread().getName()IDthread.getId()); System.out.println(Thread.currentThread().getName()名字thread.getName()); System.out.println(Thread.currentThread().getName()状态thread.getState()); System.out.println(Thread.currentThread().getName()优先级thread.getPriority()); System.out.println(Thread.currentThread().getName()后台线程thread.isDaemon()); System.out.println(Thread.currentThread().getName()活着thread.isAlive()); System.out.println(Thread.currentThread().getName()被中断thread.isInterrupted()); thread.start(); while(thread.isAlive()){ System.out.println(Thread.currentThread().getName()状态thread.getState()); } } }