Synchronization In Java Part 5

BIS
2 min readJan 25, 2020

In this article, we will explain intrinsic locks in Java for fairness strategy. The lockInterruptibly() locks the lock unless the thread calling the method has been interrupted.

lock()
lockInterruptibly()
tryLock()

tryLock(timeout, timeUnit)
unlock()

The various method allows making programming easier.

ReentrantReadWriteLock is mostly used for using reading and writing to a resource. It maintains a pair of associated locks one for read-only operations and one for writing. The advantage is that read lock allows multiple threads to access at the same time, provided write lock has not acquired it first. Whereas a write lock is exclusive.

While creating a lock it accepts an optional fairness parameter when set true to grant access to the longest waiting thread.

The tryLock() backs out after the expiry gives the advantage to take action in terms of system hangs up.

lockInterruptibly() backout if another thread interrupts it.

Example:
private final ReentrantReadWriteLock rw1 = new ReentrantReadWriteLock();
final Lock rl = rw1.readLock();
final Loc wl = rw1.writeLock();

Now in your code you can simply use both read and write locks separately.

public Object readExample() {

rl.Lock()

//Do reading here

rl.unLock()

}

public void writeExample(…) {

wl.Lock();

//do writing here

wl.unLock();

}

The locking mechanism is simplified and becomes an integral part of the multi-threading application. Multi-Threading is complicated and used with great care to avoid race-conditions, deadlock and live lock.

Please visit Business Integration Software to see various products using locking and synchronization.

Online Test Software

Exam Software

Examination Software

Question Bank Software

Online Exam Software

Online Test System

Online Examination Software

Online Assessment System

Assessment Management Software

Multiple Choice Question Software

Examination Management Software

Exam Management Software

Examination Management System

Exam Management System

Online Examination System

MCQ Software

Online Assessment Software

Test Management Software

Online Assessment Software

Exam System

Examination System

RHEED Software

MLM Software

Service Management Software

Online Timesheet Software

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

BIS
BIS

Written by BIS

Specialist in Cloud Native development

No responses yet

Write a response

Recommended from Medium

Lists

See more recommendations