小编Hit*_*esh的帖子

使用不同的加载器在JVM中加载两次类

我有一个关于类加载概念的问题.如何在JVM中加载.class文件两次.我也在编写一段代码,我已经写完了这个代码.

1)装载机1代码

public class MyClassLoader extends ClassLoader {

    public MyClassLoader(){
        super(MyClassLoader.class.getClassLoader());
    }

    public Class loadClass(String classname){
        try {
            return super.loadClass(classname);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

2)装载机2代码

public class AnotherClassLoader extends ClassLoader {

    public AnotherClassLoader(){
        super(AnotherClassLoader.class.getClassLoader());
    }

    public Class loadClass(String classname){
        try {
            return super.loadClass(classname);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

3)现在我使用这两个不同的类加载器加载一个名为A的类.我想操作classA1 == newClassA应该返回false.这是代码:

public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java classloader

5
推荐指数
3
解决办法
1万
查看次数

字符串常量池机制

谁能解释这种奇怪的行为String

这是我的代码:

String s2 = "hello";
String s4 = "hello"+"hello";
String s6 = "hello"+ s2;

System.out.println("hellohello" == s4);
System.out.println("hellohello" == s6);

System.out.println(s4);
System.out.println(s6);
Run Code Online (Sandbox Code Playgroud)

输出是:

true
false
hellohello
hellohello
Run Code Online (Sandbox Code Playgroud)

java string

4
推荐指数
3
解决办法
311
查看次数

当double值传递float值时,Double Precision

我有关于双精度的问题.当浮点值传递给double时,我会得到一些不同的结果.例如

float f= 54.23f;
double d1 = f;
System.out.println(d1);
Run Code Online (Sandbox Code Playgroud)

输出为54.22999954223633.有人可以解释这种行为背后的原因.它是否像两个十进制精度的默认值一样.

java floating-point double

3
推荐指数
1
解决办法
1333
查看次数

谁完成后会通知线程?

我有一个关于基于等待/通知的线程交互的问题.

以下代码的输出是Im.如何输出,Im因为没有其他线程调用notify()Thread对象.是否像JVM notify()在上面尝试等待Thread类实例的情况下隐式调用.

线程操作在没有收到任何通知的情况下等待时会卡住.现在如果我等待Thread类实例怎么办wait()?例如

public class WaitingThread {
    public static void main(String[] args) {
        Thread t1 = new Thread();
        t1.start();
        synchronized (t1) {
            try {
                t1.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Im");
    }
}
Run Code Online (Sandbox Code Playgroud)

java multithreading

3
推荐指数
1
解决办法
148
查看次数