根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。public void test(int i){lock(this){if (i>10){i--;test(i);}}}

题目

根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。

public void test(int i)

{

lock(this)

{

if (i>10)

{

i--;

test(i);

}

}

}


相似考题
更多“根据线程安全的相关知识,分析以下代码,当调用test方法时i10时是否会引起死锁?并简要说明理由。public void test(int i){lock(this){if (i10){i--;test(i);}}}”相关问题
  • 第1题:

    已知如下类说明: public class Test { private float f = 1.0f; int m = 12; static int n=1; public static void main(String arg[]) { Test t = new Test(); // 程序代码… } } 如下哪个使用是正确的?()

    A.t.f

    B.this.n

    C.Test.m

    D.Test.n


    正确答案:AD

  • 第2题:

    下列代码的执行结果是public class Test{ public int aMethod(){ static int i=0; i++; System.out.println(i); } public static void main(String args[]){ Test test= new Test(); test. aMethod(); }}

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出


    正确答案:A
    解析:static不能修饰局部变量。

  • 第3题:

    下面代码的运行结果是 public class Test{ public static void main(String args[]){ for(int i=0; i<3;i++){ if(i<2) continue; System.out.println(i); } } }

    A.0

    B.1

    C.2

    D.3


    正确答案:C
    解析:本题考查简单的Java控制语句。题目非常简单,但还是应该细心。这里应注意continue语句。continue语句是跳过循环体中下面尚未执行的语句,回到循环体的开始继续下一轮的循环。当然,在下一轮循环开始前,要先进行终止条件的判断,以决定是否继续循环。对于for语句,在进行终止条件的判断前,还要先执行迭代语句。题目所给程序中,当i=0和i=1时,都会执行continue语句,而不会执行System.out.println(i)语句,只有当i=2时才执行System.out.println(i)语句,输出为2,选项C正确。

  • 第4题:

    下面程序段的输出结果是( )。 public class Test { public static void main (String[] args) { int j=2,i=5; while (j<i--) j++; System.out.println(j);} }

    A.2

    B.3

    C.4

    D.5


    正确答案:C
    解析:循环时,首先判断结束条件,25,然后i=4,j=3,继续循环,i=3,j=4,结果条件ji为假,退出循环,因此j=4。所以选C。

  • 第5题:

    下列程序的输出结果是 class Demo { void test( ) { Systeme.out.pnnt("NO");} void test(int i) { System.out.print(a);} void test(int a,int b) { System.out.print(a+b);} } class Test { public static void main(String args[ ] ) { Demo de=new Demo( ); de.test( ); de.test(5); de.test(6,8); } }

    A.No 5 6 8

    B.5 6 8 No

    C.No 5 14

    D.8 6 No 5


    正确答案:C
    解析:本题考查的是方法重载的概念及应用,本题中应顺调查用test(),test(5)和test(6,8)方法,所以答案为选项C)。

  • 第6题:

    下列程序的输出结果是( )。 public class fff{ void printValue (int m) { do{System.out.println("The value is”+m): } while (--m>10) } public static void main(String arg []){ int i=10; Test t=new Test(); t.printValue(i); } }

    A.8

    B.9

    C.10

    D.11


    正确答案:C

  • 第7题:

    如下程序编译时发生错误,错误的原因是show函数实现语句错误,则正确的语句应该为______。

    include<iostream.h>

    class test

    {

    private:

    int hum;

    public:

    test(int);

    void show( );

    };

    test::test(int n){num=n;}

    test::show( ){cout<<num<<endl;}

    void main( )

    {

    test T(10):

    T.show( );

    }


    正确答案:void test::show( ){coutnumendl;}
    void test::show( ){coutnumendl;} 解析:show成员函数的声明和实现不一致,即实现部分应有void修饰符,这样才能编译通过。

  • 第8题:

    public class Test {  public static void leftshift(int i, int j) {  i<<=j;  }  public static void main(String args[])  {  int i = 4, j = 2;  leftshift(i, j);   System.out.printIn(i); }  }     What is the result?()  

    • A、 2
    • B、 4
    • C、 8
    • D、 16
    • E、 The code will not compile.

    正确答案:B

  • 第9题:

    public class Test {  public int aMethod() {  static int i = 0;  i++;  return i;  }  public static void main (String args[]) {  Test test = new Test();  test.aMethod();  int j = test.aMethod();  System.out.println(j);  }  }  What is the result?()  

    • A、 0
    • B、 1
    • C、 2
    • D、 Compilation fails.

    正确答案:D

  • 第10题:

    Consider the following class:     class Test(int i) {     void test(int i) {  System.out.println(“I am an int.”); }    void test(String s) {   System.out.println(“I am a string.”);     }          public static void main(String args) {    Test t=new Test();     char ch=“y”;    t.test(ch);     }      }     Which of the statements below is true?()

    • A、 Line 5 will not compile, because void methods cannot be overridden.
    • B、 Line 12 will not compile, because there is no version of test() that rakes a charargument.
    • C、 The code will compile but will throw an exception at line 12.
    • D、 The code will compile and produce the following output: I am an int.
    • E、 The code will compile and produce the following output: I am a String.

    正确答案:D

  • 第11题:

    单选题
    interface foo {   int k = 0;   }    public class test implements Foo (   public static void main(String args) (    int i;   Test test = new test ();   i= test.k;   i= Test.k;   i= Foo.k;   )   )   What is the result? ()
    A

     Compilation succeeds.

    B

     An error at line 2 causes compilation to fail.

    C

     An error at line 9 causes compilation to fail.

    D

     An error at line 10 causes compilation to fail.

    E

     An error at line 11 causes compilation to fail.


    正确答案: D
    解析: 暂无解析

  • 第12题:

    单选题
    Given the following code:    public class Test {  void printValue(int m){  do {  System.out.println("The value is"+m);     }  while( --m > 10 )     }  public static void main(String arg[]) {     int i=10;  Test t= new Test();     t.printValue(i);     }     }  Which will be output?()
    A

     The value is 8

    B

     The value is 9

    C

     The value is 10

    D

     The value is 11


    正确答案: B
    解析: 此题考察的是do… while循环和 -- 操作符的知识,do…while最少被执行一次,在执行完do中的内容后判断while中的条件是否为true,如果为true的话就再执行do中的内容,然后再进行判断,以此类推直到while的判断为false时退出循环执行循环后面的内容,而—操作符的规则是在变量右边的-- 将先进行运算,然后才是使变量的值减一,而在变量左边的是先将变量的值减一再运算。

  • 第13题:

    下列程序的执行结果是 ( ) public class Test { public int aMethod() { satic int i=0; i++; System.out.println(i); } public static void.main(String args[]) { Test test=new Test(); test.aMethod(); }

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出


    正确答案:A

  • 第14题:

    在下列源代码文件Test.java中,正确定义类的代码是( )。

    A.pblic class test { public int x=0; public test(int x) { this. x=x;} }

    B.public class Test { public int x=0; public Test(int x) { this. x=x;} }

    C.public class Test extends T1,T2{ public int x = 0; public Test(int x){ this. x = x; } }

    D.protected class Test extends T2{ public int x = 0; public Test(int x) { this. x = x; } }


    正确答案:B
    解析:本题主要考查类声明格式为[修饰符]class类名[extends父类名][implements类实现的接口列表],选项A中源文件名与程序名不相同,Java不支持多重继承所以选项C错误,选项D中类的访问权限不对,应为public。

  • 第15题:

    在下面程序的下画线处应填入的选项是 public class Test______{ public static void main(String args[]) { Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run() { for(int i=0;i<5;i++) System.out.println("i="+i); } }

    A.implements Runnable

    B.extends Thread

    C.implements Thread

    D.extends Runnable


    正确答案:A
    解析:创建线程有两种方法:实现java.lang.Runnahle接口和继承Thread类并重写run()方法。从创建线程实例的语句Thread tt=new Thread(t);可以看出本程序将Test类的实例t作为参数传递给Thread类的构造方法,因此是通过实现Runnable接口创建线程。

  • 第16题:

    下列代码的执行结果是( )。

    public class Test{

    public int aMethod( ){

    static int i=0;

    i++;

    System.out.println(i):

    }

    public static void main (String args[]){

    Trest test=new Test ( );

    test aMethod( ):

    }

    }

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出

    B.

    C.

    D.


    正确答案:A

  • 第17题:

    下列程序的输出结果是 ( ) class Derao { void test() { Systeme.out.print("NO");} void test (int i) {System.out.print(a);} void test(int a,int b) {System.out.print(a+b);} } class Test { public static void main(String args[]) { Demo de=new Demo(); de.test(); de.test5.; de.test(6,8); } }

    A.No568

    B.568No

    C.No514

    D.86No5


    正确答案:C

  • 第18题:

    考虑如下类:

    1. class Test(int i) {

    2. void test(int i) {

    3. System.out.println("I am an int.");

    4. }

    5. void test(String s) {

    6. System.out.println("I am a string.");

    7. }

    8.

    9. public static void main(String args[]) {

    10. Test t=new Test();

    11. char ch="y";

    12. t.test(ch);

    13. }

    14. }

    以下哪条为真?

    A.行 5 不能通过编译,方法不能被覆盖.

    B.行 12 不能通过编译, 因为没有一个test()方法含字符参数.

    C.代码可以编译但在12行将出现异常.

    D.代码可以编译且产生如下输出: I am an int.

    E.代码可以编译且产生如下输出: I am a String.


    正确答案:D

  • 第19题:

    ( 30 )在程序的下划线处应填入的选项是

    public class Test _________{

    public static void main(String args[]){

    Test t = new Test();

    Thread tt = new Thread(t);

    tt.start();

    }

    public void run(){

    for(int i=0;i<5;i++){

    system.out.println( " i= " +i);

    }

    }

    }

    A ) implements Runnable

    B ) extends Thread

    C ) implements Thread

    D ) extends Runnable


    正确答案:A

  • 第20题:

    public class test(    public int aMethod()[   static int i=0;   i++;   return I;   )    public static void main (String args){   test test = new test();    test.aMethod();   int j = test.aMethod();   System.out.printIn(j);   ]  }   What is the result?()

    • A、 Compilation will fail.
    • B、 Compilation will succeed and the program will print “0”
    • C、 Compilation will succeed and the program will print “1”
    • D、 Compilation will succeed and the program will print “2”

    正确答案:D

  • 第21题:

    Given the following code:    public class Test {  void printValue(int m){  do {  System.out.println("The value is"+m);     }  while( --m > 10 )     }  public static void main(String arg[]) {     int i=10;  Test t= new Test();     t.printValue(i);     }     }  Which will be output?()    

    • A、 The value is 8
    • B、 The value is 9
    • C、 The value is 10
    • D、 The value is 11

    正确答案:C

  • 第22题:

    interface foo {   int k = 0;   }    public class test implements Foo (   public static void main(String args) (    int i;   Test test = new test ();   i= test.k;   i= Test.k;   i= Foo.k;   )   )   What is the result? ()

    • A、 Compilation succeeds.
    • B、 An error at line 2 causes compilation to fail.
    • C、 An error at line 9 causes compilation to fail.
    • D、 An error at line 10 causes compilation to fail.
    • E、 An error at line 11 causes compilation to fail.

    正确答案:A

  • 第23题:

    单选题
    public class Test {  public int aMethod() {  static int i = 0;  i++;  return i;  }  public static void main (String args[]) {  Test test = new Test();  test.aMethod();  int j = test.aMethod();  System.out.println(j);  }  }  What is the result?()
    A

     0

    B

     1

    C

     2

    D

     Compilation fails.


    正确答案: D
    解析: 暂无解析