第四章 Java多线程

Java多线程

1.线程与进程

  • 进程:
    • 是指一个内存中运行的应用程序,每个进程都有一个独立的内存空间
  • 线程:
    • 是进程中的一个执行路径,共享一个内存空间,线程之间可以自由切换,并发执行一个进程最少有一个线程
    • 线程实际上是在进程基础之上的进一步划分,一个进程启动之后,里面的若干执行路径又可以划分成若干个线程

1.1 继承Thread类

主线程的for循环打印“朝辞白帝彩云间”,线程m会打印“千里江陵一日还”,两者交替

package chapter4.section5;
/*
实现Runnable与继承Thread相比有如下优势
1.通过创建任务,然后给线程分配任务的方式实现多线程,更适合多个线程同时执行任务的情况
2.可以避免单继承所带来的局限性
3.任务与线程是分离的,提高了程序的健壮性
4.后期学习的线程池技术,接受Runnable类型的任务,不接受Thread类型的线程
**/
public class Demo1 {
    public static void main(String[] args) {

        MyThread m = new MyThread();
        m.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("朝辞白帝彩云间"+i);
        }
    }
}


public class MyRunnable implements Runnable{
    @Override
    public void run() {
        //线程的任务
        for (int i = 0; i < 10; i++) {
            System.out.println("千里江陵一日还"+i);
        }
    }
}

1.2 实现Runnable接口

实现换为Runnable接口,与继承Thread类相同

package chapter4.section5;

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

        //实现runnable
        //1 创建一个任务对象
        MyRunnable r = new MyRunnable();
        //创建一个线程并给他一个任务
        Thread t = new Thread(r);
        //启动线程
        t.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("千里江陵一日还"+i);
        }
    }
}

1.3 正常写法与lambda写法

package chapter4.section5;

public class Demo2 {
    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("12345"+i);
                }
            }
        }.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("离离原上草"+i);
        }
    }
}

-------------------------------------------------
package chapter4.section5;

public class Demo2 {
    public static void main(String[] args) {
        (Thread) run() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("12345"+i);
            }
        }.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("离离原上草"+i);
        }
    }
}

另一个例子:

package chapter4.section5;

public class Demo17 {
    /*
    lambda表达式
    函数式编程思想
    **/
    public static void main(String[] args) {
        //冗余的Runnable编写方式
       /* Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("应怜屐齿映苍苔");
            }
        });
        t.start();*/

        Thread t = new Thread(() -> System.out.println("应怜屐齿映苍苔"));
        t.start();
    }
}

1.4 获取线程名称

Thread.currentThread().getName()

设置线程名称:不设置线程名称,就会有系统设置默认的名字,形如:Thread-123,后面跟的是线程序号数字

  • setName
Thread t = new Thread(new MyRunnable());
t.setName("wwww");
t.start();
  • 构造方法命名
new Thread(new MyRunnable(),"小扣柴扉久不开").start();

1.5 线程休眠

Thread.sleep(1000); 

1.6 线程中断

主线程先循环到5,调用t1.interrupt通知t1线程该结束了

package chapter4.section5;

public class Demo5 {
    public static void main(String[] args) {
        //线程中断
        //y一个线程是一个独立的执行路径,它是否结束应该由其自身决定
        Thread t1 = new Thread(new MyRunnable());
        t1.start();
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //给线程t1添加中断标记
        t1.interrupt();
    }

    static class MyRunnable implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //e.printStackTrace();
                    System.out.println("发现了中断标记,线程自杀");
                    return;
                }
            }
        }
    }
}

1.7 线程分用户线程和守护线程

  • 用户线程:平时用到的普通线程均是用户线程,当一个进程不包含任何的存活的用户线程时,进行结束。
  • ​​​​守护线程:指在程序运行的时候在后台提供一种通用服务的线程,守护线程是为用户线程服务的,当有用户线程在运行,那么守护线程同样需要工作。是守护用户线程的,当最后一个用户线程结束时,所有守护线程自动死亡。设置守护线程:t1.setDaemon(true);。典型的守护线程如垃圾回收线程。

下面的例子中t1被设置为守护线程,它要等到main主线程退出后才退出

package chapter4.section5;


