Exercise of Thread(3)

博客围绕Java代码展开,涉及线程同步和异常问题。给出两段代码,第一段探讨如何确保数据完整性,第二段分析代码运行结果。指出线程调用wait()时若不属于对象锁,且方法同步,代码在运行时会发生异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

20.Given the following.

public class SyncTestInner{
     public static void main(String[] args) {
          Thread t = new Thread(){
           Foo f = new Foo();{
            f.increase(20);
                }
           };
           t.start();
       }
}


and assuming that data must be protected from corruption, what - if anything - can you add to the preceding code to ensure the integrity of data?


A.    Synchronize the run method.

B.    Warp a synchronize(this) around the call to f.increase().

C.    The existing code will not compile.

D.    The existing code will cause a runtime exception.

E.    Put in a wait() call prior to invoking the increase() method.

F.    Synchronize the increase() method.


(F).


21.Given the following.

1.    public class  Test{
2.         public static void main(String[] args) {
3.              final Foo f = new Foo();
4.              Thread t = new Thread(new Runnable()){
5.                   public void run(){
6.                        f.doStuff();
7.                    }
8.          }};
9.      Thread g = new Thread(){
10.           public void run(){
11.                f.doStuff();
12.        }
13.   };
14.      t.start();
15.      g.start();
16.         }
17.    }

1.    class Foo{
2.         int x = 5;
3.         public void doStuff(){
4.              if(x<10){
5.               //nothing to do
6.                   try{
7.                       wait();
8.                       }catch(InterruptedException ex){}
9.          }    else()    {
10.               System.out.println("x is " + x++);
11.               if(x>=10){
12.                    notify();
13.            }
14.       }
15.  }
16.}

what is the result?

A.The code will not compile because of an error on line 12 of class Foo.

B.The code will not compile because of an error on line 7 of class Foo.

C.The code will not compile because of an error on line 12 of class Test.

D.The code will not compile because of some other error in class Test.

E.An exception occurs at runtime.

F.x is 5
  x is 6


(E).因为线程在调用wait()时不属于对象的锁,如果方法是同步的,代码将在运行时会发生异常。


   public static void main(String[] args)                                                         
{                                                                                                                  
    new Thread()                                                                                           
        {                                                                                                          
          public void run(){System.out.println("Run");}                                      
        }.start();                                                                                               
    }                                                                                                              

此代码不会产生Compile Error!!


public class A                                                                                               
{                                                                                                                  
  private interface inA                                                                                    
  {                                                                                                                
    void show();                                                                                             
  }                                                                                                                
  /*                                                                                                               
  class clA implements inA                                                                            
  {                                                                                                                
   void show()                                                                                               
   {                                                                                                               
    // some great code here                                                                             
   }                                                                                                               
  }                                                                                                                
  */                                                                                                               
}                                                                                                                  


接口inA如果不想给其他class implement,则在这里mark private.
但是, 把inA mark private之后, 就变成只有A的inner class才能implement了.
因此, 在这样的情况下, inA实际上作用不大. 只是一种合法的 syntax 而已 .


boolean a =   Boolean.TRUE;                                                                       

此行代码是错误的,因为Boolean.TRUE是一个Boolean!!不是boolean!!

class Base extends Thread                                                                             
{                                                                                                                   
 static public void main(String[] a)                                                                   
 {                                                                                                                   
        Base anObj = new Base();                                                                       
        anObj.start();                                                                                          
     }                                                                                                               
}                                                                                                                    

编译不会出错,因为Thread类implements Runnable接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值