Java多线程读这一篇即可!

1.线程与进程。

进程是程序的一次执行过程,一个进程可以有多个线程。(看视频可以同时看见图像和弹幕还能听见声音 )
在这里插入图片描述

2.线程的创建

2.1创建方式

在这里插入图片描述

2.2继承Thread(单继承局限性)

public class TestThread1 extends Thread{
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 20; i++) {
    
    
            System.out.println("康康吃了"+i+"坨屎");
        }
    }

    public static void main(String[] args) {
    
    
        //创建线程对象
        TestThread1 testThread1 = new TestThread1();
        //开启线程
        testThread1.start();
        for (int i = 0; i < 2000; i++) {
    
    
            System.out.println("浩哥吃了"+i+"坨屎");
        }
    }
}

2.3实现Runnable(推荐)

  • Thread是实现了Runnable接口,我们创建线程是只需要在Runnable实现类里重写run()方法对逻辑进行实现,再将Runnable实现类交给Thread的子类进行代理,即可由其代理实现我们编写的逻辑。(静态代理模式)
public class TestThread2 implements Runnable{
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 20; i++) {
    
    
            System.out.println("康康吃了"+i+"坨屎");
        }
    }

    public static void main(String[] args) {
    
    
        //创建Runnable实现类对象
        TestThread2 testThread2 = new TestThread2();
        //创建线程对象并开启线程
        new Thread(testThread2).start();
        for (int i = 0; i < 2000; i++) {
    
    
            System.out.println("浩哥吃了"+i+"坨屎");
        }
    }

2.4实现Callable

public class TestCallable implements Callable<Boolean> {
    
    
    @Override
    public Boolean call() throws Exception {
    
    
        for (int i = 0; i < 20; i++) {
    
    
            System.out.println("康康吃了" + i + "坨屎");
        }
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
    
    
        //获取Callable实现对象
        TestCallable testThread3 = new TestCallable();
        //启动方式一:通过线程池启动
//        //创建线程池
//        ExecutorService ser = Executors.newFixedThreadPool(1);
//        //提交Callable实现对象去执行
//        Future<Boolean> result1 = ser.submit(testThread3);
//        //获取执行对象
//        boolean r1 = result1.get();
//        //关闭线程池
//        ser.shutdownNow();

        //启动方式二
        FutureTask<Boolean> futureTask = new FutureTask<Boolean>(testThread3);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }
}

3.线程的五大状态

3.1图解

在这里插入图片描述

3.2常用方法

在这里插入图片描述

3.3线程停止(stop)

public class TestStop implements Runnable {
    
    
    //设置标识符
    private Boolean flag = true;
    //自定义stop
    public void stop() {
    
    
        this.flag = false;
    }

    @Override
    public void run() {
    
    
        while (flag) {
    
    
            System.out.println("run.....");
        }
    }

    public static void main(String[] args) {
    
    
        TestStop testStop = new TestStop();
        new Thread(testStop).start();
        for (int i = 0; i < 100000; i++) {
    
    
            if (i == 99999) {
    
    
                testStop.stop();
            }
        }
    }
}

3.4线程休眠(sleep)

  • 模拟倒计时
public class TestSleep implements Runnable {
    
    

    @Override
    public void run() {
    
    
        int num = 10;
        while (true) {
    
    
            try {
    
    
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println(num--);
            if (num<=0){
    
    
                break;
            }
        }
    }

    public static void main(String[] args) {
    
    
        TestSleep testSleep = new TestSleep();
        new Thread(testSleep).start();
    }
}

3.5线程礼让(yield)

  • 礼让线程,让当前正在执行的线程暂停,但不阻塞
  • 将线程从运行状态转为就绪状态
  • 让cpu重新调度,礼让不一定成功!看CPU心情
public class TestYield {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"a").start();
        new Thread(myYield,"b").start();
    }
}

class MyYield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+":start...");
        //礼让
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+":end...");
    }
}

3.6 Join

  • Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞。
  • 可以想象成插队。
