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));
}
}
}
{
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));
}
}
}
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
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055)
byKathy SierraandBert Bates
McGraw-Hill/Osborne 2006 (864 pages)
ISBN:0072253606