java
共 80 篇文章
Java NIO
看了这个java nio的教程,明白了什么是Selector.wakeUp() http://ifeve.com/selectors/ wakeUp() 某个线程调用select()方法后阻塞了,即使没有通道已经就绪,也有办法让其从select()方法返回。只要让其它线程在第一个线程调用select()方法的那个对象上调用Selector.wakeup()方法即可。阻塞在select()方法上的线程会立马返回。 如果有其它线程调用了wakeup()方法,但当前没有线程阻塞在select()方法上,下个调用select()方法的线程会立即“醒来(wake up)”。
2014-01-10 21:28 · java
再谈Java的wait(), sleep(), notify()和notifyAll()
一段时间不用java,这些概念就全混淆了,有必要彻底澄清一下,总结在这里,当然以我的本事,不太可能写出水平较高的总结,这里主要是总结stackoverflow上面高人的言论。 先说sleep() 和 wait() sleep() method causes the current thread to move from running state to block state for a specified time. If the current thread has the lock of any object then it keeps holding it, which means …
2013-07-25 10:59 · java
How to generate the serialVersionUID when you implement Serializable interface,j
http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/serialver.html
2013-07-01 10:52 · java
Java Override的两个问题
1: 如果子类中的方法的参数是父类的方法的子类型,那么算不算override? 例如: class A { public void foo(A a){ } } class B extends A { public void foo(B b){ } } 2:如果子类中的方法的返回类型改变了,那么算不算override? class A { public A foo(A a){ return a; } } class B extends A { public B foo(B b){ return b; } } There is answer from stackoveflow: http://s…
2013-06-01 11:40 · java
How many string objects are created?
This is a very common java interview question. Given the following: String x = new String("xyz"); y="abc"; x=x+y; How Many Strings have been created: A - 2 B - 3 C - 4 D - 5 To answer this question, I suggest you to read http://www.javaranch.com/journal/200409/Journal200409.jsp
2013-06-01 10:18 · java
使用Java的DelayQueue容易碰到的一个坑
今天不忙,学习一下java.util.concurrent.DelayQueue这个类的使用。参照了 http://www.concretepage.com/java/example_delayqueue_java.php 上的例子,但是这里有个坑。 先看一下整个code吧: import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class DelayQueueExample { /** * @param…
2013-05-27 17:32 · java
[leetcode] Balanced Binary Tree
Check if a binary tree is balanced or not. This solution is from the discuss board. Much better than mine. /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced…
[leetcode] find median of two sorted arrays
http://leetcode.com/onlinejudge#question_4 Solution: a little ugly. public class Solution { public double findMedianSortedArrays(int A[], int B[]) { // Start typing your Java solution below // DO NOT write main() function int len1 = A.length; int len2 = B.length; int i = 0; int j = 0; int k = 0; do…
[leetcode] word ladder
Q: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start = "hit" end = "cog" dict = As one shortest t…
[leetcode] word ladder II
http://leetcode.com/onlinejudge#question_126 Q: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start = "hit…
[leetcode] Count and Say
http://leetcode.com/onlinejudge#question_38 Question: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n, genera…
Date/Time处理函数总结 [To Do]
几种我所用到的用来处理日期,时间的函数总结。 Perl localtime # 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); If parameter is not provides, it will use current time. Note the $year returned contains the number of years since 1900, to get a 4-digit year you need: $year += 1900; …
2013-04-12 10:46 · java · perl · python · javascript · shell
[leetcode] Palindrome Partition
http://leetcode.com/onlinejudge#question_131 Question: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return , ] Solution: This is also a DP. I like DP :D import java.util.Arra…
[leetcode] Palindrome Partitioning II
http://leetcode.com/onlinejudge#question_132 Question: Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning could be produ…
Profiling your Java code using Spring
Quite good article!!! http://www.unicon.net/node/740
2013-03-05 15:02 · java
Java的Generics的几点限制
参见 http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html To use Java generics effectively, you must consider the following restrictions: Cannot Instantiate Generic Types with Primitive Types Cannot Create Instances of Type Parameters Cannot Declare Static Fields Whose Types are Typ…
2012-12-28 15:00 · java
Overriding Method Using Parameter That is a Subclass?
参见 http://www.coderanch.com/t/389279/java/java/Overriding-Method-Parameter-Subclass 本来想用汉语写标题,发现实在不好说这个逻辑。。。 好吧,这个问题的答案是“ 这是overload不是override ”。 可以参考一下程序验证: public class Animal { public void talkTo(Animal a){ System.out.println("animal"); } } public class Cat extends Animal { public void talkTo(Ca…
2012-12-27 22:14 · java
Java的Generics和c++的Template到底有什么不同?
先了解Java的Generics: 根据Java的文档,Java引入Generics一是为了在编译时提供更强的类型检查,二是为了泛型编程。 编译时,Java靠type erasure来实现Generics: 1. 将所有的泛型参数替换为限定(bound这里如何翻译?)的类型,如果没有限定,就替换为Object类。因此然生的字节码和普通的类文件一样; 2. 为了保证类型安全,必要的时候会是使用cast; 3. 为了维护继承的泛型类型之间的多态的正确性,必要的时候会生成bridge methods。 考虑下面的泛型类,这个类代表单链表的节点。(一下例子均出自java doc) public cl…