Java theory and practice】的更多相关文章

This content is part of the series: Java theory and practice A brief history of garbage collection Anatomy of a flawed microbenchmark Are all stateful Web applications broken? Be a good (event) listener Building a better HashMap Characterizing thread…
Why thread pools? Many server applications, such as Web servers, database servers, file servers, or mail servers, are oriented around processing a large number of short tasks that arrive from some remote source. A request arrives at the server in som…
粗略看完<Java Concurrency in Practice>这部书,确实是多线程/并发编程的一本好书.里面对各种并发的技术解释得比较透彻,虽然是面向Java的,但很多概念在其他语言的并发编程中,也可以用到.因此,开始写下其读书笔记,归纳总结. 闲话少说,从第十章开始,先上思维导图: 本章的重点,是死锁以及死锁的分析和避免方法. 10.1.1 锁的顺序产生死锁: public class WorkerThread implements Runnable{ private LeftRigh…
Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与异步模型相比,多线程同步模型更简单; 通过分离界面线程和工作线程, 可用于创建灵敏的用户界面. 劣势 多线程模型下,对象的状态不在受顺序执行的安全保护,而是需要同步. 同步下可能会出现不一致的问题,如死锁,饥饿. 多线程上下文切换开销可能会导致性能下降. 线程安全和同步 线程安全就是正确的同步状态,…
Writing thread-safe code is managing access to state and in particular to shared, mutable state. Object's state is its data, stored in state variables such as instance or static fields. Whether an object needs to be thread-safe depends on whether it…
第一章   线程共享进程范围内的资源,但每个线程都有各自的程序计数器.栈以及局部变量等. 多个线程可以同时调度到多个CPU上运行.   线程的优势? 在服务应用程序中,可以提升资源利用率以及系统吞吐率,发挥多处理器的强大功能.   线程的优先级  执行时间  线程切换需要额外的开销   第二章   如果多个线程访问同一个可变的状态变量是没有使用合适的同步,那么程序就会出现错误,有以下三种方法修复这种问题. 1.不在线程之间共享该状态变量 2.将状态变量改为不可变的变量 3.在访问状态变量时使用同…
这一章开讲任务执行.绝大多数并发程序的工作都可以分解为抽象的.互不相关的工作单元,称之为任务(Task). 使用java线程来执行任务 以web服务器的实现举例, 此时将用户的一次连接,当做一个独立的任务. 单线程顺序执行所有任务. ServerSocket socket = new ServerSocket(80); while(true) { Socket connection = socket.accept(); handleRequest(connection); } 这是最简单的方式,…
Thread Safety线程安全 线程安全编码的核心,就是管理对状态(state)的访问,尤其是对(共享shared.可变mutable)状态的访问. shared:指可以被多个线程访问的变量 mutable:指在其生命周期内,它的值可被改变 通常,一个对象Object的状态state就是他的数据data,存储于状态变量(state variables)如实例对象或者静态变量,以及他所依赖的其他对象. Java中最常用的同步机制是使用Synchronized关键字,其他还有volatile变量…
线程安全 定义 A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coo…
1.1. A (Very) Brief History of Concurrency motivating factors for multiple programs to execute simultaneously: Resource utilization. Programs sometimes have to wait for external operations such as input or output, and while waiting can do no useful w…