public class Demo6 {
    public static void main(String[] args) {
        //线程分为守护线程和用户线程
        //用户线程:当一个进程不包含任何的存活的用户线程时,进行结束
        //守护线程:守护用户线程的,当最后一个用户线程结束时,所有守护线程自动死亡。
        Thread t1 = new Thread(new MyRunnable());
        //设置守护线程
        t1.setDaemon(true);
        t1.start();
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class MyRunnable implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

1.8 线程不安全

由于没有控制好对共享对象的互斥访问,导致多线程运行的时候产生和预期结果不符的情况就是线程不安全问题。

下面的例子是使用3个线程模拟卖票,三个线程同时在while循环里操作count,导致count有时候结果为负数,所谓的超卖现象发生。

package chapter4.section5;

public class Demo7 {
    public static void main(String[] args) {
        //线程不安全
        Runnable run = new Ticket();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
    }

    static class Ticket implements Runnable {
        //总票数
        private int count = 10;

        @Override
        public void run() {
            while (count > 0) {
                //卖票
                System.out.println("正在准备卖票");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count--;
                System.out.println("卖票结束,余票:" + count);
            }
        }
    }
}

1.8.1 隐式锁-Sychronized同步代码块

使用sychronized关键字锁住一个对象或一段代码:

package chapter4.section5;


public class Demo8 {
    public static void main(String[] args) {
        Object o = new Object();
        //线程不安全
        //解决方案1  同步代码块
        //格式:synchronized(锁对象){
        //
        //
        //      }
        Runnable run = new Ticket();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
    }

    static class Ticket implements Runnable {
        //总票数
        private int count = 10;
        private final Object o = new Object();

        @Override
        public void run() {
            //Object o = new Object();    //这里不是同一把锁,所以锁不住
            while (true) {
                synchronized (o) {
                    if (count > 0) {
                        //卖票
                        System.out.println("正在准备卖票");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        count--;
                        System.out.println(Thread.currentThread().getName() + "卖票结束,余票:" + count);
                    } else {
                        break;
                    }
                }
            }
        }
    }
}

1.8.2 隐式锁-Sychronized同步方法

使用Sychronized锁住一个方法:

package chapter4.section5;

public class Demo9 {
    public static void main(String[] args) {
        Object o = new Object();
        //线程不安全
        //解决方案2  同步方法
        Runnable run = new Ticket();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
    }

    static class Ticket implements Runnable {
        //总票数
        private int count = 10;

        @Override
        public void run() {
            while (true) {
                boolean flag = sale();
                if (!flag) {
                    break;
                }
            }
        }

        public synchronized boolean sale() {
            if (count > 0) {
                //卖票
                System.out.println("正在准备卖票");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count--;
                System.out.println(Thread.currentThread().getName() + "卖票结束,余票:" + count);
                return true;
            }
            return false;
        }
    }
}

1.8.3 显示锁-lock

公平锁:每个线程获得CPU执行的机会是均等的

不公平锁:每个线程都尽量抢占获取CPU执行的机会

package chapter4.section5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//同步代码块和同步方法都属于隐式锁
//线程同步lock

public class Demo10 {
    public static void main(String[] args) {
        Object o = new Object();
        //线程不安全
        //解决方案1   显示锁  Lock  子类 ReentrantLock

        Runnable run = new Ticket();
        new Thread(run).start();
        new Thread(run).start();
        new Thread(run).start();
    }

    static class Ticket implements Runnable {
        //总票数
        private int count = 10;
        //参数为true表示公平锁    默认是false 不是公平锁
        private final Lock l = new ReentrantLock(true);

        @Override
        public void run() {
            while (true) {
                l.lock();
                if (count > 0) {
                    //卖票
                    System.out.println("正在准备卖票");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count--;
                    System.out.println(Thread.currentThread().getName() + "卖票结束,余票:" + count);
                } else {
                    break;
                }
                l.unlock();
            }
        }
    }
}

1.9 线程死锁

package chapter4.section5;

public class Demo11 {
    public static void main(String[] args) {
        //线程死锁
        Culprit c = new Culprit();
        Police p = new Police();
        new MyThread(c, p).start();
        c.say(p);
    }

    static class MyThread extends Thread {
        private final Culprit c;
        private final Police p;

        MyThread(Culprit c, Police p) {
            this.c = c;
            this.p = p;
        }

        @Override
        public void run() {
            p.say(c);
        }
    }

    static class Culprit {
        public synchronized void say(Police p) {
            System.out.println("罪犯:你放了我,我放了人质");
            p.fun();
        }

        public synchronized void fun() {
            System.out.println("罪犯被放了,罪犯也放了人质");
        }
    }

    static class Police {
        public synchronized void say(Culprit c) {
            System.out.println("警察:你放了人质,我放了你");
            c.fun();
        }

        public synchronized void fun() {
            System.out.println("警察救了人质,但是罪犯跑了");
        }
    }
}

死锁结果:程序运行到如下情况不能继续

Connected to the target VM, address: '127.0.0.1:54129', transport: 'socket'
罪犯:你放了我,我放了人质
警察:你放了人质,我放了你

1.10 多线程通信

notify():唤醒一个在此对象的监视器上等待的线程

notifyAll():唤醒所有在此对象的监视器上等待的线程

wait():导致当前线程等待,直到另一个线程为这个对象调用notify()方法或notifyAll()方法

wait(long timeout): 导致当前线程等待,直到另一个线程调用该对象的notify()方法或notifyAll()方法,或者指定的时间已经过去

wait(long timeout, int nanos):导致当前线程等待,直到另一个线程调用这个对象的notify()方法或notifyAll()方法,或者其他一些线程中断当前线程,或者已经经过了一定的实时时间

package chapter4.section5.producerconsumer;

public class Demo4 {

    /**
     * 多线程通信问题, 生产者与消费者问题
     *
     * @param args
     */
    public static void main(String[] args) {
        Food f = new Food();
        new Cook(f).start();
        new Waiter(f).start();
    }

    //厨师
    static class Cook extends Thread {
        private final Food f;

        public Cook(Food f) {
            this.f = f;
        }

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                if (i % 2 == 0) {
                    f.setNameAndTaste("老干妈小米粥", "香辣味");
                } else {
                    f.setNameAndTaste("煎饼果子", "甜辣味");
                }
            }
        }
    }

