DEV Community

Janardhan Pulivarthi
Janardhan Pulivarthi

Posted on • Updated on

Day 16 of 100 - Java: Multi-threading


volatile helps facilitate atomic operation

sample code to trigger multiple threads

class Runner extends Thread {

    public void run() {
        for(int i=0; i<10; i++) {
            System.out.println("Hello" + i);


public class App {

    public static void main(String[] args) {
        Runner runner1 = new Runner();

        Runner runner2 = new Runner();
Enter fullscreen mode Exit fullscreen mode

synchronized keyword to use intrinsic lock to wait till one thread completes.

    // Second thread have to wait until the first thread completes the
    // execution due to instrinsic lock
    public synchronized void increment() {
Enter fullscreen mode Exit fullscreen mode

Intrinsic locks

Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.

Every object has an intrinsic lock associated with it.

Project exercise repo -

Top comments (0)