Java Nio系列-Buffer

Buffer

最开始看java.nioBuffer包,相关的class非常多,一头雾水,所以还是先对其命名进行简单梳理。

命名

所有的命名均遵循如下的基本原则。

[MS][T]Buffer[AM][BO]

  • MS - memory scheme: Heap or Direct.
  • T - type: int, short, float, double, char or byte.
  • AM - access mode: W writable (default), R read-only.
  • BO - byte ordering: S - non-native, U - native.
  • B - BigEndian or L - LittleEndian.

Classes whose names dont include R, by default are W - writable.

所以基本所有的buffer基本都是:

  1. [T]Buffer
  2. Heap[T]Buffer
  3. Heap[T]BufferR
  4. Direct[T]Buffer[S|U]
  5. Direct[T]BufferR[S|U]
  6. ByteBufferAs[T]Buffer[B|L]
  7. ByteBufferAs[T]BufferR[B|L]

比如:

ByteBufferAsCharBufferB

1
2
3
4
5
6
7
public char get() {
return Bits.getCharB(bb, ix(nextGetIndex()));
}
public char get(int i) {
return Bits.getCharB(bb, ix(checkIndex(i)));
}

ByteBufferAsCharBufferL

1
2
3
4
5
6
7
public char get() {
return Bits.getCharL(bb, ix(nextGetIndex()));
}
public char get(int i) {
return Bits.getCharL(bb, ix(checkIndex(i)));
}

ByteBufferAsCharBufferRB

1
2
3
4
5
6
7
8
9
public CharBuffer put(char x) {
throw new ReadOnlyBufferException();
}
public boolean isReadOnly() {
return true;
}

DirectCharBufferS

1
2
3
public char get() {
return (Bits.swap(unsafe.getChar(ix(nextGetIndex()))));
}

DirectCharBufferU

1
2
3
public char get() {
return ((unsafe.getChar(ix(nextGetIndex()))));
}

Buffer基础

其实就是一个方便读写操作的数据结构,定义了一些属性,方便操作。

0 <= mark <= position <= limit <= capacity

其中flip() clear() rewind() 三个对position, limit的操作

  • flip: limit=position, position。 基本就是读写切换,进行read操作(from buffer)
  • clear: position=0, limit=capacity。开启一个新的write(for buffer)
  • rewind: position=0。 重新进行read。

mark默认-1, undefined. mark()操作使得mark=position, reset() 使得 position=mark

同事Buffer可以是readonly

它是非thread safety的,特殊情况下,使用需要特别小心。

同时它是支持invocation chaining,

buffer.flip().position(5).limit(100)

非boolean类型的所有基本类型都有一个对应的buffer。

业务型创业公司的技术发展

来到业务型创业公司将近一年半了,最初的激情过后,总有些淡淡的隐忧。创业成功毕竟是我们选择创业的目标,但是技术的衰减也是我们不希望看到的,无论是职业生涯还是技术生涯,Hoffman的ABZ理论,是一个毕竟好的指导准则,创业的Plan A如果失败了,我们技术的Plan B如何

Distributed systems for fun and profit读书笔记

这两天读了一个比较有意思的系列文章,Distributed systems for fun and profit,简单的做下读书笔记。

文章目的有二:

  • 梳理一些关键概念
  • 总结梳理分布式里的一些细节思想

在作者看来,分布式编程就是用来关注

  • 信息以光速传递
  • 不相干的个体不相关连的失败

换句话说,其核心就是处理distance,并且不止一个。希望读过该文章之后,能够对distance, time,consistency models三者之间的相互影响,有个比较感性的认识。

###Distributed systems at a high level

Distributed programming is the art of solving the same problem that you can solve on a single computer using multiple computers.

任何计算机系统,需要完成两个任务:

  • 存储 (Storage)
  • 计算 (Computation)

Cluster size

performance gap. 机器的增长带来的性能增长是非线性的

希望达到的目标:Scalability

is the ability of a system, network, or process, to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growth.

关注的三点:

  • Size scalability
  • Geographic scalability
  • Administrative scalability:

A scalable system is one that continues to meet the needs of its users as scale increases. There are two particularly relevant aspects - performance and availability - which can be measured in various ways.

一个扩展性很强的系统,就是可以持续的满足其用户的大规模增长。这里有两个特殊的相关概念,performance,availability,他们可以从多种方式来度量

Performance (and latency)

Performance is characterized by the amount of useful work accomplished by a computer system compared to the time and resources used.

体现在:

  • 低延迟 Short response time/low latency for a given piece of work
  • 高吞度 High throughput (rate of processing work)
  • 低资源利用率 Low utilization of computing resource(s)

Latency The state of being latent; delay, a period between the initiation of something and the occurrence.

Availability (and fault tolerance)

the proportion of time a system is in a functioning condition. If a user cannot access the system, it is said to be unavailable.

Availability = uptime / (uptime + downtime)

Availability % How much downtime is allowed per year?
90% (“one nine”) More than a month
99% (“two nines”) Less than 4 days
99.9% (“three nines”) Less than 9 hours
99.99% (“four nines”) Less than an hour
99.999% (“five nines”) ~ 5 minutes
99.9999% (“six nines”) ~ 31 seconds

高效使用Xcode[译]

翻译来源Supercharging Your Xcode Efficiency

You’ve all seen the all-star Hollywood programmer hacking through the mainframe, fingers racing on the keyboard while terminals fly across the screen. If you’ve ever wanted to be like that, you’re in the right place!
This tutorial will teach you how to be more like that programmer, in Xcode. Call it what you like — magic, mad skillz, pure luck or hacks, there is no doubt you’ll feel much cooler (and have improved Xcode efficiency) after following along this tutorial, and maybe even save the world from destruction with your newly found prowess.
你曾经在电影里看到一些伟大的黑客程序员操作大型机,手指扫过键盘,屏幕里的终端便飞驰而过,如果你也希望像他们那样,今天你来对地方了!

Learn some cool Xcode Tricks!