Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Threading in Java: Object Locks - II

0.00/5 (No votes)
14 Jun 2021CPOL2 min read 3K  
This is in continuance with my previous article on Threading in Java: Object Locks - I
This series of articles makes an attempt to help readers with the concept of locks in Java. It is assumed that the reader has some prior knowledge of Java and threads creation in Java.

Introduction

This is a continuation of my previous article on "Threading in Java: Object Locks - I" where we had created threads out of the same processor class object "p1" and synchronized them with the "this" i.e., "p1" object itself. To some, it's difficult to understand the concept using "this". So let's add a small twist to the earlier program to better understand object locks.

Java
locks.java
===========

class processor implements Runnable{
    Object objectLock;
    public processor(){
        objectLock = new Object();
    }
    public void run() {
        display(); 
    }
    public void display() {
        synchronized(objectLock) {
            for (int i = 0 ; i<= 5; i++) {
                System.out.println("i = " + i + " In thread" + Thread.currentThread());
            }
        }   
    }
} 

class locks {
    public static void main(String[] args) {
        processor p1 = new processor();
        Thread t1 = new Thread(p1, "t1");
        Thread t2 = new Thread(p1, "t2");
        t1.start();
        t2.start();
    }
}

Output
======

i = 0 In threadThread[t1,5,main]
i = 1 In threadThread[t1,5,main]
i = 2 In threadThread[t1,5,main]
i = 3 In threadThread[t1,5,main]
i = 4 In threadThread[t1,5,main]
i = 5 In threadThread[t1,5,main]
i = 0 In threadThread[t2,5,main]
i = 1 In threadThread[t2,5,main]
i = 2 In threadThread[t2,5,main]
i = 3 In threadThread[t2,5,main]
i = 4 In threadThread[t2,5,main]
i = 5 In threadThread[t2,5,main]

The above program compiles and runs successfully to give the desired output.

We have kept the locks class as it was in the previous example. A slight change is added at the beginning of the class "processor". We have created an Object reference variable objLock. The default constructor of the processor class initializes it by creating an object of Object class. It is this "objectLock" that is used in the synchronized block by the threads to lock the critical area which is supposed to be accessed mutually.

Inside main(), when the processor object "p1" is created, its member objectLock is initialized via new Object(). Next two threads are created, t1 and t2 based on the same object p1". Thus both the threads "t1" and "t2" see one common object lock which is "objLock". The moment, "t1" and "t2" are started, both start to contend to acquire a lock in order to enter the synchronized block. Let's assume, "t1" acquires "objectLock" and enters the synchronized block. In this case, "t2" is not allowed entry into the synchronized block until "t1" completes the execution of the for loop and exits the synchronized block. While exiting the synchronized block, "t1" releases the "objectLock" which is then acquired by "t2" which then enters the synchronized block and completes the execution of the for loop to produce the desired output.

Thus, synchronization between two threads "t1" and "t2" occurs because of one "objectLock" acquired by each thread one at a time. Remember that "objectLock" belongs to "p1" and all the threads, viz "t1" and "t2" are created out of "p1".

Points of Interest

Thus "object lock" ensures that all the threads depending on the same object are synchronized.

History

  • 14th June, 2021: Initial version

Link to Part 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)