多选题class Waiting implements Runnable {  boolean flag = false;  public synchronized void run() {  if (flag) {  flag = false;  System.out.print("1 ");  try { this.wait(); } catch (Exception e) { }  System.out.print("2 ");  }  else {  flag = true;  System.ou

题目
多选题
class Waiting implements Runnable {  boolean flag = false;  public synchronized void run() {  if (flag) {  flag = false;  System.out.print("1 ");  try { this.wait(); } catch (Exception e) { }  System.out.print("2 ");  }  else {  flag = true;  System.out.print("3 ");  try { Thread.sleep(2000); } catch (Exception e) { }  System.out.print("4 ");  notify();  }  }  public static void main(String [] args) {  Waiting w = new Waiting();  new Thread(w).start();  new Thread(w).start ();  }  }  以下哪两项是正确的?()
A

代码输出 1 3 4

B

代码输出 3 4 1

C

代码输出 1 2 3 4

D

代码不会完成


相似考题
参考答案和解析
正确答案: D,B
解析: 暂无解析
更多“class Waiting implements Runnable {  boolean flag = false; ”相关问题
  • 第1题:

    ( 18 )阅读下列程序

    Public class Test implements Runnable{

    Private int x=0;

    Private int y=o;

    boolean flag=true;

    Public static void main(string[ ] args) {

    Test r =new Test( );

    Thead t1=new Thead(r);

    Thead t2=new Thead(r);

    t1.start( );

    t2.start( );

    }

    Public void run(){

    While(flag) {

    x++;

    y++;

    system.out.println( “ ( ” +x_ “ , ” +y+ ” ) ” );

    if (x>=10)

    flag=false;

    }

    }

    }

    下列对程序运行结果描述的选项中,正确的是

    A)每行的( x,y )中,可能有;每一对( x,y )值都出现两次。

    B)每行的( x,y )中,可能有;每一对( x,y )值仅出现一次。

    C)每行的( x,y )中,可能有 x=y ;每一对( x,y )值都出现两次。

    D)每行的( x,y )中,可能有 x=y ;每一对( x,y )值都出现一次。


    正确答案:B

  • 第2题:

    如果一个正整数从高位到低位上的数字依次递减,则称其为降序数(如:9632是降序数,而8516则不是降序数)。现编写如下程序,判断输入的正整数是否为降序数。 Private Sub Command1 Click( ) Dim n As Long Dim flag As Boolean n=InputBox("输入一个正整数") S=Trim(Str(n)) For i=2 To Len(s) If Mid(s,i-1,1)<Mid(S,i,1)Then Exit For Next i If i=Len(S)Then flag=True Else flag=False If flag Then Print n;"是降序数" Else Print n;"不是降序数" End If End Sub 运行以上程序,发现有错误,需要对给flag变量赋值的If语句进行修改。以下正确的修改是( )。

    A.If i=Len(s)+1 Then flag=False Else flag=True

    B.If i=Len(s)+1 Then flag=True Else flag=False

    C.If i=Len(s)-1 Then flag=False Else flag=True

    D.If i=Len(s)-1 Then flag=True Else flag=False


    正确答案:B
    B。【解析】此题的考查点在for循环,当最后一次执行完Next的时候,i会越界。越界后是len(s)+1,已经比i的长度要长了。因此要对越界进行判断。

  • 第3题:

    下面程序段的输出结果为 public class Test { public static void main(String args[]) { boolean a,b,c; a=(3<5); b=(a==true); System.out.println("a="+a+"b="+B); c=(b==false); System.out.println("b="+b+"c="+C); } }

    A.a=true b=false b=true c=false

    B.a=true b=false b=true c=true

    C.a=true b=true b=true c=false

    D.a=false b=false b=true c=false


    正确答案:C
    解析:本题考查关系运算符和==。题目中a=(35);比较3和5的大小,因为 35,返回true给a;b=(a==true);判断a是否为真,因为a确实为真,返回true给b;c=(b==false);判断 b是否为假,因为b不为假,返回false给c。最后结果a=true,b=true,b=true,c=false,选项C正确。

  • 第4题:

    下列布尔变量定义中,正确并且规范的是

    A.BOOLEAN canceled=false;

    B.boolean canceled=false;

    C.boolean CANCELED=false;

    D.boolean canceled=FALSE;


    正确答案:B
    解析:本题考查Java的简单数据类型的变量定义及Java的命名约定。Java中的命名规则中包含如下几条:
      (1)变量名、方法名首单词小写,其余单词只有首字母大写;
      (2)常量完全大写;
      (3)变量命名采用完整的英文描述符,第一个字母小写,任何中间单词的首字母大写。
      Java语言区分大小写。简单数据类型布尔型用boolean表示。布尔型数据只有两个值: true(真)和false(假)。
      经过上述分析可知,选项B和选项C都能正确的定义一个布尔型变量,但是选项C的变量名CANCELED不符合Java中的命名规则,而选项B的变量名符合变量名的命名规则。因此,选项B是符合规范的布尔变量定义语句。
      本题的正确答案是选项B。

  • 第5题:

    下列变量定义不合法的是( )。

    A、boolean flag=false;

    B、int k=1+'k';

    C、char ch="c";

    D、float r=1/2;


    答案:D

  • 第6题:

    下列能够判断Spring容器是否包含ID为proBean的Bean的代码为( )。

    A.boolean flag = beanFactory.containsBean("proBean");

    B.PropertiesBean propertiesBean

    = (PropertiesBean)beanFactory.getBean("proBean");

    C.Class classType = beanFactory.getType("proBean");

    D.PropertiesBean propertiesBean

    = (PropertiesBean)beanFactory.getBean("proBean", PropertiesBean.class);


    参考答案:A

  • 第7题:

    现有:  class  Ifs  {  public  static void main (String  []  args)  {      boolean state=false;      int i=2;  if( (++i>2)  &&  (state=true))     i++;  if( (++i>4)  l l  (state=false))      i++;  System.out .println (i);     }     } 结果为:()     

    • A、  6
    • B、  5
    • C、  4
    • D、编译失败

    正确答案:A

  • 第8题:

    下列代码正确的是哪项?() 

    • A、 public class Session implements Runnable, Clonable{   public void run ();public Object clone () ; }
    • B、 public class Session extends Runnable, Cloneable {  public void run() {/*dosomething*/}       public Object clone() {/*make a copy*/} }
    • C、 public abstract class Session implements Runnable, Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}        }
    • D、 public class Session implements Runnable, implements Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}       }

    正确答案:C

  • 第9题:

    class Waiting implements Runnable {  boolean flag = false;  public synchronized void run() {  if (flag) {  flag = false;  System.out.print("1 ");  try { this.wait(); } catch (Exception e) { }  System.out.print("2 ");  }  else {  flag = true;  System.out.print("3 ");  try { Thread.sleep(2000); } catch (Exception e) { }  System.out.print("4 ");  notify();  }  }  public static void main(String [] args) {  Waiting w = new Waiting();  new Thread(w).start();  new Thread(w).start ();  }  }  以下哪两项是正确的?() 

    • A、代码输出 1 3 4
    • B、代码输出 3 4 1
    • C、代码输出 1 2 3 4
    • D、代码不会完成

    正确答案:A,B

  • 第10题:

    多选题
    Which statements concerning the relationships between the following classes are true?()   class Foo {  int num;   Baz comp = new Baz();   }   class Bar {  boolean flag;   }   class Baz extends Foo {   Bar thing = new Bar();   double limit;   }
    A

    A Bar is a Baz.

    B

    A Foo has a Bar.

    C

    A Baz is a Foo.

    D

    A Foo is a Baz.

    E

    A Baz has a Bar.


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

  • 第11题:

    多选题
    现有:  class Waiting implements Runnable  {       boolean flag=false;  public  synchronized void run()  {       if  (flag)  {       flag=false;  System.out.print ("1");  try  {  this.wait();  )  catch  (Exception e)  {  }       System.out.print ("2");       }  else  {       flag=true;  System.out.print ("3");  try{Thread.sleep (2000); } catch(Exception e)  {}      System.out.print ("4");       notify();       }       }  public static void main (String  []  args)  {       Waiting w=new Waiting();       new Thread (w) .start();       new Thread (w) .start();       }       }  以下哪两项是正确的?()
    A

    代码输出l 3 4

    B

    代码输出3 4 1

    C

    代码输出l 2 3 4

    D

    代码输出1 3 4 2

    E

    代码运行完毕

    F

    代码不会完成


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

  • 第12题:

    单选题
    现有:  interface Animal {       void eat () ;       }       //insert code here       public class HouseCat extends Feline {       public void eat() { }       }  和五个申明  abstract class Feline implements Animal { }  abstract  class  Feline  implements  Animal  {  void eat () ;  }  abstract class Feline implements Animal { public void eat();}  abstract class Feline implements Animal { public void eat() {}  }  abstract class Feline implements Animal { abstract public void eat();} 结果为:()
    A

    1

    B

    2

    C

    3

    D

    4


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

  • 第13题:

    本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入DataPool类中,消费者从中取出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。

    注意:不改变程序的结构,不得增行或删行。

    class DataPool

    {

    private int data;

    private boolean isFull;

    public DataPool()

    {

    isFull=false;

    }

    public synchronized void putData(int d)

    {

    if(isFull= =true)

    {

    try

    {

    this.notify();

    }

    catch(InterruptedException e)

    {}

    }

    data=d;

    isFull=true;

    System.out.println("生产了一个数据:"+data);

    this.notify();

    }

    public synchronized int getData()

    {

    if(isFull= =false)

    {

    try

    {

    this.wait();

    }

    catch(InterruptedException e)

    {}

    }

    isFull=false;

    System.out.println("消费了一个数据"+data);

    this.wait();

    return this.data;

    }

    boolean getIsFull()

    {

    return isFull;

    }

    }

    class Producer extends Thread

    {

    DataPool pool;

    public Producer(DataPool pool)

    {

    this.pool=pool;

    }

    public void run()

    {

    for(int i=0; i<10; i++)

    {

    int data=(int) (Math.random()*1000);

    try

    {//用于生产数据

    sleep(data);

    }

    catch(InterruptedException e)

    {}

    pool.putData(data);

    }

    }

    }

    class Consumer implements Runnable

    {

    DataPool pool;

    public Consumer(DataPool pool)

    {

    this.pool=pool;

    }

    public void run()

    {

    for(int i=0; i<10; i++)

    {

    int data=pool.getData();

    try

    {//用于处理数据

    sleep((int) (Math.random()*1000));

    }

    catch(InterruptedException e)

    {}

    }

    }

    }

    public class advance

    }

    public static void main(String[] args)

    {

    Data Pool pool=new Data Pool();

    Producer pro=new Producer(pool);

    Runnable con=new Consumer(pool);

    Thread conTh=new Thread(con);

    &n


    正确答案:this.wait() this.notify() thread.sleep((int)(Math.random()*1000))
    this.wait() this.notify() thread.sleep((int)(Math.random()*1000)) 解析:本题考查知识点:多线程同步与互斥、线程的概念和实现方法。解题思路:Data Pool是一个用来存放数据的缓冲池,其中可以存放一个血型数据,变量isFull作为标志量,标志该缓冲池中是否有数据。Put Data()方法负责向Data Pool中存放数据,本方法调用成功,缓冲池中就存入了数据,getData()方法负责从DataPool中获得数据,本方法调用成功,缓冲池就为空。Producer类负责产生随机数据,然后将数据存放到DataPool中。Consumer类负责才能够从DataPool中取出数据。Producer和Consumer共享同一个Data Pool对象。当某个线程进入synchronized块后,共享的数据并不一定能满足该线程的需要,这样线程就需要等待其他线程修改共享数据,直到满足需要以后才继续执行,但是当前线程必须释放锁以使得其他线程可以运行。wait()和notify()方法是实现线程之间通信的两个方法。wait()用来释放线程拥有的锁,使线程进入等待队列;notify()用来唤醒等待队列中的线程,并把它加入到申请锁的队列。本题中生产者在DataPool有数据的情况下需要进入等待消费者取出数据,所以需要调用wait()方法,因此第1个下划线的地方应该改为this.wait()。消费者线程在取出数据以后,必须通知生产者线程DataPool中已经没有数据了,因此第2个下划线的地方改为this.notify()。第3个下划线的地方,考查常用的线程类的使用。Runnable接口的目的是使任何类都可以为线程提供线程体,但它本身并不是线程,所以并不能直接调用Thread类的方法,需要改为 thread.sleep。

  • 第14题:

    boolean a=false;boolean b=true;boolean c=(a&&b)&&(!b);boolean result=(a&am

    boolean a=false; boolean b=true; boolean c=(a&&b)&&(!b); boolean result=(a&b)&(!b); 执行完后,正确的结果是( )。

    A.c=false;result=false

    B.c=true,result=true

    C.c=true;result=false

    D.c=false;result=true


    正确答案:A
    解析:本题考查Java中的运算。首先要清楚,“&&”是逻辑与运算符;“!”是逻辑非运算符;“&”是按位与运算符。按照逻辑运算符“a&&b”是false,“!b”是false,所以c是false。“a及b”是false,所以result是false。要注意区分“&&”和“&”,以及运算符之间的优先级关系,本题虽然没有涉及,但也要作为重点掌握。

  • 第15题:

    下面程序段: boolean a=false; boolean b=true; boolean c=(a||b)&&(b); boolean result=(a|b)&(b); 执行完后,正确的结果是

    A.c=false;result=false

    B.c=true,result=true

    C.c=true;result=false

    D.c=false;result=true


    正确答案:B
    解析:本题考查Java中的运算符。考试重点内容,历次考试都有题目涉及。首先要清楚,“&&”是逻辑与运算符;“&”是按位与运算符;“||”是逻辑或运算符;“|”是按位或运算符。“a||b”的结果为true,所以“true&&true”结果为true。而“a|b”的结果也为true,故result=(a|b)&(b)语句的结果也为true,选项B正确。

  • 第16题:

    下列关于Test类的定义中,正确的是( )。

    A.class Test implements Runnable{ public void run{} Dublic void someMethod[]{} }

    B.class Test implements Runnable( puIblic void run; }

    C.class Test implements Runnable( Dublic void someMethod[]; }

    D.class Test implements Runnable( public void someMethod{} }


    正确答案:A
    A。【解析】java中实现多线程的方法之一就是实现Runnable接口中的run方法,把实现Runnable接口的子类对象传递给Thread类的构造函数。

  • 第17题:

    下列程序通过实现Runnable接口创建一个线程,选择正确的语句填入程序的横线处。 class MyRun implements Runnable { String str; MyRun(String s) { str = s; } public void run() System.out.println(str); } } public class ex40 { public static void main(String[] args) { String name = "实现阶段Runnable 接口"; MyRun my = new MyRun(name); Thread th = th. start ( ); } }

    A.new MyRun(my)

    B.new Thread()

    C.new Thread(my)

    D.Thread(my)


    正确答案:C

  • 第18题:

    ( 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

  • 第19题:

    public class Alpha1 {  public static void main( String[] args ) {  boolean flag; int i=0;  do {  flag = false;  System.out.println( i++ );  flag = i < 10;  continue;  } while ( (flag)? true:false );  }  }  What is the result?()  

    • A、 000000000
    • B、 0123456789
    • C、 Compilation fails.
    • D、 The code runs with no output.
    • E、 The code enters an infinite loop.
    • F、 An exception is thrown at runtime.

    正确答案:B

  • 第20题:

    Which statements concerning the relationships between the following classes are true?()   class Foo {  int num;   Baz comp = new Baz();   }   class Bar {  boolean flag;   }   class Baz extends Foo {   Bar thing = new Bar();   double limit;   }  

    • A、A Bar is a Baz.
    • B、A Foo has a Bar.
    • C、A Baz is a Foo.
    • D、A Foo is a Baz.
    • E、A Baz has a Bar.

    正确答案:C,E

  • 第21题:

    class Test4 {   public static void main(String [] args) {   boolean x = true;   boolean y = false;   short z = 42;   if((z++ = = 42) && (y = true)) z++;   if((x = false) || (++z = = 45)) z++;   System.out.println("z = " + z);   }   }   结果为:()  

    • A、z = 42
    • B、z = 44
    • C、z = 45
    • D、z = 46

    正确答案:D

  • 第22题:

    单选题
    下列代码正确的是哪项?()
    A

     public class Session implements Runnable, Clonable{   public void run ();public Object clone () ; }

    B

     public class Session extends Runnable, Cloneable {  public void run() {/*dosomething*/}       public Object clone() {/*make a copy*/} }

    C

     public abstract class Session implements Runnable, Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}        }

    D

     public class Session implements Runnable, implements Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}       }


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

  • 第23题:

    单选题
    现有:  class  Test4  {  public static void main (String  []  args)  {    boolean X=true;   boolean y=false;    short Z=42;    if((z++==42)  &&  (y=true))z++;    if((x=false)  ||    (++z==45))  z++;    System. out.println(¨z=”+z);     }    }  结果为:()
    A

      Z=42

    B

      z=44

    C

      Z= 45

    D

      z= 46


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