多線程(一)

[TOC]

1. Java并發編程基礎

1.1 什么是線程?

現代操作系統調度的最小單元;

一個進程可以創建多個線程,每個線程擁有自己的計數器、堆棧、局部變量等屬性,同時可以訪問共享的內存變量;

CPU在線程之前高速切換,使之有同時執行的感覺。

1.2 為什么使用多線程?

  • 更多的處理器核心;
  • 更快的響應時間
  • 更好地編程模型

1.3 線程的優先級

1-10個級別,默認是5;

注意:程序的正確性不能依賴線程的優先級高低。

1.4 線程的狀態

  • NEW;
  • RUNNABLE;
  • BLOCKED;
  • WAITING;
  • TIME_WAITING;
  • TERMINATED;

1.5 Daemon線程

一種支持型線程。用于程序中后臺調度及支持性工作。

注意:當一個Java虛擬機中不存在非Daemon線程時,Java虛擬機將會退出。

可以通過Thread.setDaemon(true)將線程設置為Daemon線程

注意:在構建Daemon線程時,不能依靠finall塊中的內容來確保執行關閉或清理資源的邏輯

1.6 啟動和終止線程

  • 構造線程:需要提供:線程組、優先級、是否是Daemon線程等信息
  • 啟動線程:start();其含義是:當前線程(即parent線程)同步告知Java虛擬機,只要線程規劃器空閑,立即啟動該線程
  • 理解中斷:線程的一個標志位屬性,它表示一個運行中的線程是否被其他線程進行了中斷。(見Interrupted.java類)
  • 如何安全的終止線程: 見Shutdown.java
/**
 * Shutdown.java
 *
 * 創建了一個線程CountThread,它不斷地進行變量累加,而主線程嘗試對其進行中斷操作和停止操作。
 */
public class Shutdown {

    public static void main(String[] args) throws InterruptedException {
        Runner one= new Runner();
        Thread countThread = new Thread(one,"countThread");
        countThread.start();
        //睡眠1秒,main線程對CountThread進行中斷,使CountThread能夠感知中斷而結束
        TimeUnit.SECONDS.sleep(1);
        countThread.interrupt();
        Runner two= new Runner();
        countThread = new Thread(two,"countThread");
        countThread.start();
        //睡眠1秒,main線程對two進行取消,使CountThread能夠感知on為false而結束
        TimeUnit.SECONDS.sleep(1);
        two.cancel();
    }

    private static class Runner implements Runnable{
        private long i;
        private volatile boolean on = true;

        @Override
        public void run() {
            while (on && !Thread.currentThread().isInterrupted()){
                i ++ ;
            }
            System.out.println("Count i="+ i);
        }
        public void cancel(){
            on = false;
        }
    }
}

1.7 線程間通信

volatile關鍵字:告知程序任何對該變量的訪問均需要從共享內存中獲取,而對它的改變必須同步刷新回共享內存,以保證可見性。

注意:過多的使用它會降低程序的效率。

synchronized關鍵字:確保多個線程在同一時刻,只能有一個線程處于方法或者同步塊中,保證了線程對變量訪問的可見性和排他性。

對象、對象的監視器、同步隊列、執行線程之間的關系:

線程想要對Object(Object由Synchronized保護)進行訪問

-> 首先需要獲得Object的監視器

-> 獲取監視器成功,則可訪問

-> 獲取監視器失敗,則該線程進入同步隊列,狀態變為阻塞,直到擁有鎖的線程釋放了鎖,會喚醒同步隊列中的線程,再次嘗試進行對監視器的獲取操作

1.8 等待/通知機制

  • notify()
  • notifyAll()
  • wait()
  • wait(long)
  • wait(long,int)

見WaitNotify.java

/**
 * 兩個線程wait線程由notify線程喚醒
 */
public class WaitNotify {

    static boolean flag = true;
    static Object lock = new Object();


    public static void main(String[] args) throws InterruptedException {
        Thread waitThread = new Thread(new Wait(),"waitThread");
        waitThread.start();
        TimeUnit.SECONDS.sleep(1);
        Thread notifyThread = new Thread(new Notify(),"notifyThread");
        notifyThread.start();
    }


