Quick actions

cmd+k|ctrl+k

Navigation

Languages

"Protected" resource in main

Snippet info

Language

Java

Visibility

public

Author

orochi-100

Created

2020-02-25T14:38:22Z

Updated

2023-11-13T10:10:02.931421Z

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

/**
 * This class contains a main-method that demonstrates some locking-mechanism over multiple <code>Thread</code>s
 * 
 * @author orochi-100
 * @version 25.02.2020 
 * 
 */
public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println("Hello World!");
        
        ReentrantLock lock = new ReentrantLock();
        
        int a = 5;
        
        for (int i = 0; i < 4; i++)
            new Thread(() ->
            {
                System.out.println("asd");
                lock.lock();
                System.out.println(a + "\n");
                
                try
                {
                    TimeUnit.SECONDS.sleep(1);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    lock.unlock();
                }
                
            }).start();
    }
}
INFO