學習Java併發編程

JUC:java

  1. JUC是什麼:java.util.concurrent 在併發(concurrent)編程中使用的工具類編程

  2. 什麼是進程/線程多線程

    進程是一個具備必定獨立功能的程序關於某個數據集合的一次運動單元。它是操做系統動態執行的基本單元,在傳統的操做系統中,進程既是基本的分配單元,也是基本的執行單元。併發

    線程一般在一個進程中科院包含若干個線程,固然一個進程中至少有一個線程,否則沒有存在的意義。線程能夠利用常常所用有的資源,在引入線程的操做系統中,一般把進程做爲分配資源的基本單位,而把線程做爲獨立運行和獨立調度的基本單位,因爲線程比進程更小,基本上不擁有系統資源,故對它的調度所付出的開銷就會小的多,能更高效的提升多個程序間併發執行的程度。ide

    俗話說,舉個栗子 進程就是QQ.exe。 再好比 word文檔若是沒有保存,在通電後打開world文檔,能夠恢復以前未保存的文檔,word也會檢查你的拼寫,這就是是兩個線程:容錯備份,語法檢查。函數

  3. 什麼是併發/並行工具

    併發:同一時刻多個線程訪問同一個資源,多個線程對一個點。spa

    並行:多項工做同時執行。操作系統

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 類型 方法名(參數){} ---------> 能夠有多個;

相關文章
相關標籤/搜索