public class TestJoin implements Runnable{
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 100; i++) {
    
    
            System.out.println(i + "插队了");
        }
    }

    public static void main(String[] args) throws InterruptedException {
    
    
        TestJoin testJoin = new TestJoin();
        Thread thread = new Thread(testJoin);
        thread.start();

        //主线程 当i==100时 thread来插队 其他线程休眠等待
        for (int i = 0; i < 500; i++) {
    
    
            System.out.println(i+"来排队了");
            if (i==100){
    
    
                thread.join();
            }
        }
    }
}

3.7 Thread.State

在这里插入图片描述

public class TestState implements Runnable {
    
    
    @Override
    public void run() {
    
    
        System.out.println("康康吃屎");
        try {
    
    
            Thread.sleep(500);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("浩浩吃屎");
    }

    public static void main(String[] args) throws InterruptedException {
    
    
        //创建Runnable实现类对象
        TestState testThread2 = new TestState();
        //创建线程
        Thread thread = new Thread(testThread2);
        Thread.State state = thread.getState();
        System.out.println(state);//NEW
        //启动
        thread.start();
        state = thread.getState();
        System.out.println(state);//RUNNABLE
        //运行状态监测
        while (state!=Thread.State.TERMINATED){
    
    
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);//TIMED_WAITING TERMINATED
        }
    }
}

3.8线程优先级(Priority)

  • 优先级越高越先执行,其他线程要等比起优先级高的线程执行完才执行。(一般情况)
  • 优先级低只是意味着获得调度的概率低,并不是优先级低就不会被先调用了,这都是看CPU的调度。
  • getPriority() . setPriority(int xxx)
  • 优先级的设定最好在start()调度前。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H14Z40l2-1614317720854)(C:%5CUsers%5C11324%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210225203155834.png)]

public class TestPriority {
    
    
    public static void main(String[] args) {
    
    
        MyYield myYield = new MyYield();
        Thread thread1 = new Thread(myYield, "a");
        Thread thread2 = new Thread(myYield, "b");
        Thread thread3 = new Thread(myYield, "c");
        Thread thread4 = new Thread(myYield, "d");
        thread1.start();
        thread1.setPriority(4);
        thread2.start();
        thread2.setPriority(2);
        thread3.start();
        thread3.setPriority(1);
        thread4.start();
        thread4.setPriority(8);
    }
}

class MyPriority implements Runnable {
    
    
    @Override
    public void run() {
    
    
        System.out.println(Thread.currentThread().getName() + ":start...");
        try {
    
    
            Thread.sleep(200);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ":end...");
    }
}
//        d:start...
//        d:end...
//        a:start...
//        a:end...
//        c:start...
//        c:end...
//        b:start...
//        b:end...

3.9守护线程(daemon)

  • 线程分为用户线程和守护线程
  • 虚拟机必须确保用户线程执行完毕
  • 虚拟机不用等待守护线程执行完毕
  • 如,后台记录操作日志,监控内存,垃圾回收等待
public class TestDaemon {
    public static void main(String[] args) {
        MyDaemon myDaemon = new MyDaemon();
        MyDaemon2 myDaemon2 = new MyDaemon2();
        Thread thread = new Thread(myDaemon2, "a");
        Thread thread1 = new Thread(myDaemon, "b");
        //thread设置为守护线程
        thread.setDaemon(true);
        thread.start();
        thread1.start();
    }
}

class MyDaemon implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + ":还活着...");
        System.out.println(Thread.currentThread().getName() + ":死了...");
    }
}

4.线程同步(条件:队列+锁)

由于同一进程的多个线程共享同一块存储空间 , 在带来方便的同时,也带来了访问冲突问题 , 为了保证数据在方法中被访问时的正确性 , 在访问时加入锁机制synchronized , 当一个线程获得对象的排它锁 , 独占资源 , 其他线程必须等待 。

缺点:

  • 一个线程持有锁会导致其他所有需要此锁的线程挂起 。
  • 在多线程竞争下 , 加锁 , 释放锁会导致比较多的上下文切换和调度延时,引起性能问题 。
  • 如果一个优先级高的线程等待一个优先级低的线程释放锁会导致优先级倒置 , 引起性能问题。

