在LINUX上,java线程对应LWP,LWP对应KLT
Thread count limit:$ cat /proc/sys/kernel/threads-max
How it is calculated:
max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
and:
x86_64 page size (PAGE_SIZE) is 4K;
Like all other architectures, x86_64 has a kernel stack for every active thread. These thread stacks are THREAD_SIZE (2*PAGE_SIZE) big;
for mempages :cat /proc/zoneinfo | grep spanned | awk ‘{totalpages=totalpages+$2} END {print totalpages}’;
so actually the number is not related with limitation of thread memory stack size (ulimit -s).
ps: thread memory stack limitation is 10M in my rhel VM, and for 1.5G memory, this VM can only afford 150 threads?
初始化顺序:
父类静态变量(静态初始化话块)> 子类静态变量(静态初始化块)> 父类普通变量(普通初始化块)> 父类构造方法 > 父类普通方法 > 子类普通变量(普通初始化块)> 子类构造方法 > 子类普通方法
构造方法中可以调用普通方法
##############################################
但接口不存在这样的问题,接口全都是抽象方法继承谁都无所谓,所以接口可以继承多个接口。
##############################################
s.getClass().getName().compareTo( "mypacakge.MyClass ") == 0 和 s instanceof MyClass
getClass返回的是真正的类名,而不是s引用的类型前者 s 必须是 MyClass的实例,即使是它的子类也不行!
后者 s 只要是MyClass或者它的子类就可以了
后者的效率高多了
##############################################
static 方法是类方法,它是不允许子类覆写(override)(可以重写,但@override不认,非多态,即Parent p = new Child(); p.staticX都调用Parent的内容) 而abstract方法是没有实现的方法,是必须由子类来覆写的,能这么设就矛盾了 static final public synchronized 可以混用 而且顺序不限 abstract 只能与public或protected连用,不能用default或private 也不能与static final synchronized中的任意一个连用 原因:abstract要继承(多态),但static的是编译期绑定,拒绝多态; synchronized没有继承性,父类加锁 子类默认不加锁 ,所以加了synchtonizd也没用;final本身就是阻止重写的 没有实际的需要把构造器定义成同步的,因为它将会在构造的时候锁住该对象,直到所有的构造器完成它们的工作,这个构造的过程对其它线程来说,通常是不可访问的 static 变量和方法都可以被继承并通过子类名来调用,有继承,无多态
##############################################
final字段必须显示初始化
##############################################
ThreadLocal, 取消变量的多线程共享:静态域 和 多线程同时运行一个线程对象中的对象域
##############################################
数组的实质在于外层的new调用:
Object[] objArr = {"abc", "de"} VS Object[] objArr = new String[]{"abc", "de"}
二者都编译通过String[] strArr = (String[])objArr;
但前者会在运行时因java.lang.ClassCastException失败
其实普通对象也一样:
Parent p = new Parent() VS Parent p = new Child()
二者都编译通过Child c = (Child)p;
但前者会在运行时因java.lang.ClassCastException失败
##############################################
居然可以这样:
List<?> strList = new ArrayList<String>();
((List<String>)strList).add("abc"); //List<?>是List<String>的超类
((List<Date>)strList).add(new Date());
System.out.println(strList); //echo [abc, Thu Feb 16 15:37:53 CST 2012]
涉及泛型的类型问题,如果编译时捉不到,那么运行时更捉不到(如果都是List)。同样道理:
public static <T> ArrayList<T> getArrayList(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(22);
ArrayList<T> r = (ArrayList<T>)list; //warning
return r;
}
ArrayList<String> strList = getArrayList();
strList.add("test");
System.out.println(strList); //echo [22, test]
如果要取出这个Integer,只能:
Object o = strList.get(0);
Integer i = (Integer)o;
对于Arrays类的 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)编译完全通过,运行时才报错:
Integer[] intArr = Arrays.copyOf(new String[]{"ccc", "ddd"}, 4, Integer[].class);
如果这样才编译报错:
Integer[] intArr = Arrays.copyOf(new String[]{"ccc", "ddd"}, 4);
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)如果两数组的元素都为引用型,则编译通过,但运行时会因src元素类型与dest数组实质类型不同而报错