作为类变量的对象会导致堆栈溢出
public class stack {
stack obj = new stack(); // its obvious that during class loading obj will call class to
// load and infinite loop will occur.
}
Run Code Online (Sandbox Code Playgroud)
让我们说我static在课堂上使用obj
public class stack {
static stack obj = new stack(); // it will not cause infinite loop and program will //execute successfully
}
Run Code Online (Sandbox Code Playgroud)
当JVM第一次捕获类时,静态变量被分配到内存中(据我所知).仅在JVM开始将内存分配给上述static对象变量时才说第一次.它会再次实际调用该类,这也应该导致无限循环.在某个地方我错了.有人可以突出我错在哪里.
我们可以使用直接运行方法,如下面的类所做.它产生与我们使用t1.start();时相同的结果.有没有理由只使用start方法来调用run?
public class runcheck extends Thread{
public void run(){
System.out.println(" i am run");
}
public static void main(String args[]){
runcheck as = new runcheck();
Thread t1 = new Thread(as);
t1.run();
}
}
Run Code Online (Sandbox Code Playgroud) 运行将从main方法开始,如果在某些方法中我使用this.wait()而没有synchronized块,它将抛出illegalStateMonitorException.
如果只有一个线程为什么不能等待,我们为什么要在这里使用锁定.线程也不会执行任何其他任务.它可以在那里等待.
我不明白为什么如果有单线程需要锁定.
public class waitSleepCheck extends Thread{
public void method() throws InterruptedException ,IllegalMonitorStateException{
this.wait(1000);
}
public static void main(String args[]) throws InterruptedException{
waitSleepCheck object = new waitSleepCheck();
object.method();
}
}
Run Code Online (Sandbox Code Playgroud) Set set = new HashSet();
set.add(1);
set.add(2)
Iterator iter = set.iterator(); // Wher hasnext been overrided
while(iter.hasnext()){
System.out.println(iter.next);
}
Run Code Online (Sandbox Code Playgroud)
Iterator是一个接口,set.iterator返回Iterator引用.在HashSet(及其扩展的,实现的类)中,hasnext无处覆盖.我怀疑的是hasnext方法被覆盖(实现)以及它在此上下文中的链接方式.
谢谢.
public class suspendCheck extends Thread{
int t ;
public static void main(String args[]) throws InterruptedException {
suspendCheck as = new suspendCheck();
Thread t2 = new Thread(as);
t2.start();
Thread t3 = new Thread(as);
t3.start(); }
public void run(){
if(t==0){
System.out.println("JAVA Develper");
t= ++t;
}
else{
System.out.println("JAVA Test");
}
}
}
Run Code Online (Sandbox Code Playgroud)
多次运行此程序后,存在差异和差异:
通常:
JAVA Develper
JAVA Develper
Run Code Online (Sandbox Code Playgroud)
并偶尔:
JAVA Develper
JAVA Test
Run Code Online (Sandbox Code Playgroud)
我对此感到震惊.我哪里做错了?