zoukankan      html  css  js  c++  java
  • Java学习路线:day19 多线程2

    全部源码:https://github.com/name365/JavaSE-30Day

    第8章 多线程

    线程的生命周期

    • JDK中用Thread.State类定义了线程的几种状态

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

      • 新建:当一个Thread类或其子类的对象被声明并创建时,新生的线程对象处于新建状态
      • 就绪:处于新建状态的线程被start()后,将进入线程队列等待CPU时间片,此时它已具备了运行的条件,只是没分配到CPU资源
      • 运行:当就绪的线程被调度并获得CPU资源时,便进入运行状态,run()方法定义了线程的操作和功能
      • 阻塞:在某种特殊情况下,被人为挂起或执行输入输出操作时,让出CPU并临时中止自己的执行,进入阻塞状态
      • 死亡:线程完成了它的全部工作或线程被提前强制性地中止或出现异常导致结束
    • 线程的生命周期
      在这里插入图片描述

    线程的同步

    问题的提出

    多个线程执行的不确定性引起执行结果的不稳定

    多个线程对账本的共享,会造成操作的不完整性,会破坏数据。

    在这里插入图片描述

    • 例题——模拟火车站售票程序,开启三个窗口售票。
    class Windows1 implements Runnable{
    
        private int ticket = 100;
    
        @Override
        public void run() {
            while(true){
                if(ticket > 0){
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为: " + ticket);
                    ticket--;
                }else{
                    break;
                }
            }
        }
    }
    
    public class WindowsTest1 {
        public static void main(String[] args) {
            Windows1 w = new Windows1();
    
            Thread t1 = new Thread(w);
            Thread t2 = new Thread(w);
            Thread t3 = new Thread(w);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    
    • 理想状态

    在这里插入图片描述

    • 极端状态

    在这里插入图片描述

    同步代码块处理实现Runnable的线程安全问题

    /**
     *  例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式
     *  1.卖票过程中出现重票、错票 ---》出现了线程的安全问题
     *  2.问题出现的原因:当某个线程操作车票的过程中,尚未操作完成时,其他线程参与进来,也操作车票
     *  3.如何解决:当一个线程在操作ticket的时候,其他线程不能参与进来。直到线程a操作完ticket时,其他
     *            线程才可以操作ticket。这种情况即使线程a出现了阻塞,也不能被改变。
     *  4.在java中,我们通过同步机制,来解决线程的安全问题。
     *
     *  方式一:同步代码块
     *  synchronized(同步监视器){
     *      //需要被同步的代码
     *
     *  }
     *  说明:1.操作共享数据的代码,即为需要被同步的代码 --->不能包含代码多了,也不能包含代码少了。
     *       2.共享数据:多个线程共同操作的变量。比如:ticket就是共享数据
     *       3.同步监视器,俗称:锁。任何一个类的对象,都可以来充当锁。
     *          要求:多个线程必须要共用同一把锁。
     *
     *       补充:在实现Runnable接口创建多线程的方式中,我们可以考虑使用this充当同步监视器。
     *
     *  方式二:同步方法
     *      如果操作共享数据的代码完整的声明在一个方法中,我们不妨将此方法声明同步的
     *
     *  5.同步的方式,解决了线程的安全问题。---好处
     *    操作同步代码时,只能有一个线程参与,其他线程等待。相当于是一个单线程的过程,效率低。---局限性
     *
     * @author subei
     * @create 2020-05-07 18:52
     */
    
    class Windows1 implements Runnable{
    
        private int ticket = 100;
    //    Object obj = new Object();
    //    Dog dog = new Dog();
    
        @Override
        public void run() {
            while(true){
                synchronized (this) {//此时的this:唯一的windows1的对象 //方式二:synchronized (dog) {
                    if (ticket > 0) {
    
                        try{
                            Thread.sleep(100);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
    
                        System.out.println(Thread.currentThread().getName() + ":卖票,票号为: " + ticket);
                        ticket--;
                    } else {
                        break;
                    }
                }
            }
        }
    }
    
    public class WindowsTest1 {
        public static void main(String[] args) {
            Windows1 w = new Windows1();
    
            Thread t1 = new Thread(w);
            Thread t2 = new Thread(w);
            Thread t3 = new Thread(w);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    class Dog{
    
    }
    
    • 分析同步原理

    在这里插入图片描述

    同步代码块处理继承Thread类的线程安全问题

    /**
     * 使用同步代码块解决继承Thread类的方式的线程安全问题
     *
     * 例子:创建三个c窗口卖票,总票数为100张
     *
     *
     *
     * @author subei
     * @create 2020-05-08 11:32
     */
    class Windows extends Thread{
    
        private static int ticket = 100;
        private static Object obj = new Object();
    
        @Override
        public void run() {
            while(true){
                //正确的
    //            synchronized (obj) {
                synchronized (Windows.class){   //Class clazz = Windows.class
                //错误的,因为此时this表示的是t1,t2,t3三个对象
    //            synchronized (this) {
                    if (ticket > 0) {
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(getName() + ":卖票,票号为: " + ticket);
                        ticket--;
                    } else {
                        break;
                    }
                }
            }
        }
    }
    
    public class WindowsTest2 {
        public static void main(String[] args) {
            Windows t1 = new Windows();
            Windows t2 = new Windows();
            Windows t3 = new Windows();
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    

    同步方法处理实现Runnable的线程安全问题

    /**
     * 使用同步方法解决实现Runnable接口的线程安全问题
     *
     * 关于同步方法的总结:
     *  1. 同步方法仍然涉及到同步监视器,只是不需要我们显式的声明。
     *  2. 非静态的同步方法,同步监视器是:this
     *     静态的同步方法,同步监视器是:当前类本身
     *
     * @author subei
     * @create 2020-05-08 14:27
     */
    
    class Windows3 implements Runnable {
    
        private int ticket = 100;
    
        @Override
        public void run() {
            while (true) {
                show();
            }
        }
    
        public synchronized void show() { //同步监视器:this
    //        synchronized (this){
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":卖票,票号为: " + ticket);
                    ticket--;
                }
    //        }
        }
    }
    
    public class WindowsTest3 {
        public static void main(String[] args) {
            Windows3 w3 = new Windows3();
    
            Thread t1 = new Thread(w3);
            Thread t2 = new Thread(w3);
            Thread t3 = new Thread(w3);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    

    同步方法处理继承Thread类的线程安全问题

    /**
     * 使用同步方法处理继承Thread类的方式中的线程安全问题
     *
     * @author subei
     * @create 2020-05-08 14:42
     */
    
    class Windows4 extends Thread {
    
    
        private static int ticket = 100;
    
        @Override
        public void run() {
    
            while (true) {
    
                show();
            }
    
        }
        private static synchronized void show(){//同步监视器:Window4.class
            //private synchronized void show(){ //同步监视器:t1,t2,t3。此种解决方式是错误的
            if (ticket > 0) {
    
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
                ticket--;
            }
        }
    }
    
    
    public class WindowsTest4 {
        public static void main(String[] args) {
            Windows4 t1 = new Windows4();
            Windows4 t2 = new Windows4();
            Windows4 t3 = new Windows4();
    
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
    
        }
    }
    

    线程安全的单例模式之懒汉式

    /**
     * 使用同步机制将单例模式中的懒汉式改写为线程安全的
     *
     * @author subei
     * @create 2020-05-08 16:57
     */
    public class BankTest {
    }
    class Bank{
    
        private Bank(){}
    
        private static Bank instance = null;
    
        public static Bank getInstance(){
            //方式一:效率稍差
            //快捷键:Alt+Shift+Z
    //        synchronized (Bank.class) {
    //            if(instance == null){
    //                instance = new Bank();
    //            }
    //            return instance;
    //        }
    
            //方式二:效率较高
            if(instance == null) {
                synchronized (Bank.class) {
                    if (instance == null) {
                        instance = new Bank();
                    }
                }
            }
            return instance;
        }
    }
    

    死锁的问题

    • 例1
    /**
     * 演示线程的死锁
     *
     * 1.死锁的理解:不同的线程分别占用对方需要的同步资源不放弃,
     *       都在等待对方放弃自己需要的同步资源,就形成了线程的死锁
     * 2.说明:
     *      》出现死锁后,不会出现异常,不会出现提示,只是所有的线程都处于阻塞状态,无法继续
     *      》我们使用同步时,要避免出现死锁。
     *
     * @author subei
     * @create 2020-05-08 17:25
     */
    public class ThreadTest {
        public static void main(String[] args) {
    
            StringBuffer s1 = new StringBuffer();
            StringBuffer s2 = new StringBuffer();
    
            new Thread(){
                @Override
                public void run() {
    
                    synchronized (s1){
                        s1.append("a");
                        s2.append("1");
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        synchronized (s2){
                            s1.append("b");
                            s2.append("2");
    
                            System.out.println(s1);
                            System.out.println(s2);
                        }
                    }
                }
            }.start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (s2){
                        s1.append("c");
                        s2.append("3");
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        synchronized (s1){
                            s1.append("d");
                            s2.append("4");
    
                            System.out.println(s1);
                            System.out.println(s2);
                        }
                    }
                }
            }).start();
        }
    }
    
    • 例2
    class A {
    	public synchronized void foo(B b) {
    		System.out.println("当前线程名: " + Thread.currentThread().getName()
    				+ " 进入了A实例的foo方法"); // ①
    		try {
    			Thread.sleep(200);
    		} catch (InterruptedException ex) {
    			ex.printStackTrace();
    		}
    		System.out.println("当前线程名: " + Thread.currentThread().getName()
    				+ " 企图调用B实例的last方法"); // ③
    		b.last();
    	}
    
    	public synchronized void last() {
    		System.out.println("进入了A类的last方法内部");
    	}
    }
    
    class B {
    	public synchronized void bar(A a) {
    		System.out.println("当前线程名: " + Thread.currentThread().getName()
    				+ " 进入了B实例的bar方法"); // ②
    		try {
    			Thread.sleep(200);
    		} catch (InterruptedException ex) {
    			ex.printStackTrace();
    		}
    		System.out.println("当前线程名: " + Thread.currentThread().getName()
    				+ " 企图调用A实例的last方法"); // ④
    		a.last();
    	}
    
    	public synchronized void last() {
    		System.out.println("进入了B类的last方法内部");
    	}
    }
    
    public class DeadLock implements Runnable {
    	A a = new A();
    	B b = new B();
    
    	public void init() {
    		Thread.currentThread().setName("主线程");
    		// 调用a对象的foo方法
    		a.foo(b);
    		System.out.println("进入了主线程之后");
    	}
    
    	public void run() {
    		Thread.currentThread().setName("副线程");
    		// 调用b对象的bar方法
    		b.bar(a);
    		System.out.println("进入了副线程之后");
    	}
    
    	public static void main(String[] args) {
    		DeadLock dl = new DeadLock();
    		new Thread(dl).start();
    		dl.init();
    	}
    }
    

    Lock锁方式解决线程安全问题

    • java.util.concurrent.locks.Lock接口是控制多个线程对共享资源进行访问的工具。锁提供了对共享资源的独占访问,每次只能有一个线程对Lock对象加锁,线程开始访问共享资源之前应先获得Lock对象。
    • ReentrantLock 类实现了Lock ,它拥有与synchronized 相同的并发性和内存语义,在实现线程安全的控制中,比较常用的是ReentrantLock,可以显式加锁、释放锁。
    • 从JDK 5.0开始,Java提供了更强大的线程同步机制——通过显式定义同步锁对象来实现同步。同步锁使用Lock对象充当。
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * 解决线程安全问题的方式三:lock锁---》JDK5.0新增
     *
     * 注意:如果同步代码有异常,要将unlock()写入finally语句块
     *
     * 1. 面试题:synchronized 与 Lock的异同?
     *    相同:二者都可以解决线程安全问题
     *    不同:synchronized机制在执行完相应的同步代码以后,自动的释放同步监视器
     *         Lock需要手动的启动同步(lock()),同时结束同步也需要手动的实现(unlock())
     *
     * 2.优先使用顺序:
     *      Lock 同步代码块(已经进入了方法体,分配了相应资源)同步方法(在方法体之外)
     *
     * 面试题:如何解决线程安全问题?有几种方式
     *
     * @author subei
     * @create 2020-05-08 17:49
     */
    
    class Windows implements Runnable{
    
        private int ticket = 100;
        //1.实例化ReentrantLock
        private ReentrantLock lock = new ReentrantLock();
    
    
        @Override
        public void run() {
            while(true){
                try{
    
                    //调用锁定方法:lock()
                    lock.lock();
    
                    if(ticket > 0){
    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(Thread.currentThread().getName() + ":售票,票号为: " + ticket);
                        ticket --;
                    }else{
                        break;
                    }
                }finally {
                    //3.调用解锁方法:unlock()
                    lock.unlock();
                }
            }
        }
    }
    
    public class LockTest {
        public static void main(String[] args) {
            Windows w = new Windows();
    
            Thread t1 = new Thread(w);
            Thread t2 = new Thread(w);
            Thread t3 = new Thread(w);
    
            t1.setName("窗口1");
            t2.setName("窗口2");
            t3.setName("窗口3");
    
            t1.start();
            t2.start();
            t3.start();
        }
    }
    
    • 练习
    /**
     * 银行有一个账户。
     * 有两个储户分别向同一个账户存3000元,每次存1000,存3次。
     * 每次存完打印账户余额。
     *
     * 分析:
     *      1.是否是多线程问题?是,两个储户线程
     *      2.是否有共享数据?有,账户(或账户余额)
     *      3.是否有线程安全问题?有
     *      4.需要考虑如何解决线程安全问题?同步机制:有三种方式。
     *
     * @author subei
     * @create 2020-05-08 18:23
     */
    class Account{
        private double balance;
    
        public Account(double balance){
            this.balance = balance;
        }
    
        //存钱
        public synchronized void deposit(double amt){
            if(amt > 0){
    
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                balance += amt;
                System.out.println(Thread.currentThread().getName() + ":" + "存钱成功,当前余额:" + balance);
            }
        }
    }
    
    class Customer extends Thread{
    
        private Account acct;
        public Customer(Account acct){
            this.acct = acct;
        }
    
        @Override
        public void run() {
    
            for(int i = 0;i < 3;i++){
                acct.deposit(1000);
            }
        }
    }
    
    public class AccountTest {
        public static void main(String[] args) {
            Account acct = new Account(0);
            Customer c1 = new Customer(acct);
            Customer c2 = new Customer(acct);
    
            c1.setName("甲");
            c2.setName("乙");
    
            c1.start();
            c2.start();
        }
    }
    

    线程的通信

    /**
     * 线程通信的例子:使用两个线程打印1-100。线程1, 线程2 交替打印
     *
     * 涉及到的三个方法:
     * wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器。
     * notify():一旦执行此方法,就会唤醒被wait的一个线程。如果有多个线程被wait,就唤醒优先级高的那个。
     * notifyAll():一旦执行此方法,就会唤醒所有被wait的线程。
     *
     * 说明:
     *      1.wait(),notify(),notifyAll()三个方法必须使用在同步代码块或同步方法中。
     *      2.wait(),notify(),notifyAll()三个方法的调用者必须是同步代码块或同步方法中的同步监视器。
     *         否则,会出现IllegalMonitorStateException异常
     *      3.wait(),notify(),notifyAll()三个方法是定义在java.lang.Object类中。
     *
     * @author subei
     * @create 2020-05-08 18:50
     */
    
    class Number implements Runnable{
    
        private int number = 1;
        public Object obj = new Object();
    
        @Override
        public void run() {
    
            while (true){
                synchronized (obj) {
    
                    obj.notify();
    
                    if(number <= 100){
    
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        System.out.println(Thread.currentThread().getName() + ":" + number);
                        number++;
    
                        try {
                            //使得调用如下wait()方法的线程进入阻塞状态
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                    }else{
                        break;
                    }
                }
            }
        }
    }
    
    public class CommunicationTest {
        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();
        }
    }
    

    sleep()和wait()的异同

     /**
     * 面试题:sleep() 和 wait()的异同?
     * 1.相同点:一旦执行方法,都可以使得当前的线程进入阻塞状态。
     * 2.不同点:1)两个方法声明的位置不同:Thread类中声明sleep() , Object类中声明wait()
     *          2)调用的要求不同:sleep()可以在任何需要的场景下调用。 wait()必须使用在同步代码块或同步方法中
     *          3)关于是否释放同步监视器:如果两个方法都使用在同步代码块或同步方法中,sleep()不会释放锁,wait()会释放锁。
     *
     * @author shkstart
     * @create 2019-02-15 下午 4:21
     */
    

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

    在这里插入图片描述

    /**
     * 线程通信的应用:经典例题:生产者/消费者问题
     *
     * 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品,
     * 店员一次只能持有固定数量的产品(比如:20),如果生产者试图生产更多的产品,
     * 店员会叫生产者停一下,如果店中有空位放产品了再通知生产者继续生产;
     * 如果店中没有产品了,店员会告诉消费者等一下,
     * 如果店中有产品了再通知消费者来取走产品。
     *
     * 分析:
     *      1.是否是多线程的问题?是,生产者的线程,消费者的线程
     *      2.是否有共享数据的问题?是,店员、产品、产品数
     *      3.如何解决线程的安全问题?同步机制,有三种方法
     *      4.是否涉及线程的通信?是
     *
     * @author subei
     * @create 2020-05-08 19:23
     */
    
    class Clerk{
    
        private int productCount = 0;
    
        //生产产品
        public synchronized void produceProduct() {
    
            if(productCount < 20){
                productCount++;
                System.out.println(Thread.currentThread().getName() + ": 开始生产第" + productCount + "个产品");
    
                notify();
            }else{
                //等待
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        //消费产品
        public synchronized void consumeProduct() {
    
            if(productCount > 0){
                System.out.println(Thread.currentThread().getName() + ":开始消费第" + productCount + "个产品");
                productCount--;
    
                notify();
            }else{
                //等待
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    
    class Producer extends Thread{//生产者
        private Clerk clerk;
    
        public Producer(Clerk clerk){
            this.clerk = clerk;
        }
    
        @Override
        public void run() {
            System.out.println(getName() + ": 开始生产产品......");
    
            while(true){
    
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                clerk.produceProduct();
            }
        }
    }
    
    class Consumer extends Thread{  //消费者
        private Clerk clerk;
    
        public Consumer(Clerk clerk){
            this.clerk = clerk;
        }
    
        @Override
        public void run() {
            System.out.println(getName() + ": 开始消费产品......");
    
            while(true){
    
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                clerk.consumeProduct();
            }
    
        }
    }
    
    public class ProductTest {
        public static void main(String[] args) {
            Clerk clerk = new Clerk();
    
            Producer p1 = new Producer(clerk);
            p1.setName("生产者1");
    
            Consumer c1 = new Consumer(clerk);
            c1.setName("消费者1");
            Consumer c2 = new Consumer(clerk);
            c2.setName("消费者2");
    
            p1.start();
            c1.start();
            c2.start();
        }
    }				
    

    JDK5.0新增线程创建方式

    创建多线程的方式三:实现Callable接口

    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    /**
     * 创建多线程的方式三:实现Callable接口 ---> JDK 5.0新增
     *
     * 如何理解实现Callable接口的方式创建多线程比实现Runnable接口创建多线程方式强大?
     *      1.call()可以有返回值的。
     *      2.call()可以抛出异常,被外面的操作捕获,获取异常的信息
     *      3.Callable是支持泛型的
     *      4.需要借助FutureTask类,比如获取返回结果
     *
     * @author subei
     * @create 2020-05-08 20:31
     */
    //1.创建一个实现Callable的实现类
    class NumThread implements Callable{
    
        //2.实现call方法,将此线程需要执行的操作声明在call()中
        @Override
        public Object call() throws Exception {
            int sum = 0;
            for(int i = 1;i <= 100;i++){
                if(i % 2 == 0){
                    System.out.println(i);
                    sum += i;
                }
            }
            return sum;
        }
    }
    
    public class ThreadNew {
        public static void main(String[] args) {
            //3.创建Callable接口实现类的对象
            NumThread numThread = new NumThread();
    
            //4.将此Callable接口实现类的对象作为传递到FutureTask构造器中,创建FutureTask的对象
            FutureTask futureTask = new FutureTask(numThread);
    
            //5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()
            new Thread(futureTask).start();
    
            try {
                //6.获取Callable中call方法的返回值
                //get()返回值即为FutureTask构造器参数Callable实现类重写的call()的返回值。
                Object sum = futureTask.get();
                System.out.println("总和为:" + sum);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
    
    • Future接口
      • 可以对具体Runnable、Callable任务的执行结果进行取消、查询是否完成、获取结果等。
      • FutrueTask是Futrue接口的唯一的实现类
      • FutureTask 同时实现了Runnable, Future接口。它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值

    使用线程池的好处

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

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

    好处

    • 提高响应速度(减少了创建新线程的时间)
    • 降低资源消耗(重复利用线程池中线程,不需要每次都创建)
    • 便于线程管理
      • corePoolSize:核心池的大小
      • maximumPoolSize:最大线程数
      • keepAliveTime:线程没有任务时最多保持多长时间后会终止

    创建多线程的方式四:使用线程池

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ThreadPoolExecutor;
    
    /**
     * 创建多线程的方式四:使用线程池
     *
     * 好处:
     *      1.提高响应速度(减少了创建新线程的时间)
     *      2.降低资源消耗(重复利用线程池中线程,不需要每次都创建)
     *      3.便于线程管理
     *          corePoolSize:核心池的大小
     *          maximumPoolSize:最大线程数
     *          keepAliveTime:线程没有任务时最多保持多长时间后会终止
     *
     * 面试题:创建多线程有几种方式?四种!
     *
     * @author subei
     * @create 2020-05-08 21:05
     */
    
    class NumberThread implements Runnable{
        @Override
        public void run() {
            for(int i = 0;i <= 100;i++){
                if(i % 2 == 0){
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }
    }
    
    class NumberThread1 implements Runnable{
        @Override
        public void run() {
            for(int i = 0;i <= 100;i++){
                if(i % 2 != 0){
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }
    }
    
    public class ThreadPool {
        public static void main(String[] args) {
    
            //1. 提供指定线程数量的线程池
            ExecutorService service = Executors.newFixedThreadPool(10);
            ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
            //设置线程池的属性
    //        System.out.println(service.getClass());
    //        service1.setCorePoolSize(15);
    //        service1.setKeepAliveTime();
    
            //2.执行指定的线程的操作。需要提供实现Runnable接口或Callable接口实现类的对象
            service.execute(new NumberThread());  //适合适用于Runable
            service.execute(new NumberThread1());  //适合适用于Runable
    
    //        service.submit(Callable callable);   //适合适用于Callable
    
            //3.关闭连接池
            service.shutdown();
        }
    }
    
    • 线程池相关API

      • JDK 5.0起提供了线程池相关API:ExecutorServiceExecutors
    • ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor

      • void execute(Runnable command) :执行任务/命令,没有返回值,一般用来执行Runnable
      • Future submit(Callable task):执行任务,有返回值,一般又来执行Callable
      • void shutdown() :关闭连接池
    • Executors:工具类、线程池的工厂类,用于创建并返回不同类型的线程池

      • Executors.newCachedThreadPool():创建一个可根据需要创建新线程的线程池
      • Executors.newFixedThreadPool(n); 创建一个可重用固定线程数的线程池
      • Executors.newSingleThreadExecutor() :创建一个只有一个线程的线程池
      • Executors.newScheduledThreadPool(n):创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。

      整个Java全栈系列都是笔者自己敲的笔记。写作不易,如果可以,点个赞呗!✌

    欢迎查阅
  • 相关阅读:
    js截取字符串区分汉字字母代码
    List 去处自定义重复对象方法
    63. Unique Paths II
    62. Unique Paths
    388. Longest Absolute File Path
    41. First Missing Positive
    140. Word Break II
    139. Word Break
    239. Sliding Window Maximum
    5. Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/gh110/p/12904623.html
Copyright © 2011-2022 走看看