美文网首页
Java编程思想目录(Bruce Eckel著)

Java编程思想目录(Bruce Eckel著)

作者: 我是小曼巴 | 来源:发表于2020-08-08 12:15 被阅读0次

第10章 内部类

  1. 创建内部类
  2. 链接到外部类
  3. 使用.this和.new
  4. 内部类与向上转型
  5. 在方法和作用域内的内部类
  6. 匿名内
    6.1 再访工厂方法
  7. 嵌套类
    7.1 接口内部的类
    7.2 从多层嵌套类中访问外部类的成员
  8. 为什么需要内部类
    8.1 闭包与回调
    8.2 内部类与控制框架
  9. 内部类的继承
  10. 内部类可以被覆盖吗
  11. 局部内部类
  12. 内部类标识符

第15章 泛型

  1. 与C++的比较
  2. 简单泛型
    2.1 一个元组类库
    2.2 一个堆栈类
    2.3 RandomList
  3. 泛型接口
  4. 泛型方法
    4.1 杠杆利用类型参数推断
    4.2 可变参数与泛型方法
    4.3 用于Generator的泛型方法
    4.4 一个通用的Generator
    4.5 简化元组的使用
    4.6 一个Set实用工具
  5. 匿名内部类
  6. 构建复杂模型
  7. 擦除的神秘之处
  8. 擦除的补偿
  9. 边界
  10. 通配符

第21章 并发

  1. 并发的多面性
    1.1 更快地执行
    1.2 改进代码设计

  2. 基本的线程机制
    2.1 定义任务
    Runnable接口:https://blog.csdn.net/yangyechi/article/details/88079983
    2.2 Thread类
    Thread类构造器接收Runnable对象;
    继承Thread类并覆盖run方法;
    2.3 使用Executor
    Executor用于管理Thread对象;
    CachedThreadPool,FixedThreadPool,SingleThreadPool
    线程池介绍:https://www.cnblogs.com/liyus/p/10235942.html

    public static void main(String[] args) {
        // 不建议这样使用, 建议 new ThreadPoolExcutor(), 强制程序员理解其中各个参数的意思
        ExecutorService executorService = Executors.newCachedThreadPool();  
        for(int i=0; i<5; i++){
            executorService.execute(new TaskThread()); // thread对象或者runnable对象
        }
        executorService.shutdown(); // 不在接收任务
    }

2.4 从任务中产生返回值
Callalbe接口+Future对象:https://blog.csdn.net/zsj777/article/details/85089993
2.5 休眠
2.6 优先级
线程优先级:https://blog.csdn.net/weixin_37139197/article/details/81989749
2.7 让步
yield:https://www.cnblogs.com/chinaifae/articles/10373846.html
2.8 后台线程
https://blog.csdn.net/staticFinal_shuaibi/article/details/84865688
2.9 编码的变体
2.10 术语
2.11 加入一个线程
join:https://www.iteye.com/blog/uule-1101994
2.12 创建有响应的用户界面
2.13 线程组
2.14 捕获异常

  1. 共享受限资源
    3.1 不正确地访问资源
    3.2 解决共享资源竞争
    Synchronized关键字的用法:https://www.cnblogs.com/fnlingnzb-learner/p/10335662.html
    Synchronized关键字配合wait(), notify(), notifyAll()的使用:https://www.cnblogs.com/moongeek/p/7631447.html
    Lock和Condition使用:https://www.cnblogs.com/renjianpiaoliu/p/9250444.html
    Lock的实现原理:https://blog.csdn.net/w_s_h_y/article/details/77450166
    复习Java内存模型:https://www.cnblogs.com/songpingyi/p/9121745.html
    3.3 原子性与易变性
    原子性与易变性介绍:https://blog.csdn.net/victorwux/article/details/80466853
    volatile关键字原理:https://www.cnblogs.com/wildwolf0/p/11449506.html
    3.4 原子类
    Java当中的原子类:https://www.jianshu.com/p/b92f0b7da636
    3.5 临界区
    synchronized实现和Lock实现:https://www.jianshu.com/p/2089ef4d7f09
    3.6 在其它对象上同步
    3.7 线程本地存储
    TheadLocal使用:https://www.zhihu.com/question/341005993/answer/793627819

  2. 终结任务
    4.1 装饰性花园
    4.2 在阻塞时终结
    4.3 中断
    4.4 检查中断

  3. 线程之间的协作
    5.1 wait()与notifyAll()
    5.2 notify()与notifyAll()
    5.3 生产者与消费者
    5.4 生产者-消费者队列
    5.5 任务间使用管道进行输入/输出

  4. 死锁

  5. 新类库中的构件

8.其它

  • 手动实现线程池
public class MyThreadPool {

    //线程池中允许的最大线程数
    private static int MAXTHREDNUM = Integer.MAX_VALUE;
    //当用户没有指定时默认的线程数
    private int threadNum = 6;
    //线程队列,存放线程任务
    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);
            //提交任务后唤醒等待在队列的线程
            queue.notifyAll();
        }
    }

    private class WorkerThread extends Thread {

        private volatile boolean on = true;

        @Override
        public void run() {
            Runnable task = null;
            // 判断是否可以取任务
            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) {
                            // 取到任务后执行
                            task.run();
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            task = null;// 任务结束后手动置空,加速回收
        }

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

    public void shutdown() {
        for (int i = 0; i < threadNum; i++) {
            workerThreads[i].cancel();
            workerThreads[i] = null;
        }
        queue.clear();
    }
}

相关文章

网友评论

      本文标题:Java编程思想目录(Bruce Eckel著)

      本文链接:https://www.haomeiwen.com/subject/ovvhdktx.html