900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 多线程高并发编程(1) -- 基础及详解

多线程高并发编程(1) -- 基础及详解

时间:2024-05-08 15:01:52

相关推荐

多线程高并发编程(1) -- 基础及详解

进程和线程的区别:

进程的内存大小为:堆内存+线程数量*栈内存,线程数量 =( 最大地址空间[MaxProcessMemory] - JVM堆内存 - 系统保留内存[ReservedOsMemory] )/ ThreadStackSize(XSS),从中可以看出,线程的数量随栈内存的增多而减少。

线程是程序执行的一个路径,每一个线程都有自己的局部变量表、程序计数器(指向正在执行的指令指针)以及各自的生命周期。当启动了一个Java虚拟机(JVM)时,从操作系统开始就会创建一个新的进程(JVM进程),JVM进程将会派生或者创建很多线程。

一个线程的创建肯定是由另一个线程完成的;被创建线程的父线程是创建它的线程;

线程会带来额外的开销,如CPU调度时间、并发控制开销等;每个线程在自己的工作内存交互,加载和存储主内存控制不当会造成数据不一致。

一.线程创建方式:

构造Thread类:实现线程的执行单元run有两种方式,分别是下面

继承Thread,重写run方法:Thread实现了Runnable接口,使用start开启线程,start开启后线程会加入调度器,然后调用run方法,start会调用start0本地方法跟OS进行交互运行;下面是start源码解析

