Java粗浅认识-并发编程(一)

线程简介

进程,操作系统中分配资源的基本单元,线程,操作系统中运行的基本单元,在一个进程中可以包含一个或多个线程,进程间通信,资源共享效率低,在同一个进程中,所有线程共享资源。

线程在使用时,也存在各种问题,线程安全性线程活跃性线程性能

线程安全性

在多线程环境中,能够正确地处理多个线程之间的共享变量,使程序功能正确完成,这里的正确完成,就是每个线程得到预期值。

示例代码中,thread1和thread2共享资源ArrayList,而ArrayList本身并不是线程安全的容器,在每个线程中都各自取出list中第一个元素加50次,我们期望的值是100,经过运行后,值可能不是100,就存在线程安全性问题

public static void count() {
        List<Integer> list = new ArrayList<>();
        list.add(0);
        Thread thread1 = new Thread(new Task(list));
        Thread thread2 = new Thread(new Task(list));
        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.get(0));
    }

    public static class Task implements Runnable {
        List<Integer> list;

        public Task(List<Integer> list) {
            this.list = list;
        }

        @Override
        public void run() {
            for (int i = 0; i < 50; i++) {
                int value = list.get(0);
                list.set(0, ++value);
            }
        }
    }

线程活跃性问题

线程活跃性问题,死锁弱响应性饥饿活锁丢失的信号

死锁

当两个以上的运算单元,双方都在等待对方停止运行,以获取系统资源,但是没有一方提前退出时,就称为死锁。

死锁实例

public static void deadlock() {
        String sourceA = "A";
        String sourceB = "B";

        Thread thread1 = new Thread(new DeadlockTask(sourceA,sourceB));
        Thread thread2 = new Thread(new DeadlockTask(sourceB,sourceA));

        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static  class DeadlockTask implements Runnable{
        private String first;
        private String sec;
        public DeadlockTask(String first,String sec){
            this.first = first;
            this.sec = sec;
        }

        @Override
        public void run() {
            synchronized (first){
                System.out.println(Thread.currentThread().getName()+"获取到资源:"+ first);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (sec){
                    System.out.println(Thread.currentThread().getName()+"获取到资源:"+ sec);
                }
            }
        }
    }

造成死锁有几个因素,禁止抢占、持有和等待、互斥、循环等待,只要打破其中一个条件就不会产生死锁,java中减少在锁嵌套,超时等待获取锁等。

死锁检测手段

1.用 jstack <pid>工具转存stack信息,进行检测

jstack

2.用jvisualvm 图形化工具查看

jvisualvm

饥饿

在多线程环境中,线程永远轮不到cpu时间片,一直处于等待状态,这个不好演示,也不好检测以下程序演示了设置线程优先级,避免饥饿就是尽量减少线程优先级设置。

public static void lockHunger(){
        Thread thread1 = new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("A");
            }
        });
        thread1.setPriority(Thread.MIN_PRIORITY);
        Thread thread2 = new Thread(()->{
            System.out.println("B");
        });
        thread2.setPriority(Thread.MAX_PRIORITY);
        
        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

弱响应性

cpu大量时间在处理其他任务,而对某些线程调度时间较少,导致响应缓慢,缓解办法,就是把响应较慢的线程优先级设置高些。

活锁

线程并没有阻塞,而是不断重试相同的操作,但是总是失败,一直处于这种尴尬的状态,避免的操作,增加重试次数限制机制。

实例代码中,线程尝试去对状态值经行修改,然后做一次db交互。 

    private static void livelock() {
        final AtomicInteger status = new AtomicInteger();
            Thread thread = new Thread(()->{
                while(status.getAndAdd(0)==0){
                    System.out.println("尝试修改状态.");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("做一次DB交互,保存状态值。");
            },"活锁测试.");
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

丢失的信号

给线程一个可预期的信号值,然后让这个线程继续下面的,但是没有得到这个信号,导致的问题。

测试实例,thread1设置信号,thread2根据信号执行任务,由于信号线程的执行无法预知,虽然thread1先调用start(),但是很可能thread2得不到预期的值,避免程序,在thread2中while等待,将不会产生这个问题。

    private static void lostSemaphore() {
        final AtomicBoolean semaphore = new AtomicBoolean();
        Thread thread1 = new Thread(() -> {
            System.out.println("设置信号量");
            semaphore.set(true);
        }, "设置信号量。");

        Thread thread2 = new Thread(() -> {
            if (semaphore.get()) {
                System.out.println("继续我的任务。");
            }
        }, "接受信号量。");
        thread1.start();
        thread2.start();
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

 线程性能

多线程比单线提高了性能,但是也会带来一定开销,当这个开销非常大时,就会出现线程新能问题,具体只对多线程的管理,线程创建,线程调度,线程销毁,上下文切换(线程在运行和阻塞状态相互切换时),自旋等待,内存同步等。

1.采用多线程,是多大限度的让cpu做有用的事情(而不是在上下文切换这种任务消耗),一个任务需要多少线程同时处理才能达到达到最佳性能呢?设置一个非常大的值对不对呢?

这个值需要不断的测试才能得出,在Netty中NioEventLoopGroup中默认值是cup核数*2。

2.阿姆达尔定律(英语:Amdahl's law,Amdahl's argument),一个计算机科学界的经验法则,因吉恩·阿姆达尔而得名。它代表了处理器并行运算之后效率提升的能力。

S=1/(1-a+a/n),a为并行计算部分所占比例,n为并行处理结点个数。这样,当1-a=0时,(即没有串行,只有并行)最大加速比s=n;当a=0时(即只有串行,没有并行),最小加速比s=1;当n→∞时,极限加速比s→ 1/(1-a),这也就是加速比的上限。(阿姆达尔定律

总结,这章节讲了线程简介,以及线程安全性问题,线程活跃性问题,以及线程性能问题,下一章节讲解线程的状态,线程的创建,线程同步器。

猜你喜欢

转载自blog.csdn.net/bpz31456/article/details/85161956