Java多线程笔记

2021-04-04  本文已影响0人  zqyadam

基本概念

程序(program):是为完成特定任务、用某种语言编写的一组指令的集合。即指一段静态的代码,静态对象。

进程(process):是程序的一次执行过程,或是正在运行的一个程序。是一个动态的过程:有它自身的产生、存在和消亡的过程。--生命周期

线程(thread):进程可进一步细化为线程,是一个程序内部的一条执行路径

何时需要多线程

线程的创建和使用

方式一:继承Thread类

1)定义子类继承Thread类。
2)子类中重写Thread类中的run方法。
3)创建Thread子类对象,即创建了线程对象。
4)调用线程对象start方法:启动线程,调用run方法。

注意点:

  1. 如果自己手动调用run()方法,那么就只是普通方法,没有启动多线程模式。
  2. run()方法由JVM调用,什么时候调用,执行的过程控制都有操作系统的CPU调度决定。
  3. 想要启动多线程,必须调用start方法。
  4. 一个线程对象只能调用一次start()方法启动,如果重复调用了,则将抛出非法线程状态的异常"IllegalThreadStateException"。

代码:

public class ThreadTest {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
        MyThread thread2 = new MyThread();
        thread2.start();
        for (int i = 0; i < 1000; i++) {
            System.out.println(i+"---- main ----");
        }
    }
}


class MyThread extends Thread{

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}

方式二:实现Runnable接口

  1. 创建一个实现Runnable接口的类。
  2. 在实现类中实现Runnable接口中的run()抽象方法。
  3. 创建实现类的对象。
  4. 将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象。
  5. 通过Thread类的对象调用start()

示例:

public class ThreadTest2 {
    public static void main(String[] args) {
        MyThread thread = new MyThread();

        new Thread(thread).start();
    }
}

class MyThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
  
}

继承Thread方式和实现Runnable方式的联系与区别

区别

实现Runnable方式的好处(优先选择)

方式三:实现Callable接口(JDK5.0新增)

与使用Runnable相比,Callable功能更强大些

Future接口

使用步骤

  1. 创建Callable接口的实现类

  2. 实现call方法,将此线程需要执行的操作声明在call()中

  3. 创建Callable接口实现类的对象

  4. 将此Callable接口实现类的对象传递给FutureTask的构造器中,创建FutureTask的对象

  5. 将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()启动线程

  6. 获取Callable中call()的返回值,该操作需要在启动线程后进行,否则将卡在此处。

例:利用线程获取0~100中偶数的返回值


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/*
 * 实现Callable接口创建线程
 */

//1. 创建Callable接口的实现类
class NumberCallable implements Callable {

    // 2. 实现call方法,将此线程需要执行的操作声明在call()中
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                sum += i;
            }
        }
        return sum; // 如果没有返回值,就return null;
    }
}