    //服务生
    static class Waiter extends Thread {
        private final Food f;

        public Waiter(Food f) {
            this.f = f;
        }

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                f.get();
            }
        }
    }

    //食物
    static class Food {
        private String name;
        private String taste;

        //true 表示可以生产
        private boolean flag = true;

        public synchronized void setNameAndTaste(String name, String taste) {
            if (flag) {
                this.name = name;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.taste = taste;
                flag = false;
                this.notifyAll();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public synchronized void get() {
            if (!flag) {
                System.out.println("服务员端走的菜的名称是:" + name + ",味道:" + taste);
                flag = true;
                this.notifyAll();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2.线程调度

  • 分时调度:
    • 所有线程轮流使用 CPU 的使用权,平均分配每个线程占用 CPU 的时间。
  • 抢占式调度:
    • 优先让优先级高的线程使用 CPU,如果线程的优先级相同,那么会随机选择一个(线程随机性), Java使用的为 抢占式调度。
    • CPU使用抢占式调度模式在多个线程间进行着高速的切换。对于CPU的一个核新而言,某个时刻, 只能执行一个线程,而 CPU的在多个线程间切换速度相对我们的感觉要快,看上去就是 在同一时 刻运行。 其实,多线程程序并不能提高程序的运行速度,但能够提高程序运行效率,让CPU的使 用率更高。

3.同步与异步

  • 同步:排队执行 , 效率低但是安全
  • 异步:同时执行 , 效率高但是数据不安全

4.并发与并行

  • 并发:指两个或多个事件在同一个时间段内发生
  • 并行:指两个或多个事件在同一时刻发生(同时发生)

5.线程的状态

操作系统层面的多线程状态转换:

Java多线程状态转换:

  • NEW
    A thread that has not yet started is in this state.
  • RUNNABLE
    A thread executing in the Java virtual machine is in this state.
  • BLOCKED
    A thread that is blocked waiting for a monitor lock is in this state.
  • WAITING
    A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  • TIMED_WAITING
    A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
  • TERMINATED
    A thread that has exited is in this state.

需要注意的是:操作系统中的线程除去new和terminated状态,一个线程真实存在的状态是ready,running和waiting

6.线程池 Executors

如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程
就会大大降低 系统的效率,因为频繁创建线程和销毁线程需要时间. 线程池就是一个容纳多个线程的容
器,池中的线程可以反复使用,省去了频繁创建线程对象的操作,节省了大量的时间和资源。

6.1 线程池的好处

  • 降低资源消耗
  • 提高响应速度
  • 提高线程的可管理性

6.2 Java中的四种线程池 ExecutorService

1) 缓存线程池

/**
* 缓存线程池.
* (长度无限制)
* 执行流程:
* 1. 判断线程池是否存在空闲线程
* 2. 存在则使用
* 3. 不存在,则创建线程 并放入线程池, 然后使用
*/
ExecutorService service = Executors.newCachedThreadPool();
//向线程池中 加入 新的任务
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});

2) 定长线程池

/**
* 定长线程池.
* (长度是指定的数值)
* 执行流程:
* 1. 判断线程池是否存在空闲线程
* 2. 存在则使用
* 3. 不存在空闲线程,且线程池未满的情况下,则创建线程 并放入线程池, 然后使用
* 4. 不存在空闲线程,且线程池已满的情况下,则等待线程池存在空闲线程
*/
ExecutorService service = Executors.newFixedThreadPool(2);
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});

