3i Infotech Placement: Sample Questions 80 - 81 of 1245

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

Question 80

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;

}

}

Question 81

Edit

Write in Short

Short Answer▾

What is a wrapper classes?

Explanation

Define Wapper Classes Hierarchy
  • Wrapper class object wraps or contains primitive data types.
  • Object to a wrapper class contains a field that stores a primitive data type.

Developed by: