java多线程和锁

警告
本文最后更新于 2023-05-05,文中内容可能已过时,请谨慎使用。

Thread 类的每一个实例都表示一个线程, 进程是操作系统级别的多任务,JVM 就是运行在一个进程中的。所以在 java 中我我们只考虑线程。进程有独立的内存,一个进程中的多个线程共享进程的内存,进程中至少要有一个线程。

方法1: 继承Thread

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker1 = new Worker();
        worker1.setName("thread-worker1");
        Worker worker2 = new Worker();
        worker2.setName("thread_worker2");
        worker1.start(); // worker1和worker2会争抢cpu资源,无论是谁一旦获取到cpu资源都会立刻执行
        worker2.start();
        Thread.sleep(1000); // 主线程会等待1s
        System.out.println("Main-thread finished!"); // 主线程会先于worker1和worker2结束
    }
}

class Worker extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Hello " + getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

启动线程要调用start方法,不能直接调用run方法。start方法会将当前线程纳入到线程调度中,使其具有并发运行的能力。start方法很快会执行完毕。当start方法执行完毕后,获取到了cpu时间片后当前线程的run方法才会被执行起来。但不能理解为调用start方法时run方法就执行了!

线程有几个不可控因素:

  1. cpu分配时间片给哪个线程我们说了不算。
  2. 时间片长短也不可控。
  3. 线程调度会尽可能均匀的将时间片分配给多个线程。

这种创建线程的方式存在两个不足:

  1. 由于java是单继承的,这就导致我们若继承了Thread类就无法再继承其他类,这在写项目时会遇到很大问题;
  2. 由于我们定义线程的同时重写run方法来定义线程要执行的任务,这就导致线程与任务有一个强耦合关系,线程的重用性变得非常局限。

方法2: 定义一个类并实现Runnable接口然后在创建线程的同时将任务指定。因为是实现Runnable接口,所以不影响其继承其他类

public class Main {
    public static void main(String[] args) {
        Worker worker = new Worker("thread1");
        new Thread(worker).start();
        new Thread(worker).start();
    }
}

class Worker implements Runnable{
    private String name;

   public Worker(String name) {
       this.name = name;
   }


    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Hello " + this.name);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Thread thread = Thread.currentThread(); // 获取当前线程
        System.out.println(thread);
        System.out.println(thread.getId()); // 线程id
        System.out.println(thread.getName()); // 线程名字
        System.out.println(thread.getPriority()); // 线程的优先级
        System.out.println(thread.isAlive()); // 线程是否存活
        System.out.println(thread.isDaemon()); // 是否为守护线程,主线程不是守护线程
        System.out.println(thread.isInterrupted()); // 线程是否被中断
    }
}

Thread提供了一个静态方法: sleep,该方法会阻塞运行当前方法的线程指定毫秒。当超时后,线程会自动回到Runnable状态,等待再次分配时间片运行:

System.out.println("程序开始了");
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
System.out.println("程序结束了");

join(long millis): 等待该线程执行结束,父线程才会继续执行; 可以传入一个最长等待时间,超过该时间后继续执行父线程

例如:主线程要等worker1worker2进程结束后执行

package cc.bnblogs;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker1 = new Worker();
        worker1.setName("thread-worker1");
        Worker worker2 = new Worker();
        worker2.setName("thread_worker2");
        worker1.start();
        worker2.start();

        worker1.join(); // 只有worker1执行完成之后才会执行后面的代码
        worker2.join(); // 只有worker2执行完成之后才会执行后面的代码
        System.out.println("Main-thread finished!");
    }
}

class Worker extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Hello " + getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

举一个下载图片线程和展示图片线程的例子:

public class Main {
    private static boolean isFinish = false;

    public static void main(String[] args) {
        Thread download = new Thread(() -> {
            System.out.println("开始下载图片...");
            for (int i = 1; i <= 100; i++) {
                System.out.println("down:" + i + "%");
                try {
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("图片下载完毕!");
            isFinish = true;
        });

        Thread show = new Thread(() -> {
            System.out.println("准备显示图片...");
            try {
                download.join(); // 等待download线程执行结束,执行完成后执行父进程
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!isFinish) {
                throw new RuntimeException("图片加载失败!");
            }
            System.out.println("图片显示完毕!");
        });
        download.start();
        show.start();
    }
}

interrupt():从休眠中中断线程

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker1 = new Worker();
        worker1.setName("thread-worker1");
        Worker worker2 = new Worker();
        worker2.setName("thread_worker2");
        worker1.start();
        worker2.start();

        // 主线程最多等待worker1线程5000ms
        worker1.join(5000);
        worker1.interrupt();  // 抛出InterruptedException异常

        System.out.println("Main-thread finished!");
    }
}

class Worker extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Hello " + getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // 收到InterruptedException后,结束该线程
                System.out.println(getName() + " stop!");
                break;
            }
        }
    }
}

后台线程,又叫做守护线程,当一个进程中的所有前台线程都结束了,进程就会结束,无论进程中的其他后台线程是否还在运行,都要被强制中断

setDaemon():设置某线程为守护线程

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker1 = new Worker();
        worker1.setName("thread-worker1");
        Worker worker2 = new Worker();
        worker2.setName("thread_worker2");
        // 将worker1和worker2设置为守护线程
        // 除守护线程之外的其他线程结束后(这里只有主线程),守护线程会自动结束
        worker1.setDaemon(true);
        worker2.setDaemon(true);

        worker1.start();
        worker2.start();

        // 主线程休眠5s
        Thread.sleep(5000);

        System.out.println("Main-thread finished!");
    }
}

class Worker extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Hello " + getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException();
            }
        }
    }
}

该方法用于使当前线程主动让出当次CPU时间片回到Runnable状态,等待分配时间片。

线程优先级分为10个等级,1最低,5默认,10最高。线程提供了3个常量:

  • MIN_PRIORITY:1 对应最低优先级
  • MAX_PRIORITY: 10 对应最高优先级
  • NORM_PRIORITY:5 默认优先级,主线程默认优先级为5

lock:获取锁,如果锁已经被其他线程获取,则阻塞 unlock:释放锁,并唤醒被该锁阻塞的其他线程

防止读写冲突,同一时间只有一个线程可以拥有锁,并进行写操作

import java.util.concurrent.locks.ReentrantLock;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker1 = new Worker();
        worker1.setName("thread-worker1");
        Worker worker2 = new Worker();
        worker2.setName("thread_worker2");

        worker1.start();
        worker2.start();

        worker1.join();
        worker2.join();

        System.out.println("Main-thread finished!");
        System.out.println("cnt: " + Worker.cnt);
    }
}

class Worker extends Thread {
    private static final ReentrantLock lock = new ReentrantLock();
    public static int cnt = 0;

    @Override
    public void run() {
        for (int i = 0; i < 200000; i++) {
            lock.lock();
            try {
                cnt++;
            }finally {
                lock.unlock();
            }
        }
    }
}

java实现锁的语法糖,继承Thread类和实现Runnable接口的线程使用方式有点区别

还是上面的cnt++的例子

1.继承Thread

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker1 = new Worker();
        worker1.setName("thread-worker1");
        Worker worker2 = new Worker();
        worker2.setName("thread_worker2");

        worker1.start();
        worker2.start();

        worker1.join();
        worker2.join();

        System.out.println("Main-thread finished!");
        System.out.println("cnt: " + Worker.cnt);
    }
}

class Worker extends Thread {
    public static int cnt = 0;
    private static final Object object = new Object();

    @Override
    public void run() {
		//锁加到了object对象上,多个线程共享一个object
        synchronized (object) {
            for (int i = 0; i < 200000; i++) {
                cnt++;
            }
        }
    }
}

2.实现Runnable接口

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker = new Worker();

        Thread worker1 = new Thread(worker);
        Thread worker2 = new Thread(worker);

        worker1.start();
        worker2.start();

        worker1.join();
        worker2.join();

        System.out.println("Main-thread finished!");
        System.out.println("cnt: " + Worker.cnt);
    }
}

class Worker implements Runnable {
    public static int cnt = 0;

    @Override
    public void run() {
        //锁加到了this对象上,而两个线程是由同一个worker创建而来的
        synchronized (this) {
            for (int i = 0; i < 200000; i++) {
                cnt++;
            }
        }
    }
}

也可以直接将synchronized作用到方法上,和上面的代码等价

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Worker worker = new Worker();

        Thread worker1 = new Thread(worker);
        Thread worker2 = new Thread(worker);

        worker1.start();
        worker2.start();

        worker1.join();
        worker2.join();

        System.out.println("Main-thread finished!");
        System.out.println("cnt: " + Worker.cnt);
    }
}

class Worker implements Runnable {
    public static int cnt = 0;

    @Override
    public void run() {
        Worker.work();
    }

    private synchronized static void work() {
        for (int i = 0; i < 200000; i++) {
            cnt++;
        }
    }
}

Object类中定义了两个方法wait()和notify()。它们也可以实现协调线程之间同步工作的方法。当一个线程调用了某个对象的wait方法时,这个线程就进入阻塞状态,直到这个对象的notify方法被调用,这个线程才会解除wait阻塞,继续向下执行代码。

若多个线程在同一个对象上调用wait方法进入阻塞状态后,那么当该对象的notify方法被调用时,会随机解除一个线程的wait阻塞,这个不可控。若希望一次性将所有线程的wait阻塞解除,可以调用notifyAll方法。

前面5个线程会等待1s后自动唤醒一个线程,唤醒的线程睡眠1s后叫醒下一个线程

public class Main {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Worker worker = new Worker(true);
            worker.setName("Thread_" + i);
            worker.start();
        }
        Worker worker = new Worker(false);
        worker.setName("Thread_5");
		// 第6个线程先睡2s再去唤醒线程,这时候已经晚了
        Thread.sleep(2000);
        worker.start();

    }
}

class Worker extends Thread {
    private final boolean needWait;
    // 定义一个全局object
    private static final Object object = new Object();

    public Worker(boolean needWait) {
        this.needWait = needWait;
    }

    @Override
    public void run() {
        synchronized (object) {
            try {
                if (needWait) {
                    // 最多等待1s,超过1s会自动唤醒一个线程
                    object.wait(1000);
                    System.out.println(getName() + " 被唤醒了!");
                    //睡眠1s后继续唤醒其他线程
                    Thread.sleep(1000);
                } else {
                    // 不需要睡眠的线程唤醒一个线程
                    object.notify();
                    System.out.println("尝试唤醒其他线程");

                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

当然也可以不使用静态变量object

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();
        for (int i = 0; i < 5; i++) {
            Worker worker = new Worker(object, true);
            worker.setName("Thread_" + i);
            worker.start();
        }
        Worker worker = new Worker(object, false);
        worker.setName("Thread_5");
        // 第6个线程先睡2s再去唤醒线程,这时候已经晚了
        Thread.sleep(2000);
        worker.start();

    }
}

class Worker extends Thread {
    private final boolean needWait;
    private final Object object;

    public Worker(Object object, boolean needWait) {
        this.object = object;
        this.needWait = needWait;
    }

    @Override
    public void run() {
        synchronized (object) {
            try {
                if (needWait) {
                    // 最多等待1s,超过1s会自动唤醒一个线程
                    object.wait(1000);
                    System.out.println(getName() + " 被唤醒了!");
                    //睡眠1s后继续唤醒其他线程
                    Thread.sleep(1000);
                } else {
                    // 不需要睡眠的线程唤醒一个线程
                    object.notify();
                    System.out.println("尝试唤醒其他线程");

                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

当我们的逻辑中出现了会频繁创建线程的情况时,就要考虑使用线程池来管理线程。这可以解决创建过多线程导致的系统威胁。

线程池主要解决两个问题:

  1. 控制线程数量
  2. 重用线程
public class Main {
    public static void main(String[] args) {
        //创建一个固定大小的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        //指派5个任务
        for (int i = 0; i < 5; i++) {
            Runnable runn = () -> {
                Thread t = Thread.currentThread();
                System.out.println(t + "正在运行任务!");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(t + "执行任务完毕");
            };
            threadPool.execute(runn);
        }
        //停止线程池
        threadPool.shutdown();
    }
}

此时同时只能有三个线程运行