3) 单线程线程池

效果与定长线程池创建时传入数值1效果一致.
/**
* 单线程线程池.
* 执行流程:
* 1. 判断线程池 的那个线程 是否空闲
* 2. 空闲则使用
* 4. 不空闲,则等待 池中的单个线程空闲后 使用
*/
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});
service.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("线程的名称:"+Thread.currentThread().getName());
    }
});

4) 周期性任务定长线程池

public static void main(String[] args) {
    /**
    * 周期任务 定长线程池.
    * 执行流程:
    * 1. 判断线程池是否存在空闲线程
    * 2. 存在则使用
    * 3. 不存在空闲线程,且线程池未满的情况下,则创建线程 并放入线程池, 然后使用
    * 4. 不存在空闲线程,且线程池已满的情况下,则等待线程池存在空闲线程
    *
    * 周期性任务执行时:
    * 定时执行, 当某个时机触发时, 自动执行某任务 .*/
    ScheduledExecutorService service = Executors.newScheduledThreadPool(2);

    /**
    * 定时执行
    * 参数1. runnable类型的任务
    * 参数2. 时长数字
    * 参数3. 时长数字的单位
    */
    /*service.schedule(new Runnable() {
        @Override
        public void run() {
            System.out.println("俩人相视一笑~ 嘿嘿嘿");
        }
    },5,TimeUnit.SECONDS);
    */

    /**
    * 周期执行
    * 参数1. runnable类型的任务
    * 参数2. 时长数字(延迟执行的时长)
    * 参数3. 周期时长(每次执行的间隔时间)
    * 参数4. 时长数字的单位
    */
    service.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            System.out.println("俩人相视一笑~ 嘿嘿嘿");
        }
    },5,2,TimeUnit.SECONDS);
}

7.Runnable与Callable

接口定义
//Callable接口
public interface Callable<V> {
    V call() throws Exception;
}

//Runnable接口
public interface Runnable {
    public abstract void run();
}

7.1 Callable使用步骤

1. 编写类实现Callable接口 , 实现call方法
class XXX implements Callable<T> {
    @Override
    public <T> call() throws Exception {
        return T;
    }
}

2. 创建FutureTask对象 , 并传入第一步编写的Callable类对象
FutureTask<Integer> future = new FutureTask<>(callable);

3. 通过Thread,启动线程
new Thread(future).start();

7.2 Runnable 与 Callable的相同点

  • 都是接口
  • 都可以编写多线程程序
  • 都采用Thread.start()启动线程

7.3 Runnable 与 Callable的不同点

  • Runnable没有返回值;Callable可以返回执行结果
  • Callable接口的call()允许抛出异常;Runnable的run()不能抛出

7.4 Callable获取返回值

Callalble接口支持返回执行结果,需要调用FutureTask.get()得到,此方法会阻塞主进程的继续往下执行,如果不调用不会阻塞。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值