4.1同步方法

  • 同步方法 : public synchronized void method(int args) {}
  • synchronized方法控制对 “对象” 的访问 , 每个对象对应一把锁 , 每个synchronized方法都必须获得调用该方法的对象的锁才能执行 , 否则线程会阻塞 ,方法一旦执行 , 就独占该锁 , 直到该方法返回才释放锁 , 后面被阻塞的线程才能获得这个锁 , 继续执行。
  • 缺陷 : 若将一个大的方法申明为synchronized 将会影响效率。
public class ByTicket implements Runnable {
    
    
    private int ticket = 20;
    Boolean flag = true;

    @Override
    public void run() {
    
    
        while (flag) {
    
    
            try {
    
    
                Thread.sleep(50);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            buy();
        }
    }

    //同步方法 synchronized 对this加锁
    private synchronized void buy() {
    
    
        if (ticket <= 0) {
    
    
            stop();
        }else {
    
    
            System.out.println(Thread.currentThread().getName() + "买了第" + ticket-- + "张票");
        }
    }

    public void stop() {
    
    
        this.flag = false;
    }

    public static void main(String[] args) {
    
    
        ByTicket byTicket = new ByTicket();
        new Thread(byTicket, "a").start();
        new Thread(byTicket, "b").start();
        new Thread(byTicket, "c").start();
    }
}

4.2同步块

  • 同步块 : synchronized (Obj ) { }
  • Obj 可以是任何对象 , 但是推荐使用共享资源作为同步监视器。
  • 同步方法中无需指定同步监视器 , 因为同步方法的同步监视器就是this。
public class ByTicket2 implements Runnable {
    
    
    final Ticket ticket = new Ticket();
    Boolean flag = true;

    @Override
    public void run() {
    
    
        while (flag) {
    
    
            try {
    
    
                Thread.sleep(50);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            buy();
        }
    }

    private void buy() {
    
    
        //同步块 ticket加锁
        synchronized (ticket) {
    
    
            int num = ticket.getTicket();
            if (num <= 0) {
    
    
                stop();
            } else {
    
    
                System.out.println(Thread.currentThread().getName() + "买了第" + num + "张票");
                ticket.setTicket(--num);
            }
        }

    }

    public void stop() {
    
    
        this.flag = false;
    }

    public static void main(String[] args) {
    
    
        ByTicket2 byTicket = new ByTicket2();
        new Thread(byTicket, "a").start();
        new Thread(byTicket, "b").start();
        new Thread(byTicket, "c").start();
    }
}

class Ticket {
    
    
    private int Ticket = 10;

    public int getTicket() {
    
    
        return Ticket;
    }

    public void setTicket(int ticket) {
    
    
        Ticket = ticket;
    }
}

4.3死锁

多个线程各自占有一些共享资源 , 并且互相等待其他线程占有的资源才能运行 , 而导致两个或者多个线程都在等待对方释放资源 , 都停止执行的情形 。 某一个同步块同时拥有 “ 两个以上对象的锁 ” 时 , 就可能会发生 “ 死锁 ” 的问题 。

public class DeadLock {
    
    
    public static void main(String[] args) {
    
    
        MyThread myThread = new MyThread(0);
        MyThread myThread1 = new MyThread(1);

        new Thread(myThread, "a").start();
        new Thread(myThread1, "b").start();
    }
}

class Class1 {
    
    

}

class Class2 {
    
    

}

class MyThread implements Runnable {
    
    
    //模拟每种资源只有一份
    final static Class1 class1 = new Class1();
    final static Class2 class2 = new Class2();
    int choice;

    public MyThread(int choice) {
    
    
        this.choice = choice;
    }