        static class Wait implements  Runnable{
        @Override
        public void run() {
            synchronized(lock){
                while(flag){
                    try{
                        System.out.println(Thread.currentThread() +"flag is true. wait@ "+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread() +"flag is false. running@ "+ new SimpleDateFormat("HH:mm:ss").format(new Date()));

            }
        }
    }

    static class Notify implements Runnable{

        @Override
        public void run() {
            synchronized(lock){
                System.out.println(Thread.currentThread() +"hold lock. notify@ "+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
                lock.notifyAll();
                flag = false;
                SleepUtils.second(5);
            }
            synchronized(lock){
                System.out.println(Thread.currentThread() +"hold lock again. sleep@ "+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
                SleepUtils.second(5);
            }
        }
    }

}

輸出:
Connected to the target VM, address: '127.0.0.1:57763', transport: 'socket'
Thread[waitThread,5,main]flag is true. wait@ 09:24:43
Thread[notifyThread,5,main]hold lock. notify@ 09:24:44
Thread[notifyThread,5,main]hold lock again. sleep@ 09:24:49
Disconnected from the target VM, address: '127.0.0.1:57763', transport: 'socket'
Thread[waitThread,5,main]flag is false. running@ 09:24:54

注意點:

  • 先對調用對象枷鎖,再調用notify()、notifyAll()、wait()
  • wait()方法使線程狀態由Running變為Waiting,同時線程被放置到等待隊列
  • notify()、notifyAll()被調用后,等待線程不會立即從wait()返回,需要有鎖的那個線程先釋放鎖以后,才有機會從wait()返回
  • notify()、notifyAll()的操作是將等待隊列中的線程放置到同步隊列中,同時被移動的線程狀態由Waiting轉變為Blocked(不同的是,前者只放一個,后者放置所有線程)。
  • wait()能夠返回,前提是獲得了鎖。

1.9 經典范式:生產者/消費者模式

等待方遵循如下原則:

  • 獲取對象鎖
  • 如果條件不滿足,進行wait()操作,被通知后仍要檢查條件。
  • 條件滿足則執行對應的條件
    通知方遵循如下原則:
  • 獲取對象鎖
  • 改變條件
  • 通知所有等待在對象上的線程

1.10 管道輸入/輸出流

主要用于線程之間的數據傳輸,而傳輸的媒介為內存

4種具體實現:

  • PipedOutputStream
  • PipedInputStream
  • PipedReader
  • PipedWriter

見Piped.java

/**
 * PipedWriter和PipedReader相連接,主線程讀入控制臺輸入的字符,傳給Print線程,打印到控制臺
 */
public class Piped {

    public static void main(String[] args)   {
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        try {
            out.connect(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread printThread = new Thread(new Print(in),"PrintThread");
        printThread.start();
        int receive = 0;
        try {
            while((receive = System.in.read())!= 1){
                out.write(receive);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    static class Print implements  Runnable{
        private PipedReader in;
        public Print(PipedReader in){
            this.in= in;
        }
        @Override
        public void run() {
            int receive = 0;
            try {
                while((receive = in.read()) != -1 ){
                    System.out.println((char) receive);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

1.11 Thread.join()的使用

當線程A執行了thread.join()語句,其含義是:當前線程A等待thread線程終止之后才從thread.join()返回。

見Join.java

/**
 * Join.java
 * 每個線程調用前一個線程的join()方法,意味著:從主線程結束->線程1結束-> ... -> 線程10結束
 */
public class Join {
    public static void main(String[] args) throws InterruptedException {
        Thread previous = Thread.currentThread();
        for(int i = 0; i < 10; i ++){
            Thread thread = new Thread(new Domino(previous),String.valueOf(i));
            thread.start();
            previous = thread;
        }
        TimeUnit.SECONDS.sleep(5);
        System.out.println(Thread.currentThread().getName() + " terminate.");
    }

    static class Domino implements Runnable{
        private Thread thread;
        public Domino(Thread thread){
            this.thread = thread;
        }
        @Override
        public void run() {
            try {
                this.thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " terminate.");
        }
    }

}

輸出:

main terminate.
0 terminate.
1 terminate.
2 terminate.
3 terminate.
4 terminate.
5 terminate.
6 terminate.
7 terminate.
8 terminate.
9 terminate.

join()方法的邏輯結構和等待/通知經典范式一致,即加鎖、循環和處理邏輯3個步驟

1.12 ThreadLocal的使用

ThreadLocal,即線程變量。鍵值存儲結構。

一個線程可以根據一個ThreadLocal對象查詢到綁定在這個線程上的一個值。
見Profiler.java

/**
 * Profiler.java
 *
 *相當于每個線程自己會有自己的本地變量,雖然共享了TIME_THREADLOCAL變量,但在get的時候只會獲取自己線程的本地變量值
 *通過匿名內部類來構建一個ThreadLocal子類,重寫方法initialValue,以便在get和set方法第一次調用時,進行初始化
 */
public class Profiler {
    private static final ThreadLocal<Long> TIME_THREADLOCAL = new ThreadLocal<Long>(){
        protected  Long initialValue(){
            return System.currentTimeMillis();
        }
    };
    public static final void begin(){
        TIME_THREADLOCAL.set(System.currentTimeMillis());
    }

    public static final long end(){
        return System.currentTimeMillis() - TIME_THREADLOCAL.get();
    }

    public static void main(String[] args) throws InterruptedException {
        Profiler.begin();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Cost:" + Profiler.end() + " mills");
    }


}

1.13 線程實例

等待超時模式:

  • 使用場景:調用一個方法時等待一段時間,能在時間內返回,則立即返回,超時,返回默認結果。
  • 基本點:等待持續時間 REMAINING = T 、超時時間 FUTURE = now + T
    見TimeoutPattern.java
/**
 * 一個經典的等待超時模式
 */
public class TimeoutPattern {

    public synchronized Object get(long mills) throws InterruptedException {
        Object result = null;
        long future = System.currentTimeMillis() + mills;
        long remaining = mills;
        while((result == null)&& remaining > 0 ){
            wait(remaining);
            remaining = future - System.currentTimeMillis();
        }
        return result;
    }
}

一個簡單的數據庫連接池實例

重點是:使用等待超時模式,在獲取連接的過程,如果有連接則直接返回;如果沒有,則wait(mills),在其他線程釋放連接時被喚醒,如果超時未被喚醒,返回null。

public class ConnectionPool {

    private LinkedList<Connection> pool = new LinkedList<Connection>();

    public ConnectionPool(int initialSize){
        if(initialSize > 0 ){
            for(int i = 0; i < initialSize ; i ++){
                pool.add(ConnectionDriver.createConnection()); //使用動態代理創建一個連接
            }
        }
    }
    public void releaseConnection(Connection connection){
        if(connection != null){
            synchronized (pool){
                pool.add(connection);
                pool.notifyAll();
            }

        }
    }
    public Connection fetchConnection(long mills) throws InterruptedException {
        synchronized (pool){
            //完全超時
            if(mills < 0 ){
                while (pool.isEmpty()){
                    pool.wait();
                }
                return pool.removeFirst();
            }else {
                long future = System.currentTimeMillis() + mills;
                long remaining = mills;
                while(pool.isEmpty() && remaining > 0 ){
                    pool.wait(remaining);
                    remaining = future - System.currentTimeMillis();
                }
                Connection result = null;
                if(!pool.isEmpty()){
                    result = pool.removeFirst();
                }
                return result;
            }
        }
    }
}
public class ConnectionDriver {
    static class ConnectionHandler implements InvocationHandler{
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(method.getName().equals("commit")){
                TimeUnit.MILLISECONDS.sleep(100);
            }
            return null;
        }
    }
    public static  final Connection createConnection(){
        return (Connection) Proxy.newProxyInstance(ConnectionDriver.class.getClassLoader(),new Class<?>[]{Connection.class},new ConnectionHandler());

    }
}
public class ConnectionPoolTest {

    static ConnectionPool pool = new ConnectionPool(10);
    static CountDownLatch start = new CountDownLatch(1);
    static CountDownLatch end;

    public static void main(String[] args) throws InterruptedException {
        int threadCount = 1000;
        end = new CountDownLatch(threadCount);
        int count = 1000;
        AtomicInteger got = new AtomicInteger();
        AtomicInteger notGot = new AtomicInteger();
        for(int i = 0; i < threadCount; i ++){
            Thread thread = new Thread(new ConnectionRunner(count,got,notGot),"ConnectionRunnerThread");
            thread.start();
        }
        start.countDown();
        end.await();
        System.out.println("total invoke: " + (threadCount * count));
        System.out.println("got connection:  " + got);
        System.out.println("not got connectio " + notGot);
    }
    static class ConnectionRunner implements  Runnable{
        int count;
        AtomicInteger got;
        AtomicInteger notgot;

        public ConnectionRunner(int count,AtomicInteger got,AtomicInteger notgot){
            this.count = count;
            this.got = got;
            this.notgot = notgot;
        }

        @Override
        public void run() {
            try {
                start.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (count > 0 ){
                try {
                    Connection connection = pool.fetchConnection(1000);
                    if(connection!=null){
                        try {
                            connection.createStatement();
                            connection.commit();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }finally {
                            pool.releaseConnection(connection);
                            got.incrementAndGet();
                        }
                    }else{
                        notgot.incrementAndGet();
                    }
                } catch (InterruptedException e) {

                }finally {
                    count --;
                }
            }
            end.countDown();
        }
    }
}

線程池技術

本質:一個線程安全的任務隊列,它連接了工作者線程和客戶端線程。工作者線程中,不斷地在任務隊列中獲取任務,沒有任務就wait,直到在任務隊列中新增一個任務后notify喚醒。

public interface ThreadPool<Job extends Runnable> {

    void execute(Job job);

    void shutdown();

    void addWorkers(int num);

    void removeWorker(int num);

    int getJobSize();
}
public class DefaultThreadPool<Job extends  Runnable> implements ThreadPool<Job> {

    private static final int MAX_WORKER_NUMBERS = 10;

    private static final int DEFAULT_WORKER_NUMBERS = 5;

    private static final int MIN_WORKER_NUMBERS = 1;
    //工作列表,將會向里面插入工作
    private final LinkedList<Job> jobs = new LinkedList<Job>();
    //工作者列表
    private final List<Worker> workers = Collections.synchronizedList(new ArrayList<Worker>());
    //工作者線程數量
    private int workerNum = DEFAULT_WORKER_NUMBERS;
    //線程編號
    private AtomicLong threadNum = new AtomicLong();

    public DefaultThreadPool(){
        initializeWorker(DEFAULT_WORKER_NUMBERS);
    }
    public DefaultThreadPool(int num){
        workerNum = num > MAX_WORKER_NUMBERS ? MAX_WORKER_NUMBERS : num <  MIN_WORKER_NUMBERS ? MIN_WORKER_NUMBERS : num;
        initializeWorker(workerNum);
    }
    //初始化線程工作者
    private void initializeWorker(int num){
        for(int i = 0 ; i < num; i ++ ){
            Worker worker = new Worker();
            workers.add(worker);
            Thread thread = new Thread(worker, "ThreadPool-Worker-"+ threadNum.incrementAndGet());
            thread.start();
        }
    }


    @Override
    public void execute(Job job) {
        if(job != null){
            synchronized (jobs){
                jobs.addLast(job);
                jobs.notifyAll();
            }
        }
    }

    @Override
    public void shutdown() {
        for(Worker worker : workers){
            worker.shutdown();
        }
    }

    @Override
    public void addWorkers(int num) {
        synchronized (jobs){
            if(num + this.workerNum > MAX_WORKER_NUMBERS){
                num = MAX_WORKER_NUMBERS;
            }
            initializeWorker(num);
            this.workerNum += num;
        }
    }

    @Override
    public void removeWorker(int num) {
        synchronized (jobs){
            if(num >= this.workerNum){
                throw  new IllegalArgumentException("beyond worknum");
            }
            //按照給定的數量停止Worker
            int count = 0;
            while(count < num){
                Worker worker = workers.get(count);
                if(workers.remove(worker)){
                    worker.shutdown();
                    count ++;
                }
            }
            this.workerNum -= count;
        }
    }

    @Override
    public int getJobSize() {
        return jobs.size();
    }



    class Worker implements  Runnable{
        //是否工作
        private volatile boolean running = true;
        @Override
        public void run() {
            while (running){
                Job job = null;
                synchronized (jobs){
                    //如果工作者列表是空的,那么久wait
                    while(jobs.isEmpty()){
                        try {
                            jobs.wait();
                        } catch (InterruptedException e) {
                            //感知到外部對WorkerThread的中斷操作,返回
                            e.printStackTrace();
                            return ;
                        }

                    }
                    job = jobs.removeFirst();
                }
                if(job != null){
                    try{
                        job.run();
                    }catch (Exception e){
                        //此處暫時忽略Job執行中的Exception
                    }
                }
            }
        }
        public void shutdown(){
            running =false;
        }
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、基本概念:程序 - 進程 - 線程 程序(program):是為完成特定任務、用某種語言編寫的一組指令的集合。...
    c5fc16271aee閱讀 485評論 0 2
  • 線程概述 線程與進程 進程 ?每個運行中的任務(通常是程序)就是一個進程。當一個程序進入內存運行時,即變成了一個進...
    閩越布衣閱讀 1,019評論 1 7
  • 本文出自 Eddy Wiki ,轉載請注明出處:http://eddy.wiki/interview-java.h...
    eddy_wiki閱讀 2,232評論 0 14
  • 前幾天和大學的室友去了一趟武功山 時間極端兩天 欣賞到了美景亦凈化了自己的心靈 留幾張圖作為紀念
    藍蓮滴露閱讀 229評論 0 2
  • 好長時間沒有寫東西,今天上午終于能靜下心來寫寫最近1個多月的生活 學習 工作。最近的半個月基本就是春困狀態,完全沒...
    peimin閱讀 226評論 0 0