金麟岂是池中物,一遇风云便化龙
Posts tagged java
误区!double类型做加减法不会有误差?
Aug 25th
如果你跟我一样以为Java的double类型只有在作乘除法时才会出现误差,那试一下在Java里执行一下下面的代码:
public static void main(String[] args) { System.out.println(44.42 + 710.79 + 44.42 + 88.85); }
执行前先猜一下结果,是会输出888.48么?还是……?
建议:对于和钱有关的计算,不论加减乘除,统一使用 BigDecimal!
然而不多久之后同事告诉我另一个BigDecimal的问题,试一下下面的代码:
System.out.println(new BigDecimal(0.99).setScale(2, RoundingMode.DOWN).toString());
结果是令人发指的0.98!
看了一下 BigDecimal(double)的源码,其中使用位操作分别提取了0.99浮点值的整数部分和纯小数部分,或者说是2的正数次幂和负数次幂部分。而这样提取出来的值纯小数部分本身就是近似的,与直接使用double类型没有本质区别,这是浮点表示法决定的,再经过2位截取后就产生了误差
而当使用BigDecimal(String)的构造器时得到的是精确值,因为该构造器将数字使用科学计数法表示,即 0.99表示为99*10^-2,这样做运算时先对齐至相同的整数位再进行计算
因此,对上面的补充是:如果要获取最精确的结果,请使用BD+字符串类型的构造器
有关Runtime.addShutdownHook(Thread)
Mar 29th
今天在JDK的javadoc里瞎转的时候无意间发现这么一个有趣的回调方法:
addShutdownHook
public void addShutdownHook(Thread hook)
Registers a new virtual-machine shutdown hook.
The Java virtual machine shuts down in response to two kinds of events:
- The program exits normally, when the last non-daemon thread exits or when the
exit(equivalently,System.exit) method is invoked, or- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.
A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the
exitmethod.Once the shutdown sequence has begun it can be stopped only by invoking the
haltmethod, which forcibly terminates the virtual machine.Once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously-registered hook. Attempting either of these operations will cause an
IllegalStateExceptionto be thrown.Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.
Shutdown hooks should also finish their work quickly. When a program invokes
exitthe expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.Uncaught exceptions are handled in shutdown hooks just as in any other thread, by invoking the
uncaughtExceptionmethod of the thread’sThreadGroupobject. The default implementation of this method prints the exception’s stack trace toSystem.errand terminates the thread; it does not cause the virtual machine to exit or halt.In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.
- Parameters:
hook– An initialized but unstartedThreadobject- Throws:
IllegalArgumentException– If the specified hook has already been registered, or if it can be determined that the hook is already running or has already been runIllegalStateException– If the virtual machine is already in the process of shutting downSecurityException– If a security manager is present and it deniesRuntimePermission("shutdownHooks")- Since:
- 1.3
- See Also:
removeShutdownHook(java.lang.Thread),halt(int),exit(int)
即可以在runtime对象上注册一个回调方法,该方法会在JVM退出时执行传入的线程对象。在mac下试了一下,当调用System.exit()、使用系统kill命令杀掉java进程时该方法都会被调用。但是需要注意的是,当调用kill -9杀进程时,方法不被调用,JVM直接退出,Windows下还没试过,不知道结果如何
植树节,写棵二叉树庆祝一下
Mar 12th
今天下午看见一条推
RT: @rtmeme: RT @foxzzz RT @junyu: 看到 @windbreaker 老师在Buzz说:今天是植树节,写一棵二叉树庆祝之 (via @yangfannet)
于是兴起,花了半个多小时写了棵自认为(从工程而非算法上)比较牛逼的排序二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package com.jayxu; import java.util.Iterator; import java.util.Random; import java.util.Stack; /** * * @author ijay */ public class BinaryTree<T extends Comparable<T>> implements Iterable<T> { private static final char[] FIRST_CHAR = {'B', 'C', 'K', 'F', 'D', 'G'}; private static final char[] FOLLOWING_CHAR = {'a', 'k', 'l', 'p', 'r', 't', 'n', 'm', 's', 'e'}; private static Random r = new Random(System.currentTimeMillis()); private TreeNode root; public void addNode(T value) { root = addToNode(root, value); } private TreeNode addToNode(TreeNode node, T value) { if (node == null) { node = new TreeNode(); node.value = value; } else if (value.compareTo(node.value) <= 0) { node.left = addToNode(node.left, value); } else { node.right = addToNode(node.right, value); } return node; } public Iterator<T> iterator() { return new PreOrderIterator(); } private class TreeNode { TreeNode left; TreeNode right; T value; } private class PreOrderIterator implements Iterator<T> { private Stack<TreeNode> stack = new Stack<TreeNode>(); private PreOrderIterator() { TreeNode current = root; while (current != null) { stack.push(current); current = current.left; } } public boolean hasNext() { return !stack.isEmpty(); } public T next() { TreeNode n = stack.pop(); TreeNode current = n.right; while (current != null) { stack.push(current); current = current.left; } return n.value; } public void remove() { throw new UnsupportedOperationException(); } } public static void main(String[] args) { for (int i = 8; i < Integer.MAX_VALUE; i *= 2) { buildNTraverseTree(buildRawStrings(i), false); } } private static String[] buildRawStrings(int capacity) { String[] s = new String[capacity]; s[0] = "Jay"; s[1] = "Blader"; s[2] = "Fred"; s[3] = "Gavin"; s[4] = "Alex"; for (int i = 5; i < capacity; i++) { StringBuilder sb = new StringBuilder(); sb.append(FIRST_CHAR[r.nextInt(FIRST_CHAR.length)]); int len = r.nextInt(10); for (int j = 0; j < len; j++) { sb.append(FOLLOWING_CHAR[r.nextInt(FOLLOWING_CHAR.length)]); } s[i] = sb.toString(); } return s; } private static void buildNTraverseTree(String[] strings, boolean output) { System.out.print("Current capacity: " + strings.length + "\t"); long start = System.currentTimeMillis(); BinaryTree<String> tree = new BinaryTree<String>(); for (String s : strings) { tree.addNode(s); } for (String s : tree) { if (output) { System.out.println(s); } } System.out.println("took " + (System.currentTimeMillis() - start) + " ms"); } } |
该树特点:
- 使用泛型
- 实现Iterable接口从而能够使用增强for循环遍历
- 实现非递归中序遍历
- 生成随机数据测试
Snow Leopard下的JDK链接有问题
Jan 26th
今天在SL下build JRex,在执行ant make的时候总是出现以下错误:
compile:
[javac] Compiling 86 source files to /Users/ijay/Projects/mozilla/embedding/JRex/classes
[javac] /Users/ijay/Projects/mozilla/embedding/JRex/src/java/netscape/javascript/JSObject.java:38: ?????? sun.plugin.javascript ??????
[javac] import sun.plugin.javascript.JSContext;
[javac] ^
[javac] /Users/ijay/Projects/mozilla/embedding/JRex/src/java/netscape/javascript/JSObject.java:172: ?????? sun.plugin.javascript ??????
[javac] if (c instanceof sun.plugin.javascript.JSContext)
[javac] ^
[javac] /Users/ijay/Projects/mozilla/embedding/JRex/src/java/netscape/javascript/JSObject.java:174: ?Ҳ???????
[javac] ???ţ? ?? JSContext
[javac] λ?ã? ?? netscape.javascript.JSObject
[javac] JSContext j = (JSContext) c;
[javac] ^
[javac] /Users/ijay/Projects/mozilla/embedding/JRex/src/java/netscape/javascript/JSObject.java:174: ?Ҳ???????
[javac] ???ţ? ?? JSContext
[javac] λ?ã? ?? netscape.javascript.JSObject
[javac] JSContext j = (JSContext) c;
[javac] ^
[javac] 4 ????BUILD FAILED
检查了一下build.xml引入的JRex.properties,其中有一行内容如下:
classpath =${build};${java.home}/lib/plugin.jar;${java.home}/lib/deploy.jar;${java.home}/lib/jaws.jar
于是又挨个去找那几个jar包(其中“plugin.jar”最像),最终发现了问题:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/plugin.jar(即“${java.home}/lib/plugin.jar”)是一个符号链接,指向“/System/Library/Frameworks/JavaVM.framework/Versions/A/Resources/Deploy.bundle/Contents/Home/lib/plugin2.jar”,而plugin2.jar不存在……
把原先的符号链接删除,重建链接指向…/plugin.jar,问题解决
已向apple提交该bug