    @Override
    public void run() {
    
    
        try {
    
    
            go();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }

    private void go() throws InterruptedException {
    
    
        if (choice == 0) {
    
    
            synchronized (class1) {
    
    
                System.out.println(Thread.currentThread().getName() + "获得了class1的锁");
                Thread.sleep(100);
                synchronized (class2) {
    
    
                    System.out.println(Thread.currentThread().getName() + "获得了class2的锁");
                }
            }
        } else {
    
    
            synchronized (class2) {
    
    
                System.out.println(Thread.currentThread().getName() + "获得了class2的锁");
                Thread.sleep(100);
                synchronized (class1) {
    
    
                    System.out.println(Thread.currentThread().getName() + "获得了class1的锁");
                }
            }
        }
    }
}
  • 产生死锁的四个必要条件:
    1. 互斥条件:一个资源每次只能被一个进程使用。
    2. 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
    3. 不剥夺条件 : 进程已获得的资源,在末使用完之前,不能强行剥夺。
    4. 循环等待条件 : 若干进程之间形成一种头尾相接的循环等待资源关系。

只要破坏其中任意一个条件,就可以避免死锁。

一种非常简单的避免死锁的方式就是:**指定获取锁的顺序,并强制线程按照指定的顺序获取锁。**因此,如果所有的线程都是以同样的顺序加锁和释放锁,就不会出现死锁了。

4.4 Lock(显示锁)

  • java.util.concurrent.locks.Lock接口是控制多个线程对共享资源进行访问的工具。锁提供了对共享资源的独占访问,每次只能有一个线程对Lock对象加锁,线程开始访问共享资源之前应先获得Lock对象
  • ReentrantLock 类(可重入锁)实现了 Lock ,它拥有与 synchronized 相同的并发性和内存语义,在实现线程安全的控制中,比较常用的ReentrantLock,可以显式加锁、释放锁。
public class TestLock{
    
    
    public static void main(String[] args) {
    
    
        ByTicket byTicket = new ByTicket();
        new Thread(byTicket, "a").start();
        new Thread(byTicket, "b").start();
        new Thread(byTicket, "c").start();
    }
}


class ByTicket implements Runnable {
    
    
    private int ticket = 20;
    Boolean flag = true;
    // 添加私有ReentrantLock对象
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
    
    
        while (flag) {
    
    
            try {
    
    
                Thread.sleep(50);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            buy();
        }
    }

    private void buy() {
    
    
        try {
    
    
            //加锁
            lock.lock();
            if (ticket <= 0) {
    
    
                stop();
            } else {
    
    
                System.out.println(Thread.currentThread().getName() + "买了第" + ticket-- + "张票");
            }
        }finally {
    
    
            //解锁
            lock.unlock();
        }

    }

    public void stop() {
    
    
        this.flag = false;
    }
}

4.5 synchronized 与 Lock 的对比

  • Lock是显式锁(手动开启和关闭锁,别忘记关闭锁)synchronized是隐式锁,出了作用域自动释放。
  • Lock只有代码块锁,synchronized有代码块锁和方法锁。
  • 使用Lock锁,JVM将花费较少的时间来调度线程,性能更好。并且具有更好的扩展性(提供更多的子类)。
  • 优先使用顺序:Lock > 同步代码块(已经进入了方法体,分配了相应资源)> 同步方法(在方法体之外)。

5.线程协作(生产者消费者问题

  • Java用于线程通信的常用方法:

在这里插入图片描述

  • 生产者消费者问题解决方案:管程法、信号灯法。

6.线程池

  • 对比数据库连接池学习。
  • 思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可以避免频繁创建销毁、实现重复利用。类似生活中的公共交通工具。
  • 好处:
  1. 提高响应速度(减少 了创建新线程的时间)。
  2. 降低资源消耗(重复利用线程池中线程,不需要每次都创建)。
  3. 便于管理。
public class TestPool {
    
    
    public static void main(String[] args) {
    
    
        //创建连接池 参数最大连接数
        ExecutorService service = Executors.newFixedThreadPool(10);

        //执行
        service.execute(new MyThread1());
        service.execute(new MyThread1());
        service.execute(new MyThread1());
        service.execute(new MyThread1());

        //关闭连接
        service.shutdown();
    }
}
class MyThread1 implements Runnable {
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 5; i++) {
    
    
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}
  • ExecutorService:
    • void execute(Runnable command) :执行任务/命令,没有返回值,一般用来执行Runnable
    • Future submit(Callable task):执行任务,有返回值,一般又来执行Callable
    • void shutdown() :关闭连接池

猜你喜欢

转载自blog.csdn.net/else_tdk/article/details/114127879