public class CallableThreadTest {
    public static void main(String[] args) {
        // 3. 创建Callable接口实现类的对象
        NumberCallable number = new NumberCallable();
        // 4. 将此Callable接口实现类的对象传递给FutureTask的构造器中,创建FutureTask的对象
        FutureTask task = new FutureTask(number);
        // 5. 将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()启动线程
        new Thread(task).start();
        
        try {
            // 6. 获取Callable中call()的返回值,该操作需要在启动线程后进行,否则将卡在此处。
            // get()返回值即为FutureTask构造参数Callable实现类重写的call()的返回值。
            Object sum = task.get();
            System.out.println("sum:" + sum);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

方式四:使用线程池(JDK5.0新增)

背景:经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大。

思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可以避免频繁创建销毁、实现重复利用。类似生活中的公共交通工具。

好处

线程池相关API

使用ThreadPoolExecutor设置线程池参数

import java.util.concurrent.ThreadPoolExecutor;

ExecutorService executorService = Executors.newFixedThreadPool(10);
ThreadPoolExecutor executor = (ThreadPoolExecutor)executorService;

executor.setCorePoolSize(15);
executor.setKeepAliveTime(3600, TimeUnit.SECONDS);

例:

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/*
 * 使用线程池
 */

class NumberRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}

public class ThreadPoolTest {
    public static void main(String[] args) {
        // 1. 提供指定线程数量的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        // 2. 执行指定的线程操作。需要提供实现Runnable接口或Callable接口的实现类的对象

        // 2.1 使用execute执行Runnable接口的实现类对象
        executorService.execute(new NumberRunnable());
        // 2.2 使用submit执行Callable接口的实现类对象
        Future future = executorService.submit(new NumberCallable());
        try {
            System.out.println(Thread.currentThread().getName() + ":Total:" + future.get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 关闭连接池
        executorService.shutdown();
    }
}

Thread中常用方法

1.void start()

启动线程,并执行对象的run()方法

2.run()

线程在被调度时执行的操作,通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中

3.static currentThread()

返回当前线程。在Thread子类中就是this,通常用于主线程和Runnable实现类

4.getName()/setName()

返回/设置线程的名称

5.static void yield()

线程让步,释放当前CPU的执行权

6.join()

当某个程序执行流中调用其他线程的join()方法时,调用线程将被阻塞,直到join()方法加入的join线程执行完为止。

也就是说,在线程a中调用线程b的join(),此时线程a就进入阻塞状态,直到线程b完全执行完毕以后,线程a才结束阻塞状态。

7.stop() "Deprecated"

强制线程生命期结束,不推荐使用

8.static void sleep(long millis) :(指定时间:毫秒)

令当前活动线程在指定时间段内放弃对CPU控制,使其他线程有机会被执行,时间到后重排队。

就是让当前线程“睡眠”指定的millis毫秒,在指定的millis毫秒时间内,当前线程是阻塞状态。

9.boolean isAlive()

返回boolean,判断线程是否还活着

线程的优先级

线程的优先级等级

涉及的方法

说明

线程的分类

Java中的线程分为两类:一种是守护线程,一种是用户线程

线程的生命周期

JDK 中用Thread.State类定义了线程的几种。

要想实现多线程,必须在主线程中创建新的线程对象。Java语言使用Thread类及其子类的对象来表示线程,在它的一个完整的生命周期中通常要经历如下的 五种状态:

线程状态转换图

线程的同步

方式一:同步代码块

synchronized(同步监视器){
//  需要被同步的代码;
}

说明:

  1. 操作共享数据的代码,即为需要被同步的代码。不能包含代码多了,也不能包含代码少了。

  2. 共享数据:多个线程共同操作的变量。比如:ticket就是共享数据。

  3. 同步监视器,俗称:锁。任何一个类的对象,都可以充当锁。

    要求:多个线程必须要共用同一把锁。

    在实现Runnable接口创建多线程的方式中,可以考虑使用this充当同步监视器。

    在继承Thread类创建多线程的方式中,慎用this充当同步监视器,考虑使用当前类(类名.class)充当同步监视器。

例:

  1. 在实现Runnable接口中使用同步代码块
public class SellTicketTest {
    public static void main(String[] args) {
        SellTickets tickets = new SellTickets();
        Thread window1 = new Thread(tickets);
        Thread window2 = new Thread(tickets);
        Thread window3 = new Thread(tickets);

        window1.setName("Window 1");
        window2.setName("Window 2");
        window3.setName("Window 3");

        window1.start();
        window2.start();
        window3.start();
    }
}

class SellTickets implements Runnable {
    private int tickets = 1000;
    // 同步锁
    private Object lock = new Object();

    @Override
    public void run() {

        while (true) {
            synchronized (lock) {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + tickets);
                    tickets--;
                } else {
                    break;
                }
            }
        }

    }
}

  1. 在继承Thread类中使用同步代码块
public class SellTicketTest2 {
    public static void main(String[] args) {
        Thread window1 = new SellTickets2();
        Thread window2 = new SellTickets2();
        Thread window3 = new SellTickets2();

        window1.setName("Window 1");
        window2.setName("Window 2");
        window3.setName("Window 3");

        window1.start();
        window2.start();
        window3.start();
    }
}

class SellTickets2 extends Thread {
    private int tickets = 1000;
  // 这里的lock是static的变量
    private static Object lock = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                /*
                 * 这行代码如果放在while(true)上方,则会出现问题
                 * -- 直到while(true)执行完毕,其他的线程才有可能进来,这时票已经卖完了
                 */
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + tickets);
                    tickets--;
                } else {
                    break;
                }
            }
        }
    }
}

