我在同一个文件中有以下程序.我已经同步了run()方法.
class MyThread2 implements Runnable {
Thread t;
MyThread2(String s) {
t=new Thread(this,s);
t.start();
}
public synchronized void run() {
for (int i=0;i<3;i++) {
System.out.println("Thread name : "+ Thread.currentThread).getName());
try {
t.sleep(1000);
}
catch (InterruptedException e) {
e.getMessage();
}
}
}
}
class TestSync {
public static void main(String[] args) {
MyThread2 m1=new MyThread2("My Thread 1");
c.fun();
}
}
class c {
static void fun() {
MyThread2 m1=new MyThread2("My Thread 4");
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
Thread name : My Thread 1 …
Run Code Online (Sandbox Code Playgroud) 以下代码的输出是什么
int x,a=3;
x=+ +a+ + +a+ + +5;
printf("%d %d",x,a);
输出是:11 3.我想知道怎么样?一个手段后+符号是什么?
public class testing {
testing t=new testing();
public static void main(String args[]){
testing t1=new testing();
t1.fun();
}
void fun(){
int a=2;
int b=t.fun2(a);
System.out.println(a+" "+b);
}
int fun2(int a)
{
a=3;
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码会出现以下错误?我只是想知道原因,因为StackOverFlowError
在这种情况下很难预料到错误.
Exception in thread "main" java.lang.StackOverflowError
at com.testing.<init>(testing.java:4)
at com.testing.<init>(testing.java:4)
Run Code Online (Sandbox Code Playgroud) 考虑以下2个程序给出相同的错误
First calss:
public class Testing {
Testing t=new Testing();
public static void main(String args[]){
testing t1=new testing();
}
}
Run Code Online (Sandbox Code Playgroud)
二等:
class foo{
void baz(){
new testing();
}
}
public class testing {
testing t=new testing();
public static void main(String args[]){
foo f=new foo();
f.baz();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码如何给出以下错误?
我知道类的实例是递归创建但我想知道如何?
Exception in thread "main" java.lang.StackOverflowError
at com.Testing.<init>(Testing.java:4)
at com.Testing.<init>(Testing.java:4)
Run Code Online (Sandbox Code Playgroud)
如果我们这样做,为什么不这样呢?
public class testing {
testing t2=new testing();
testing t1=new testing();
public static void main(String args[])
{//anything}
}
Run Code Online (Sandbox Code Playgroud)
因为t1将要求t2对象被初始化,反之亦然?