小编use*_*089的帖子

为什么同步方法允许多个线程同时运行?

我在同一个文件中有以下程序.我已经同步了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)

java multithreading synchronized

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

变量后面的+符号是什么意思?

以下代码的输出是什么
int x,a=3;
x=+ +a+ + +a+ + +5;
printf("%d %d",x,a);

输出是:11 3.我想知道怎么样?一个手段后+符号是什么?

c c++

5
推荐指数
2
解决办法
2311
查看次数

以下java程序中的stackoverflow错误

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)

java

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

类的实例是如何递归创建的,从而导致stackoverflow错误

考虑以下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对象被初始化,反之亦然?

java

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

标签 统计

java ×3

c ×1

c++ ×1

multithreading ×1

synchronized ×1