方式二:同步方法

如果操作共享数据的代码证号完整的声明在一个方法中,可以将此方法声明为同步方法。

同步监视器:

例:

非静态的同步方法:

public class SellTicketTest3 {
    public static void main(String[] args) {
        SellTickets3 tickets = new SellTickets3();
        Thread window1 = new Thread(tickets);
        Thread window2 = new Thread(tickets);
        Thread window3 = new Thread(tickets);

        window1.setName("Window 1");
        window2.setName("Window 2");
        window3.setName("Window 3");

        window1.start();
        window2.start();
        window3.start();
    }
}

class SellTickets3 implements Runnable {
    private int tickets = 100;

    @Override
    public void run() {

        while (hasTicket()) {   
        }
    }
    
    private synchronized boolean hasTicket() {
        if (tickets > 0) {
            System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + tickets);
            tickets--;
            return true;
        } else {
            return false;
        }
    }
}

静态的同步方法:

public class SellTicketTest4 {
    public static void main(String[] args) {

        Thread window1 = new SellTickets4();
        Thread window2 = new SellTickets4();
        Thread window3 = new SellTickets4();

        window1.setName("Window 1");
        window2.setName("Window 2");
        window3.setName("Window 3");

        window1.start();
        window2.start();
        window3.start();
    }
}

class SellTickets4 extends Thread {
    private static int tickets = 100;

    @Override
    public void run() {
        while (hasTicket()) {
        }
    }

    // 同步监视器:SellTickets4.class
    private static synchronized boolean hasTicket() {
        if (tickets > 0) {
            System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + tickets);
            tickets--;
            return true;
        } else {
            return false;
        }
    }
}

方式三:Lock锁🔒(JDK 5.0之后)

使用方法:

  1. 创建锁:private static ReentrantLock lock = new ReentrantLock();
  2. 调用锁定方法:lock()
  3. 调用解锁方法:unlock()

使用时,使用try...finally...进行包裹,在try中进行上锁,在finally中进行解锁,以保证锁一定会被释放。

例:

import java.util.concurrent.locks.ReentrantLock;

public class SellTicketTest5 {
    public static void main(String[] args) {

        Thread window1 = new SellTickets5();
        Thread window2 = new SellTickets5();
        Thread window3 = new SellTickets5();

        window1.setName("Window 1");
        window2.setName("Window 2");
        window3.setName("Window 3");

        window1.start();
        window2.start();
        window3.start();
    }
}

class SellTickets5 extends Thread {
    private static int tickets = 100;
    // 1.创建lock
    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                // 2.调用锁定方法:lock()
                lock.lock();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + tickets);
                    tickets--;
                } else {
                    break;
                }
            } finally {
                // 3.调用解锁方法:unlock()
                lock.unlock();
            }
        }
    }
}

synchronized与Lock的异同

相同点:二者都可以解决线程安全问题

不同点:synchronized机制在执行完相应的同步代码后,自动释放同步监视器(锁);Lock需要手动启动同步(lock()),结束同步也需要手动实现(unlock())

优先使用顺序

Lock 同步代码块(已经进入了方法体,分配了相应资源) 同步方法(在方法体之外)

单例设计式模式之懒汉式(线程安全)

class Singleton {
    private static Singleton instance = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
    // 这一行提高了运行效率,在创建instance后,后续的线程可以直接获取instance,而不需要再进入同步代码块进行查看,也就无需等待其他线程是否再执行同步代码块
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

public class SingletonTest {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1 == s2);
    }
}

死锁的问题

死锁:不同的线程分别占用对方需要的同步资源不放弃,都在等待对方放弃自己需要的同步资源,就形成了线程的死锁。

说明:

  1. 出现死锁后,不会出现异常,不会出现提示,只是所有的线程都处于阻塞状态,无法继续
  2. 使用同步时,要避免出现死锁

解决方法

  1. 专门的算法、原则
  2. 尽量减少同步资源的定义
  3. 尽量避免嵌套同步

线程的通信

相关的三个方法

