Test.java Source code

Java tutorial

Introduction

Here is the source code for Test.java

Source

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
    public static void main(String[] args) {
        final Lock firstLock = new ReentrantLock();
        final Lock secondLock = new ReentrantLock();
        firstLock.lock();
        Thread secondThread = new Thread(new Runnable() {
            public void run() {
                secondLock.lock();
                firstLock.lock();
            }
        });
        secondThread.start();
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        secondLock.lock();

        secondLock.unlock();
        firstLock.unlock();
    }
}