public class TestSeven extends Thread {  private static int x;  public synchronized void doThings() {  int current = x;  current++;  x = current;  }  public void run() {  doThings();  }  }  Which is true?() A、 Compilation fails.B、 An exception is thrown a

题目

public class TestSeven extends Thread {  private static int x;  public synchronized void doThings() {  int current = x;  current++;  x = current;  }  public void run() {  doThings();  }  }  Which is true?() 

  • A、 Compilation fails.
  • B、 An exception is thrown at runtime.
  • C、 Synchronizing the run() method would make the class thread-safe.
  • D、 The data in variable “x” are protected from concurrent access problems.
  • E、 Declaring the doThings() method as static would make the class thread-safe.
  • F、 Wrapping the statements within doThings() in a synchronized(new Object()) {} block would make the class thread-safe.
参考答案和解析
正确答案:E
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

interface A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}


正确答案:

 

错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

以通过A.x 来明确。

第2题:

1.class TestSuper { 22.TestSuper(int i) { } 3. } 4.class TestSub extends TestSuper{ } 5.class TestAll { 6.public static void main (String [] args) { 7.new TestSub(); 8.} 9.} Which is true?()  

  • A、 Compilation fails.
  • B、 The code runs without exception.
  • C、 An exception is thrown at line 7.
  • D、 An exception is thrown at line 2.

正确答案:A

第3题:

本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import java.awt.*; import java.awt.event.*; import javax swing.*; class MousePanel extends JPanel extends MouseMo- tionListener {public MousePanel {addMouseListener(new MouseAdapter {public void mousePressed(MouseEvent evt) {int X=evt.getX; int Y=evt.getY; current=find(x,y); if(current<0) add(x,y); } public void mouseClicked(MouseEvent evt) {int X=evt.getX; int Y=evt.getY; if(evt.getClickCount>=2) {remove(current); } } }); addMouseMotionListener(this); } public void paintComponent(Graphics g) {super.paintComponent; for(int i=0;i<nsquares;i++) draw(g,i); } public int find(int X,int y) (for(int i=0;i<nsquares;i++) if(squares[i].x-SQUARELENGTH/2<= x X<=squares[i].x+SQuARELENGTH/2 squares[i].Y-SQUARELENGTH/2< =Y y<=squares[i].Y+SQUARELENGTH /2) return i ; return-1 ; } public void draw(Graphics g,int i) {g.drawRect(squares[i].X-SQUARE- LENGTH/2。 squares[i].Y-SQUARELENGTH/2, SQUARELENGTH, SQUARELENGTH); } public void add(int X,int Y) {if(nsquares<MAXNSQUARES) {squares[nsquares]=new Point(x,y); current=nsquares ; nsquares++; repaint; } } public void remove(int n) {if(n<0 ‖ n>=nsquares)return; Nsquares- -; squares[n]=squares[nsquares]; if(current= =n)current= -l; repaint; } public void mouseMoved(MouseEvent evt) {} public void mouseDragged(MouseEvent evt) {} private static final int SQUARELENGTH=10: private static final int MAXNSQUARES=100; private Point[]squares=new Point[MAX- NSQUARES]; private int nsquares=0; private int current=-l; } class MouseFrame. extends JFramc {public MouseFrame {setTitle("java3"); setSize(300,200); addWindowListener(new WindowAdapter {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane; contentPane.add(MousePanel); } } public class java3 {public static void main(String[]args) {JFrame. frame=new MouseFrame; frame.show; } }


正确答案:
第l处:extends JPanel implements MouseMotionListener.
第2处:super.paintComponent(g)
第3处:contentPane.add(new MousePanel)
【解析】第1处是继承Jpanel实现鼠标移动监听器接口;第2处以g为参数重新绘制组件;第3处在contentPane内容面板中添加一个MousePanel鼠标面板。

第4题:

public class SyncTest (  private int x;  private int y;  private synchronized void setX (int i) (x=1;)  private synchronized void setY (int i) (y=1;)  public void setXY(int 1)(set X(i); setY(i);)  public synchronized Boolean check() (return x !=y;)  )  Under which conditions will check () return true when called from a different class?   

  • A、 Check() can never return true.
  • B、 Check() can return true when setXY is called by multiple threads.
  • C、 Check() can return true when multiple threads call setX and setY separately.
  • D、 Check() can only return true if SyncTest is changed to allow x and y to be setseparately.

正确答案:B

第5题:

public class SyncTest {  private int x;  private int y;  private synchronized void setX( int i ) { x = i; }  private synchronized void setY( int i ) { y = i; }  public void setXY( int i ) { setX(i); setY(i); }  public synchronized boolean check() { return x != y; }  }   Under which condition will check return true when called from a different class? () 

  • A、 check can never return true.
  • B、 check can return true when setXY is called by multiple threads.
  • C、 check can return true when multiple threads call setX and setY separately.
  • D、 check can return true only if SyncTest is changed to allow x and y to be set separately.

正确答案:B

第6题:

public class Threads5 {  public static void main (String[] args) {  new Thread(new Runnable() {  public void run() {  System.out.print(”bar”);  } }).start();  }  }  What is the result?() 

  • A、 Compilation fails.
  • B、 An exception is thrown at runtime.
  • C、 The code executes normally and prints “bar”.
  • D、 The code executes normally, but nothing prints.

正确答案:C

第7题:

Which are syntactically valid statement at// point x?()     class Person {     private int a;  public int change(int m){  return m;  }     }  public class Teacher extends Person {     public int b;  public static void main(String arg[]){     Person p = new Person();     Teacher t = new Teacher();    int i;  // point x     }    } 

  • A、 i = m;
  • B、 i = b;
  • C、 i = p.a;
  • D、 i = p.change(30);
  • E、 i = t.b.

正确答案:D,E

第8题:

下列哪个方法可用于创建一个可运行的类? ( )

A.public class X implements Runable {public void run(){...,.,}}

B.public class X implements Thread {public void run(){......}}

C.public class X implements Thread {public int run(){……}}

D.public class X implements Runable {protected void run(){.....}}


正确答案:A

第9题:

interface DeclareStuff{  public static final int EASY = 3;  void doStuff(int t); }  public class TestDeclare implements DeclareStuff {  public static void main(String [] args) {  int x=5;  new TestDeclare().doStuff(++x);  }  void doStuff(int s) {  s += EASY + ++s;  System.out.println(”s “ + s);  }  }  What is the result?() 

  • A、 s 14
  • B、 s 16
  • C、 s 10
  • D、 Compilation fails.
  • E、 An exception is thrown at runtime.

正确答案:D

第10题:

public class A extends Thread {  A() {  setDaemon(true);  }  public void run() {  (new B()).start();  try {  Thread.sleep(60000);  } catch (InterruptedException x) {}  System.out.println(“A done”);  }  class B extends Thread {  public void run() {  try {  Thread.sleep(60000);  } catch (InterruptedException x) {}  System.out.println(“B done”);  }  }  public static void main(String[] args) {  (new A()).start();  }  }   What is the result?()  

  • A、 A done
  • B、 B done
  • C、 A done B done
  • D、 B done A done
  • E、 There is no exception that the application will print anything.
  • F、 The application outputs “A done” and “B done”, in no guaranteed order.

正确答案:B

更多相关问题