wait():令当前线程挂起并放弃CPU、同步资源并等待,使别的线程可访问并修改共享资源,而当前线程排队等候其他线程调用notify()或notifyAll()方法唤醒,唤醒后等待重新获得对监视器的所有权后才能继续执行。

notify():唤醒正在排队等待同步资源的线程中优先级最高者结束等待

notifyAll():唤醒正在排队等待资源的所有线程结束等待

说明:

  1. wait()、notify()、notifyAll()三个方法必须使用在synchronized代码块synchronized方法中,lock锁的方式不适用。
  2. wait()、notify()、notifyAll()三个方法的调用者必须是synchronized代码块synchronized方法中的同步监视器,否则会出现IllegalMonitorStateException异常。
  3. wait()、notify()、notifyAll()三个方法定义在java.lang.Object类中。因为这三个方法必须有锁对象调用,而任意对象都可以作为synchronized的同步锁,因此这三个方法只能在Object类中声明。

基本使用

例:

使用两个线程交替打印打印1-100。


public class ThreadCommunicationTest {
    public static void main(String[] args) {
        Number number = new Number();

        Thread t1 = new Thread(number);
        Thread t2 = new Thread(number);

        t1.setName("线程1");
        t2.setName("线程2");

        t1.start();
        t2.start();
    }
}

class Number implements Runnable {
    private int number = 1;
    private Object lock = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                lock.notify();
                if (number <= 100) {
                    System.out.println(Thread.currentThread().getName() + ":" + number);
                    number++;
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }
        }
    }
}

sleep()和wait()的异同

相同点:一旦执行方法,都可以使用当前的线程进入阻塞状态。

不同点:

1)两个方法声明的位置不同:Thread类中声明sleep(),Object类中声明wait()

2) 调用的要求不同:sleep()可以在任何需要的场景下调用,wait()必须在同步代码块或同步方法中由同步监视器调用

3) 关于是否释放同步监视器:如果两个方法都使用在同步代码块或同步方法中,sleep()不会释放锁,wait()会释放锁。

经典例题:生产者/消费者问题

题目:生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品,店员一次只能持有固定数量的产品(比如:20),如果生产者试图生产更多的产品,店员会叫生产者停一下,如果店中有空位放产品了再通知生产者继续生产;如果店中没有产品了,店员会告诉消费者等一下,如果店中有产品了再通知消费者来取走产品。

这里可能出现两个问题

/*
 * 经典例题:生产者/消费者问题
 * 生产者(Producer)将产品交给店员(Clerk),而消费者(Customer)从店员处
 * 取走产品,店员一次只能持有固定数量的产品(比如:20),如果生产者试图
 * 生产更多的产品,店员会叫生产者停一下,如果店中有空位放产品了再通
 * 知生产者继续生产;如果店中没有产品了,店员会告诉消费者等一下,如
 * 果店中有产品了再通知消费者来取走产品。
 */

public class ProducerConsumerTest {
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Producer producer = new Producer(clerk);
        Consumer consumer1 = new Consumer(clerk);
        Consumer consumer2 = new Consumer(clerk);
        
        producer.setName("生产者1");
        consumer1.setName("消费者1");
        consumer2.setName("消费者2");
        
        producer.start();
        consumer1.start();
        consumer2.start();
    }
}

// 店员,相当于共享数据
class Clerk {
    private int productCount = 0;

    public synchronized void produceProduct() {

        if (productCount < 20) {
            // start produce
            productCount++;
            System.out.println(Thread.currentThread().getName() + ":生产第" + productCount + "个产品");
            notify();
        } else {
            // stop produce
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public synchronized void consumeProduct() {
        if (productCount > 0) {
            // start consume
            System.out.println(Thread.currentThread().getName() + ":消费第" + productCount + "个产品");
            productCount--;
            notify();
        } else {
            // stop consume
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

// 生产者,线程1
class Producer extends Thread {
    private Clerk clerk;

    public Producer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("开始生产...");
        while (true) {
            try {
                Thread.sleep((int)(Math.random()*300));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.produceProduct();
        }
    }
}

// 消费者,线程2
class Consumer extends Thread {
    private Clerk clerk;

    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        System.out.println("消费产品...");
        while (true) {
            try {
                Thread.sleep((int)(Math.random()*1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.consumeProduct();
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读