小编sek*_*har的帖子

来自对象的方法clone()不可见?

题:

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)

java methods clone cloneable

45
推荐指数
2
解决办法
4万
查看次数

线程能够看到java中主线程设置的更新静态变量

能否请您澄清代码有什么问题:

问:即使我没有将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循环中出来

java static multithreading volatile

0
推荐指数
1
解决办法
217
查看次数

标签 统计

java ×2

clone ×1

cloneable ×1

methods ×1

multithreading ×1

static ×1

volatile ×1