题:
package GoodQuestions;
public class MyClass {
MyClass() throws CloneNotSupportedException {
try {
throw new CloneNotSupportedException();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
MyClass obj = new MyClass();
MyClass obj3 = (MyClass)obj.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里的类'MyClass'可以通过调用'Object'类中的clone方法来克隆自己的对象.当我尝试在同一个包'GoodQuestions'中的另一个类('TestSingleTon')中克隆这个类('MyClass')时,它会抛出以下编译时错误.
'Object类型的方法clone()不可见 '
所以这是抛出上述错误的代码?
package GoodQuestions;
public class TestSingleTon {
public static void main(String[] args) {
MyClass obj = new MyClass();
MyClass obj3 = obj.clone(); ---> here is the compile error. …
Run Code Online (Sandbox Code Playgroud) 能否请您澄清代码有什么问题:
问:即使我没有将blinker声明为volatile,但是线程t1能够看到主线程设置的更新值(true)....
码:
package com.learning;
public class HowToStopRunningThread implements Runnable{
/**
* @param args
*/
public static boolean blinker;
public static void main(String[] args) {
Thread t = new Thread(new HowToStopRunningThread());
t.start();
HowToStopRunningThread obj = new HowToStopRunningThread();
obj.stop();
}
public void stop(){
try{
Thread.sleep(100);
System.out.println(“Setting the Blinker value”);
blinker = true;
}catch(InterruptedException ie){
ie.getMessage();
}
}
@Override
public void run() {
while(!blinker){
try{
System.out.println(“blinker:”+blinker);
Thread.sleep(1000);
}catch(InterruptedException ie){
ie.getMessage();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
blinker:false
Setting the Blinker value
Run Code Online (Sandbox Code Playgroud)
-----------然后线程从while循环中出来