Languages-Java [3i Infotech Placement]: Sample Questions 12 - 13 of 141

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 12

Edit

Describe in Detail

Essay▾

What are adapter classes

Explanation

The Object Adapter Pattern
  • Provides the default implementation of an interface.
  • A class inheriting from adapter class is not forced to provide the implementation of all the methods of listener interface.
  • Useful for processing only few of the events- provide definition of only few functions in the interface relevant to us.

Advantages of the Adapter Class

  • Assists bringing together unrelated classes.
  • Increases transparency of classes.
  • Allows including related patterns in a class.
  • Provides a pluggable kit for developing applications.
  • Makes an interface highly reusable.

Example of adapter class:

Example of Adapter Class

Question 13

Edit

Describe in Detail

Essay▾

How does thread synchronization occur in a monitor?

Explanation

Understanding of Java Monitor
  • A monitor defines a lock and condition variables for managing concurrent access to shared data.
  • The monitor uses the lock to ensure that only a single thread is active in the “monitored” code at any time.
  • Always only one thread can lock an object to gain access to critical section.
  • Following example illustrates the code.

Example

class MethodLevel

{

SharedResource x, y;

public synchronized void method ()

{

// multiple threads can not access

}

public synchronized void method2 ()

{

// multiple threads can՚t access

}

public void method3 ()

{

// not synchronized

// multiple threads can access

}

}

class BlockLevel

{

// shared among threads

SharedResource x, y;

// dummy objects for locking

Object xLock = new Object () , yLock = new Object () ;

public void method1 ()

{

synchronized (xLock)

{

// access x here. thread safe

}

// do something here but don՚t use

SharedResource x, y;

synchronized (xLock)

{

synchronized (yLock)

{

// access x, y here. thread safe

}

}

// do something here but don՚t use

SharedResource x, y;

}

}

Developed by: