首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

几道有关问题,求大侠给出正解!

2012-01-08 
几道问题,求大侠给出正解!!1.What sthedifferencebetweenAandB?A.classTest{publicvoidtest(){synchronize

几道问题,求大侠给出正解!!
1.           What 's   the   difference   between   A   and   B?

A.

class   Test
{
public   void   test()
{
        synchronized(this)
        {
                ...
        }
}
}

B.

class   Test
{
      public   synchronized   void   test()

      {
              ...  
      }
}

2.           Why   the   init()   method   must   be   private?   Please   provide   an   example   to   explain   it.

class   Test
{
public   Test()
{
      init();
}
private   void   init()
{
      ......
}
}

3.           Find   problems   in   this   code   snippet:
class   Test
{
public   static   void   executeQuery(   String   connectionName,     String   statementText)   throws   SQLException
{
        Connection   connection   =   null;
        try  
{
        //Get   a   connection   from   connection   pool
                connection   =   manager.getConnection(connectionName);
                Statement   statement   =   connection.createStatement();
                ResultSet   resultSet   =     statement.executeQuery(statementText);
                for   (int   i   =   0;   resultSet.next();   i++)  
                {
                        ...
                }
                resultSet.close();
                statement.close();
                return   ;
        }
        catch   (SQLException   e)  
        {
                throw(new   SQLException(statementText));
        }
}
}


[解决办法]
"When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you specify which object 's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object.

Or you can synchronize on the current instance (this) as in the code above. Since that 's the same instance that synchronized methods lock on, it means that you could always replace a synchronized method with a non-synchronized method containing a synchronized block. In other words, this:



public synchronized void doStuff () {
System.out.println( "synchronized ");
}

is equivalent to this:

public void doStuff() {
synchronized(this) {
System.out.println( "synchronized ");
}
}

These methods both have the exact same effect, in practical terms. The compiled bytecodes may not be exactly the same for the two methods, but they could be—and any differences are not really important. The first form is shorter and more familiar to most people, but the second can be more flexible. "

Quoted from Chapter 9 of this book:
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055)
byKathy SierraandBert Bates
McGraw-Hill/Osborne 2006 (864 pages)
ISBN:0072253606

热点排行
Bad Request.