众所周知,有时Java使用对象池作为包装器和String类型,有时它不会.
例如:
Integer i1 = 1;
Integer i2 = 1;
Integer i3 = new Integer(1);
String s1 = "String";
String s2 = "String";
String s3 = new String ("String");
System.out.println("(i1 == i2) " + (i1 == i2));
System.out.println("(i2 == i3) " + (i2 == i3));
System.out.println("(s1 == s2) " + (s1 == s2));
System.out.println("(s2 == s3) " + (s2 == s3));
Execution result:
(i1 == i2) true
(i2 == i3) false
(s1 == s2) true
(s2 == s3) false
Run Code Online (Sandbox Code Playgroud)
如您所见,基元的装箱从池中获取对象,通过字符串文字创建字符串也会从池中获取对象.这些对象实际上是同一个对象( …
我很惊讶Watch Service应该作为带有轮询循环的进程/线程来实现.我不记得Java中应该做的任何其他API.
将它实现为一组回调(听众,观察者等等)会不会更好?
顺便说一句 - 是否有任何第三方库几乎完全相同,但使用回调模型?
我们都非常了解strictfp的工作原理.
像这样的东西:
package com.hk.basicjava.tests.strictfp;
import java.util.*;
public class StrictFP {
public static void main(String[] argv) {
double d = Double.MIN_VALUE;
System.out.println("non strictfp : " + notStrictFP(d)); // may be 4.9E-324
System.out.println("strictfp : " + strictFP(d)); // should be 0
}
static double notStrictFP(double a) {
return (a / 2 ) * 2;
}
static strictfp double strictFP(double a) {
return (a / 2 ) * 2 ;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有人知道特定的硬件/操作系统(也许是JRE)组合,哪里有和没有strictfp说明符的方法返回的结果有差异?
我尝试了几种组合,但没有区别.
Object位于Java类的顶部.String是Object的子类.
那么,首先是什么 - 对象还是字符串?
提示答案是 - 对象.
但有趣的是,Object已经有了一个方法toString(),因此"知道"了String.因此,构造Object时,String必须已经存在.另一方面,String是Object的子类,当构造String时,Object必须已经存在.我们陷入了永无止境的定义循环(以及一些技术问题).这种方法至少违反了单个根类的想法.
关注可能看起来像理论上的而不是实际的.
但问题是我在其他框架中看到了类似的方法.我认为至少在一定程度上它受到了核心Java类设计方式的启发.
你怎么看 - 一般来说Java类之间的循环依赖(特别是在Object/String的情况下)是不可避免的?不应该不惜一切代价避免它们吗?或者,由于合理的妥协,他们有时可以接受(谨慎和谨慎)吗?如果,那么 - 标准是什么?
我一直在考虑向Java语言架构师发送一个提案.
在同步块中
synchronized(lock) {
// If there is no notification before this point
// <--- implicitly put here // lock.notifyAll(); // OR // lock.notify();
}
Run Code Online (Sandbox Code Playgroud)
线程离开同步块后,它不能再调用lock.notifyAll()/ lock.notify()而不会出现异常.
忘记通知其他线程监视器持有者可能会永远让他们(其他线程)等待(除非他们在等待方法中放置了一些超时).
synchronized(lock) {
lock.wait(); //<--- this thread may forever freeze here
}
Run Code Online (Sandbox Code Playgroud)
我无法想象这样的行为(在没有明确通知的情况下在同步块的末尾插入隐式通知)是不合需要的.
相同的方法可以应用于同步方法.
如何(技术上)实现此类行为可以有不同的方式,例如:
@autonotify
synchronized(lock) {
...
}
@autonotify
public void synchronized doSomething() {
...
}
Run Code Online (Sandbox Code Playgroud)
要么:
@autonotifyAll
synchronized(lock) {
...
}
@autonotifyAll
public void synchronized doSomething() {
...
}
Run Code Online (Sandbox Code Playgroud)
或者 - 使自动通知成为默认行为,但保留抑制它的能力,例如:
@suppressautonotify
synchronized(lock) {
...
}
@suppressautonotifyAll
public void …Run Code Online (Sandbox Code Playgroud)