Java編程思想目錄(Bruce Eckel著)

第10章 內(nèi)部類

  1. 創(chuàng)建內(nèi)部類
  2. 鏈接到外部類
  3. 使用.this和.new
  4. 內(nèi)部類與向上轉(zhuǎn)型
  5. 在方法和作用域內(nèi)的內(nèi)部類
  6. 匿名內(nèi)
    6.1 再訪工廠方法
  7. 嵌套類
    7.1 接口內(nèi)部的類
    7.2 從多層嵌套類中訪問外部類的成員
  8. 為什么需要內(nèi)部類
    8.1 閉包與回調(diào)
    8.2 內(nèi)部類與控制框架
  9. 內(nèi)部類的繼承
  10. 內(nèi)部類可以被覆蓋嗎
  11. 局部?jī)?nèi)部類
  12. 內(nèi)部類標(biāo)識(shí)符

第15章 泛型

  1. 與C++的比較
  2. 簡(jiǎn)單泛型
    2.1 一個(gè)元組類庫
    2.2 一個(gè)堆棧類
    2.3 RandomList
  3. 泛型接口
  4. 泛型方法
    4.1 杠桿利用類型參數(shù)推斷
    4.2 可變參數(shù)與泛型方法
    4.3 用于Generator的泛型方法
    4.4 一個(gè)通用的Generator
    4.5 簡(jiǎn)化元組的使用
    4.6 一個(gè)Set實(shí)用工具
  5. 匿名內(nèi)部類
  6. 構(gòu)建復(fù)雜模型
  7. 擦除的神秘之處
  8. 擦除的補(bǔ)償
  9. 邊界
  10. 通配符

第21章 并發(fā)

  1. 并發(fā)的多面性
    1.1 更快地執(zhí)行
    1.2 改進(jìn)代碼設(shè)計(jì)

  2. 基本的線程機(jī)制
    2.1 定義任務(wù)
    Runnable接口:https://blog.csdn.net/yangyechi/article/details/88079983
    2.2 Thread類
    Thread類構(gòu)造器接收Runnable對(duì)象;
    繼承Thread類并覆蓋run方法;
    2.3 使用Executor
    Executor用于管理Thread對(duì)象;
    CachedThreadPool,F(xiàn)ixedThreadPool,SingleThreadPool
    線程池介紹:https://www.cnblogs.com/liyus/p/10235942.html

    public static void main(String[] args) {
        // 不建議這樣使用, 建議 new ThreadPoolExcutor(), 強(qiáng)制程序員理解其中各個(gè)參數(shù)的意思
        ExecutorService executorService = Executors.newCachedThreadPool();  
        for(int i=0; i<5; i++){
            executorService.execute(new TaskThread()); // thread對(duì)象或者runnable對(duì)象
        }
        executorService.shutdown(); // 不在接收任務(wù)
    }

