Java并发编程(一)线程基础概念

导图
Java线程基础

一、基础概念

1、进程和线程

进程:是系统进行资源分配和调度的最小单位,是操作系统结构的基础。
线程:是程序执行的最小单位。
一个进程可以包含多个线程。

2、并发和并行

并发:某个时间段内,多任务交替执行的能力。
并行:并行是指同时执行多任务的能力。
关键区别在于,同一时间内,并发是交替执行,并行是同时执行。
此外,并发程序之间有相互制约的关系;并发程序的执行过程是断断续续的(程序需要记忆指令执行点,并且CPU需要不断调度);在并发数量设置合理的前提下,并发编程可以一定程度上提高程序运行的效率。

3、为什么要使用多线程?

Java天生就是多线程的,main方法开始执行(mainThread),以及其他监控系统状态的譬如内存、GC相关等线程存在。
高并发可以带来很多好处
充分利用CPU资源:现在处理的硬件以及计算能力越来越强,使用多线程可以尽可能多发挥多核心的优势,减少CPU的调度等待时间。
加快程序的响应时间
可以使代码模块化、异步化、简单化
但是多线程也会引发一些线程安全的问题,可以参考
Java并发编程(二)线程安全

4、线程的生命周期

线程的六种状态(Thread类中State枚举给定的是六种状态)
NEW:新建状态,是线程被创建但未被启动的状态。创建线程的方式可以参考第二部分。
RUNNABLE:RUNNABLE状态,在调用start之后运行之前的状态。
BLOCKED:被动进入阻塞;锁被其他线程占用;
WAITING:进入等待状态,主动wait/join/park方法进入无限等待,知道notify或者notifyAll,unPark所唤醒;
TIMED_WAITING:与WAITING状态类似,给定等待时间的wait/join/park等方法,以及sleep方法;
TERMINATED:DEAD状态,线程run函数执行完成或者因异常退出后的状态,此状态不可逆转。
参考码出高效:Java开发手册中的内容

二、线程开启方式

1、直接继承Thread类

public static void main(String[] args) {
        new MyThread().start();
    }
    private static class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println("This is Thread : " + this.getName());
        }
    }

2、实现Runnable接口

public static void main(String[] args) {
        new Thread(new MyRunnable()).start();
    }
    private static class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println("This is MyRunnable : " + Thread.currentThread().getName());
        }
    }

3、实现Callable接口

public class MyClass {

    public static void main(String[] args) {
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask).start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("after futureTask.get");
    }

    private static class MyCallable implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                Thread.currentThread().sleep(500);
            }
            return 123;
        }
    }
}
输出
0
1
2
3
4
5
6
7
8
9
123
after futureTask.get

Callable相比于Runnable的区别在于Callable允许有返回值;在使用时需要将Callable当做参数传递给FutureTask,并通过new Thread开启线程。
FutureTask实现了RunnableFuture,RunnableFuture extends Runnable接口,所以可以直接通过new Thread开启。

Thread类本身也实现了Runnable接口,在Thread的run方法中调用了传入的target(Runnable)的run方法

	Thread的run方法
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    

FutureTask接收Callback返回值的原理:
由于Thread类的run方法实际调用的就是传入的Runnable实现类的run方法,因此这里看一下FutureTask的run方法

	//==>分析一,FutureTask的run方法
    public void run() {
        if (state != NEW ||
            !U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
            return;
        try {
        	//拿到callable接口
            Callable<V> c = callable;
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                	//调用call方法,这样,我们自己实现的Callable中的代码被执行
                	//result是call方法的返回值
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex);
                }
                if (ran)
                	// ==>见分析二
                    set(result);
            }
        } finally {
            // runner must be non-null until state is settled to
            // prevent concurrent calls to run()
            runner = null;
            // state must be re-read after nulling runner to prevent
            // leaked interrupts
            int s = state;
            if (s >= INTERRUPTING)
                handlePossibleCancellationInterrupt(s);
        }
    }
	//==>分析二 set(V v)方法
	//该方法,将Callable的返回值传递给outcome并通过CAS更新STATE状态
	protected void set(V v) {
        if (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
            outcome = v;
            U.putOrderedInt(this, STATE, NORMAL); // final state
            finishCompletion();
        }
    }
	
	//==>分析三  futureTask.get()是如何获取值的
	//get方法会进入awaitDone方法当中,阻塞等待当前的STATE变更为结束
	public V get() throws InterruptedException, ExecutionException {
        int s = state;
        if (s <= COMPLETING)
        	//阻塞的等待当前状态大于1(1为COMPLETING状态)
            s = awaitDone(false, 0L);
        // ==> 见分析四
        return report(s);
    }
	
	// ==>分析四 report方法
	private V report(int s) throws ExecutionException {
        Object x = outcome;
        if (s == NORMAL)
        	//将x也就是前面Callable的call方法返回值,返回
            return (V)x;
        if (s >= CANCELLED)
            throw new CancellationException();
        throw new ExecutionException((Throwable)x);
    }
    

