invariant java,什么是java中的类不变量?

I googled the topic, but besides Wikipedia I didn't find any further useful documentation or articles.

Can anybody explain to me in simple words what it means or refer me to some nice and easy to understand documentation?

解决方案

It doesn't mean anything in particular in reference to java.

A class invariant is simply a property that holds for all instances of a class, always, no matter what other code does.

For example,

class X {

final Y y = new Y();

}

X has the class invariant that there is a y property and it is never null and it has a value of type Y.

class Counter {

private int x;

public int count() { return x++; }

}

fails to maintain two important invariants

That count never returns a negative value because of possible underflow.

That calls to count are strictly monotonically increasing.

The modified class preserves those two invariants.

class Counter {

private int x;

public synchronized int count() {

if (x == Integer.MAX_VALUE) { throw new IllegalStateException(); }

return x++;

}

}

but fails to preserve the invariant that calls to count always succeed normally (absent TCB-violations†) because count might throw an exception or it might block if a deadlocked thread owns the counter's monitor.

Each language with classes make it easy to maintain some class invariants but not others. Java is no exception:

Java classes consistently have or do not have properties and methods, so interface invariants are easy to maintain.

Java classes can protect their private fields, so invariants that rely on private data are easy to maintain.

Java classes can be final, so invariants that rely on there being no code that violates an invariant by crafting a malicious subclass can be maintained.

Java allows null values to sneak in in many ways, so it is tough to maintain "has a real value" invariants.

Java has threads which means that classes that do not synchronize have trouble maintaining invariants that rely on sequential operations in a thread happening together.

Java has exceptions which makes it easy to maintain invariants like "returns a result with property p or returns no result" but harder to maintain invariants like "always returns a result".

† - An externality or TCB violation is an event which a systems designer optimistically assumes will not happen.

Typically we just trust that the basic hardware works as advertised when talking about properties of high-level languages built on them, and our arguments that invariants hold don't take into account the possibility of:

A programmer using debug hooks to alter local variables as a program runs in ways that code cannot.

Your peers don't use reflection with setAccessible to modify private lookup tables.

Loki altering physics causing your processor to incorrectly compare two numbers.

For some systems our TCB might include only parts of the system, so we might not assume that

An administrator or privileged daemon won't kill our JVM process,

but we might assume that

We can checkpoint to a reliable transactional file-system.

The higher-level a system, the larger its TCB typically is, but the more unreliable things you can get out of your TCB, the more likely your invariants are to hold, and the more reliable your system will be in the long run.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值