/*** Causes this thread to begin execution; the Java Virtual Machine* calls the <code>run</code> method of this thread.* 开启线程,JVM会调用run方法【start使用了模板方法】* <p>* It is never legal to start a thread more than once.* 不能两次启动线程,否则报IllegalThreadStateException异常* In particular, a thread may not be restarted once it has completed* execution.* 一个线程生命周期结束,也就是到了TERMINATED状态,再次调用start方法是不允许的,* 也就是TERMINATED状态没法回到RUNNABLE/RUNNING状态。** @exception IllegalThreadStateException if the thread was already*started.* @see #run()* @see #stop()*/public synchronized void start() {//线程安全的/*** This method is not invoked for the main method thread or "system"* group threads created/set up by the VM. Any new functionality added* to this method in the future may have to also be added to the VM.* 这个方法不会被主线程调用或通过虚拟机系统线程组创建起来。未来任何添加到该方法里的新功能可能需要加入到虚拟机中** A zero status value corresponds to state "NEW".* 线程被构造后的new状态,threadStatus的属性值是0*/if (threadStatus != 0)throw new IllegalThreadStateException();/* Notify the group that this thread is about to be started* so that it can be added to the group's list of threads* and the group's unstarted count can be decremented. * 通知线程组新线程将要启动,以便它可以添加到线程组列表并且线程组没有开始计数*/group.add(this);//加入线程组boolean started = false;try {start0();//调用本地方法started = true;} finally {try {if (!started) {//启动失败group.threadStartFailed(this);//线程启动失败,从组中移除该线程}} catch (Throwable ignore) {/* do nothing. If start0 threw a Throwable thenit will be passed up the call stack */}}}

void add(Thread t) {synchronized (this) {if (destroyed) {//线程组状态校验throw new IllegalThreadStateException();}if (threads == null) {threads = new Thread[4];//初始化长度为4的线程组} else if (nthreads == threads.length) {threads = Arrays.copyOf(threads, nthreads * 2);//数组满了就扩容2倍}threads[nthreads] = t;//当前线程添加到线程组中// This is done last so it doesn't matter in case the// thread is killednthreads++;//线程数+1// The thread is now a fully fledged member of the group, even// though it may, or may not, have been started yet. It will prevent// the group from being destroyed so the unstarted Threads count is// decremented.nUnstartedThreads--;//未启动线程数-1}}

private native void start0();//本地方法调用重写的run方法

void threadStartFailed(Thread t) {synchronized(this) {remove(t);//移除当前线程nUnstartedThreads++;//没有启动的线程数量+1}}//=======================测试============================

Thread t = new Thread(){@Overridepublic void run(){try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}};t.start();t.start();//不能两次启动,第二次启动是不允许的,报IllegalThreadStateException,此时该线程是处于运行状态//=======================测试==============================Thread t = new Thread(){@Overridepublic void run(){try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}};t.start();TimeUnit.SECONDS.sleep(10);//设置休眠时间,上面的线程的生命周期已经终止,下面再次启动报IllegalThreadStateExceptiont.start();

实现Runnable接口,重写run方法并且将Runnable实例用作构造Thread的参数【单继承有局限性,推荐使用接口】:将线程的控制(start)和业务逻辑(run)的运行彻底分离开来,使用的是策略模式;Thread的run方法是不能共享的,但Runnbale的run方法可以共享,使用同一个Runnable的实例构造不同的Thread实例;把实现类对象(实现Runnable接口的类的实例化)放入代理类对象(Thread构造方法)中,使用的是代理模式;下面是静态代理的代码解释:

public class StaticProxy {public static void main(String[] args) {new Weeding(new Me()).happyMarry();// new Thread(对象).start();类似}}interface Marry {void happyMarry();}//真实角色class Me implements Marry {@Overridepublic void happyMarry() {System.out.println("me will marry!");}}//代理对象class Weeding implements Marry{//真实角色private Marry marry;public Weeding(Marry marry){this.marry=marry;}@Overridepublic void happyMarry() {System.out.println("start");marry.happyMarry();System.out.println("end");}}

实现Callable接口,重写call方法,Future获取返回值:Callable能接受一个泛型,然后在call方法中返回一个指定类型的值;

public interface Callable<V> {V call() throws Exception;}

//线程池队列开启线程,不会产生脏读数据//使用步骤://1.创建目标对象new//2.创建执行服务线程池//3.提交执行submit//4.获取结构get//5.关闭服务shutdownNowpublic class MyThread implements Callable {private static int count = 20;public static void main(String[] args) throws ExecutionException, InterruptedException {MyThread m1 = new MyThread();MyThread m2 = new MyThread();MyThread m3 = new MyThread();MyThread m4 = new MyThread();ScheduledExecutorService service = new ScheduledThreadPoolExecutor(2,new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build());Future submit = service.submit(m1);Future submit1 = service.submit(m2);Future submit2 = service.submit(m3);Future submit3 = service.submit(m4);System.out.println(submit.get());System.out.println(submit1.get());System.out.println(submit2.get());System.out.println(submit3.get());service.shutdown();}@Overridepublic Object call() throws Exception {count--;return count;}}

匿名内部类;

new Thread(){//相当于继承Thread的方式public void run(){System.out.println("thread1 start ... ");}}.start();new Thread(new Runnable() {//相当于实现Runnable接口的方式@Overridepublic void run() {System.out.println("thread2 start .... ");}}).start();

定时器(Timer);

Timer timer = new Timer();//创建时间器timer.schedule(new TimerTask() {//使用schedule,参数为定时器任务并重写run方法@Overridepublic void run() {System.out.println("timer task is run");}}, 0, 1000);

线程池(内部使用队列,所以加入线程池的线程是顺序执行):使用execute和重写Runnbale的run方法;

ScheduledExecutorService service = new ScheduledThreadPoolExecutor(2,new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build());service.execute(new Runnable() {@Overridepublic void run() {System.out.println("run test");}});

lambda表达式;

new Thread(()-> {for(int i = 1 ; i<10 ; i++){System.out.println("It is a lambda function!");}}).start();

Spring方式(@Async注解);

@Testpublic void test() {run();}@Asyncpublic void run(){System.out.println("Async Test");}

二.线程生命周期

new新生状态:当用new创建一个Thread对象时,此时它并不处于执行状态,因为没有调用star启动该线程,那么线程的状态为new状态,也就是说,它只是Thread对象的状态,在没有start之前,该线程是不存在的

runnable就绪状态:线程对象进入runnable就绪状态必须调用start方法,那么此时才是真正地在JVM进程中创建了一个线程就绪状态不会直接进入阻塞状态和死亡状态,即使是在线程的执行逻辑中调用wait、sleep或其他block的IO操作等,也必须先获得CPU的调度执行权才可以,严格来说,就绪状态的线程只能意外终止或进入运行状态;

running运行状态:一旦CPU通过轮询或其他方式从任务可执行队列中选中了线程,此时它才能真正地执行自己的逻辑代码一个正在running状态的线程事实上也是一个runnable的,但是反过来则不成立;

sleep:使当前线程进入指定毫秒级的休眠,暂停执行,但不会放弃monitor锁的所有权,即不会释放锁资源;使用TimeUnit来替代Thread.sleep,省去了时间单位的换算步骤;

yield:属于一种启发式的方法,其会提醒调度器我愿意放弃当前的CPU资源,如果CPU的资源不紧张,则会忽略这种提醒;yield只是一个提示(hint),CPU调度器并不会担保每次都能满足yield提示;

sleep和yield的区别:

sleep会导致当前线程暂停指定的时间,没有CPU时间片的消耗;

yield只是对CPU调度器的一个提示,如果CPU调度器没有忽略这个提示,它会导致线程上下文的切换;

sleep会使线程短暂block,会在给定的时间内释放CPU资源

yield会使running状态的线程进入runnable状态(如果CPU调度器没有忽略这个提示的话);

sleep几乎百分之百地完成了给定时间的休眠,但yield的提示并不能一定担保;

一个线程sleep另一个线程interrupt会捕获到中断信号,而yield则不会;

join:join某个线程A,会使当前线程B进入等待,直到线程A结束生命周期;可以使用join来达到线程顺序执行的效果

wait:表示线程一直等待,直到其他线程通知,与sleep不同的是它会释放锁;调用wait会加入wait set中,notify会随机唤醒一个,notifyAll会弹出所有线程;

notify:唤醒一个处于等待状态的线程;

notifyAll:唤醒同一个对象上所有调用wait方法的线程,优先级高的线程优先调度

synchronized:同步,内置锁、互斥锁、可重入锁,锁定共享资源(共享资源对象不能为null,使用static修饰保持对象引用地址只有一份),依赖JVM,JVM指令是monitor enter和monitor exit;synchronized的指令严格遵守java happens-before规则,一个monitor exit指令之前必定要有一个monitor enter;不可中断锁,适合竞争不激烈,可读性好

由于同一进程的多个线程共享同一块存储空间,在带来方便的同时,也带来了访问冲突的问题。为了保证数据在方法中被访问时的正确性,在访问时加入锁机制(synchronized),当一个线程获得对象的排他锁,独占资源,其他线程必须等待,使用后释放锁即可。但存在以下问题: 一个线程持有锁会导致其他所有需要此锁的线程挂起;在多线程竞争下,加锁、释放锁会导致比较多的上下文切换和调度延时,引起性能问题;如果一个优先级高的线程等待一个优先级低的线程释放锁会导致优先级倒置,引起性能问题;

锁信息存在对象头中:

Mark Word

线程id

Epoch

对象的分代年龄信息

是否是偏向锁

锁标志位

Class Metadata Address

使用范围: 修饰代码块:大括号括起来的代码,作用于调用的对象

@Overridepublic void run() {synchronized (this) {//类A实现了Runnable,重写了run方法,实例化对象a,b分别加入到Thread构造方法中并开启线程,this是两个不同的对象;System.out.println(this.hashCode());}}MyThread thread = new MyThread();MyThread thread2 = new MyThread();Thread t1 = new Thread(thread, "t1");Thread t2 = new Thread(thread2, "t2");Thread t3 = new Thread(thread, "t3");Thread t4 = new Thread(thread, "t4");t1.start();t2.start();t3.start();t4.start();===========结果=================693024158125914623812591462381259146238

修饰方法:整个方法,作用于调用的对象

@Overridepublic synchronized void run() {//类A实现了Runnable,重写了run方法,实例化对象a,b分别加入到Thread构造方法中并开启线程,this是两个不同的对象;System.out.println(this.hashCode());}MyThread thread = new MyThread();MyThread thread2 = new MyThread();Thread t1 = new Thread(thread, "t1");Thread t2 = new Thread(thread2, "t2");Thread t3 = new Thread(thread, "t3");Thread t4 = new Thread(thread, "t4");t1.start();t2.start();t3.start();t4.start();===============结果===================487590100697138600697138600697138600

修饰静态方法:整个静态方法,作用于所有对象

@Overridepublic void run() {test1();}public static synchronized void test1(){System.out.println(MyThread.class.hashCode());}MyThread thread = new MyThread();MyThread thread2 = new MyThread();Thread t1 = new Thread(thread, "t1");Thread t2 = new Thread(thread2, "t2");Thread t3 = new Thread(thread, "t3");Thread t4 = new Thread(thread, "t4");t1.start();t2.start();t3.start();t4.start();===============结果===================6566818656681865668186566818

修饰类:括号括起来的部分,作用于所有对象

@Overridepublic void run() {test1();}public static void test1(){synchronized (MyThread.class) {System.out.println(MyThread.class.hashCode());}}MyThread thread = new MyThread();MyThread thread2 = new MyThread();Thread t1 = new Thread(thread, "t1");Thread t2 = new Thread(thread2, "t2");Thread t3 = new Thread(thread, "t3");Thread t4 = new Thread(thread, "t4");t1.start();t2.start();t3.start();t4.start();==========结果=============6566818656681865668186566818

Lock:显示锁,依赖特殊的CPU指令;可中断锁,多样化同步,竞争激烈时能维持常态;

public class MyLock implements Lock {//自定义Lockprivate boolean isLocked = false;@Overridepublic void lock() {while (isLocked){//已经获得锁try {wait();//等待} catch (InterruptedException e) {e.printStackTrace();}}isLocked=true;//获锁成功}@Overridepublic void unlock() {isLocked = false;//释放锁notify();//唤醒等待线程}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。