学习Java并发编程

JUC:

  1. JUC是什么:java.util.concurrent 在并发(concurrent)编程中使用的工具类

  2. 什么是进程/线程

    进程是一个具有一定独立功能的程序关于某个数据集合的一次运动单元。它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,也是基本的执行单元。

    线程通常在一个进程中科院包含若干个线程,当然一个进程中至少有一个线程,不然没有存在的意义。线程可以利用经常所用有的资源,在引入线程的操作系统中,通常把进程作为分配资源的基本单位,而把线程作为独立运行和独立调度的基本单位,由于线程比进程更小,基本上不拥有系统资源,故对它的调度所付出的开销就会小的多,能更高效的提高多个程序间并发执行的程度。

    俗话说,举个栗子 进程就是QQ.exe。 再比如 word文档如果没有保存,在通电后打开world文档,可以恢复之前未保存的文档,word也会检查你的拼写,这就是是两个线程:容错备份,语法检查。

  3. 什么是并发/并行

    并发:同一时刻多个线程访问同一个资源,多个线程对一个点。

    并行:多项工作同时执行。

Java 中 线程的状态

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        // 新建状态
        NEW,

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        // 就绪可运行状态
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        // 阻塞状态
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        // 等待状态  一直等-----不见不散
        WAITING,

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        // 等待状态  有时间的等-----过期不候
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        // 终结状态
        TERMINATED;
    }

Java 中 wait 和 sleep功能都是当前线程暂停,有什么区别?

  1. wait放开手去睡,放开手里的锁。

  2. sleep握紧手去睡,醒了手里还有锁。


复习 Synchronized:

举个栗子:卖票程序

package juc;

/**
 * 题目: 三个售票员  卖出   30张票
 * <p>
 * 多线程编程企业级 套路 + 模板
 * 1.在高内聚低耦合的前提下,  线程  操作(对外暴露的调用方法)  资源类
 */
public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 40; i++) {
                    ticket.saleTicket();
                }
            }
        }, "A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 40; i++) {
                    ticket.saleTicket();
                }
            }
        }, "B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 40; i++) {
                    ticket.saleTicket();
                }
            }
        }, "C").start();
    }
}
// 资源类
class Ticket {
    private int number = 30;

    public synchronized void saleTicket() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "\t卖出第:" + (number--) + "\t还剩下:" + number);
        }
    }
}

使用 Lock 和 Lambda 改写买票例子:

package juc.lock;


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

/**
 * @author : 雪飞oubai
 * @date : 2020/3/17 13:55
 * <p>
 * 线程 操作 资源类
 */
public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //线程
        new Thread(() -> { for (int i = 0; i < 40; i++) ticket.saleTicket(); }, "A").start();
        new Thread(() -> { for (int i = 0; i < 40; i++) ticket.saleTicket(); }, "B").start();
        new Thread(() -> { for (int i = 0; i < 40; i++) ticket.saleTicket(); }, "C").start();
    }
}

class Ticket {
    private int number = 30;
    private final Lock lock = new ReentrantLock();

    public void saleTicket() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "\t卖出第:" + (number--) + "\t还剩下:" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

 

LambdaExpress复习:函数式接口前提下,拷贝小括号,写死右箭头,落地大括号

Java8 接口复习:接口里面可以有

  1. public default 类型 方法名(参数){} ---------> 可以有多个;

  2. public static 类型 方法名(参数){} ---------> 可以有多个;

 

上一篇:java线程生命周期


下一篇:总算把线程六种状态的转换说清楚了!