三种线程启动方式,第三种带返回值的一般看需求吧,赶脚平时用的比较少~ 前两种常用的方式当中,推荐使用第二种,也就是自定义类实现Runnable接口。
因为继承Thread类的方式不符合里氏替换原则,而实现Runnable接口可以使编程更加灵活
里氏替换原则:Java设计模式六大原则之一;指子类可以扩展父类的功能,但不能修改父类原有的功能;当继承Thread类时,必然要重写run方法,这样就破坏了Thread原有的run方法,因此不符合里氏替换原则。

三、线程终止方式

1、过时的suspend、stop等方法

suspend会使线程进入睡眠状态,但过时的原因在于,suspend是占着资源睡眠,不会释放已经占用的资源(比如锁),容易引发死锁,所以不建议使用;

stop方法在终结一个线程时不会保证线程资源的正常释放,可能会使程序处于一个不确定的工作状态,因此也不建议使用。

2、中断

interrupt函数:Thread的成员方法,通过设置中断标志位告诉JVM当前线程需要中断了,不过需要注意的是,调用interrupt后,线程可能不会立刻停止,而是等待资源释放完成后,检查中断标志位为true后,停止。
Thread.interrupted静态方法:Thread的静态方法,检测线程是否被中断,但是会同时将中断标志位改为false。
isInterrupted函数:Thread的成员函数,用来检测中断标志位。

3、如何安全的结束一个线程

private static class TestRunnable implements Runnable {
        @Override
        public void run() {
            while (!isStop || !Thread.currentThread().isInterrupted()) {
                System.out.println("Thread getName : " + Thread.currentThread().getName()
                        + ", thread.isInterrupted : " + Thread.currentThread().isInterrupted());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    //sleep会响应中断,并将中断标志位复位,因此isInterrupted会恢复成false
                    isStop = true;
                    System.out.println("interruptedException isStop is true");
                    e.printStackTrace();
                }
            }
        }
    }

外侧通过调用thread的interrupt方法中断线程;
1、不能只用boolean的原因在于,如果线程sleep了,那么boolean标志位将不能及时被判断,从而导致线程不能及时退出,需要等sleep结束之后才可以。
2、外侧调用interrupt但sleep响应中断并抛出异常后,会将中断标志位重新恢复为true;所以在异常的catch位置中需要修改boolean标志位,从而及时退出线程。

四、其它线程相关方法

1、yield

Thread类的静态方法;
当一个线程调用yield方法,实际上就是告诉线程调度器当前线程请求让出CPU时间片;当一个线程调用yield方法后,当前线程会让出CPU使用权,进入就绪状态;CPU会从线程就绪队列当中选出优先级最高的线程激活;不过CPU的调度速度很快,很有可能刚让出使用权,就又被执行到。

2、join

等待调用线程执行结束,让调用线程插队。
join方法最后都会调用到如下这个方法,默认join()函数传入的时间是0;当线程A中调用了线程B的join方法;
线程B拿到锁后进入while循环进入wait阻塞;直到线程A执行结束后,唤醒线程B,并检查isAlive,退出循环,线程B继续执行;


public final void join(long millis)
    throws InterruptedException {
        synchronized(lock) {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
            	//进入阻塞,等到join的调用线程执行完毕
                lock.wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                lock.wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
        }
    }

3、wait notify notifyAll

wait、notify、notifyAll都是Object下的方法,标准范式如下:
等待方遵循的策略:
获取对象锁;
判断条件,如果条件不满足,执行wait阻塞等待;
条件满足则执行相应的逻辑;
synchronized(lock){
if(条件不满足){
lock.wait();
}
执行逻辑代码
}

通知方遵循的策略:
获取对象锁;
改变条件;
判断条件,如果条件满足,执行notify或者notifyAll;
synchronized(lock){
改变条件
if(条件满足){
lock.notifyAll();
}
}

wait 方法会释放锁 当进入wait后,当前线程会释放对象锁,锁随即被其它线程所抢占;
notify以及notifyAll本身不会释放锁,当同步代码块执行结束后,锁会释放;

4、sleep

Thread类中的静态方法;
让当前线程进入睡眠状态,可以相应中断,不会释放锁

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值