2.4 從任務(wù)中產(chǎn)生返回值
Callalbe接口+Future對(duì)象:https://blog.csdn.net/zsj777/article/details/85089993
2.5 休眠
2.6 優(yōu)先級(jí)
線程優(yōu)先級(jí):https://blog.csdn.net/weixin_37139197/article/details/81989749
2.7 讓步
yield:https://www.cnblogs.com/chinaifae/articles/10373846.html
2.8 后臺(tái)線程
https://blog.csdn.net/staticFinal_shuaibi/article/details/84865688
2.9 編碼的變體
2.10 術(shù)語
2.11 加入一個(gè)線程
join:https://www.iteye.com/blog/uule-1101994
2.12 創(chuàng)建有響應(yīng)的用戶界面
2.13 線程組
2.14 捕獲異常

  1. 共享受限資源
    3.1 不正確地訪問資源
    3.2 解決共享資源競(jìng)爭(zhēng)
    Synchronized關(guān)鍵字的用法:https://www.cnblogs.com/fnlingnzb-learner/p/10335662.html
    Synchronized關(guān)鍵字配合wait(), notify(), notifyAll()的使用:https://www.cnblogs.com/moongeek/p/7631447.html
    Lock和Condition使用:https://www.cnblogs.com/renjianpiaoliu/p/9250444.html
    Lock的實(shí)現(xiàn)原理:https://blog.csdn.net/w_s_h_y/article/details/77450166
    復(fù)習(xí)Java內(nèi)存模型:https://www.cnblogs.com/songpingyi/p/9121745.html
    3.3 原子性與易變性
    原子性與易變性介紹:https://blog.csdn.net/victorwux/article/details/80466853
    volatile關(guān)鍵字原理:https://www.cnblogs.com/wildwolf0/p/11449506.html
    3.4 原子類
    Java當(dāng)中的原子類:http://www.itdecent.cn/p/b92f0b7da636
    3.5 臨界區(qū)
    synchronized實(shí)現(xiàn)和Lock實(shí)現(xiàn):http://www.itdecent.cn/p/2089ef4d7f09
    3.6 在其它對(duì)象上同步
    3.7 線程本地存儲(chǔ)
    TheadLocal使用:https://www.zhihu.com/question/341005993/answer/793627819

  2. 終結(jié)任務(wù)
    4.1 裝飾性花園
    4.2 在阻塞時(shí)終結(jié)
    4.3 中斷
    4.4 檢查中斷

  3. 線程之間的協(xié)作
    5.1 wait()與notifyAll()
    5.2 notify()與notifyAll()
    5.3 生產(chǎn)者與消費(fèi)者
    5.4 生產(chǎn)者-消費(fèi)者隊(duì)列
    5.5 任務(wù)間使用管道進(jìn)行輸入/輸出

  4. 死鎖

  5. 新類庫中的構(gòu)件

8.其它

  • 手動(dòng)實(shí)現(xiàn)線程池
public class MyThreadPool {

    //線程池中允許的最大線程數(shù)
    private static int MAXTHREDNUM = Integer.MAX_VALUE;
    //當(dāng)用戶沒有指定時(shí)默認(rèn)的線程數(shù)
    private int threadNum = 6;
    //線程隊(duì)列,存放線程任務(wù)
    private List<Runnable> queue;

    private WorkerThread[] workerThreads;

    public MyThreadPool(int threadNum) {
        this.threadNum = threadNum;
        if (threadNum > MAXTHREDNUM)
            threadNum = MAXTHREDNUM;
        this.queue = new LinkedList<>();
        this.workerThreads = new WorkerThread[threadNum];
        init();
    }

    //初始化線程池中的線程
    private void init() {
        for (int i = 0; i < threadNum; i++) {
            workerThreads[i] = new WorkerThread();
            workerThreads[i].start();
        }
    }

    public void execute(Runnable task) {
        synchronized (queue) {
            queue.add(task);
            //提交任務(wù)后喚醒等待在隊(duì)列的線程
            queue.notifyAll();
        }
    }

    private class WorkerThread extends Thread {

        private volatile boolean on = true;

        @Override
        public void run() {
            Runnable task = null;
            // 判斷是否可以取任務(wù)
            try {
                while (on && !isInterrupted()) {
                    synchronized (queue) {
                        while (on && !isInterrupted() && queue.isEmpty()) {
                            queue.wait(1000);
                        }
                        if (on && !isInterrupted() && !queue.isEmpty()) {
                            task = queue.remove(0);
                        }

                        if (task != null) {
                            // 取到任務(wù)后執(zhí)行
                            task.run();
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            task = null;// 任務(wù)結(jié)束后手動(dòng)置空,加速回收
        }

        public void cancel() {
            on = false;
            interrupt();
        }
    }

    public void shutdown() {
        for (int i = 0; i < threadNum; i++) {
            workerThreads[i].cancel();
            workerThreads[i] = null;
        }
        queue.clear();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容