@@ -0,0 +1,3065 @@
+package ru.apermyakov.waitnotify;%0A%0Aimport net.jcip.annotations.GuardedBy;%0Aimport net.jcip.annotations.ThreadSafe;%0A%0A/**%0A * Class for modulate simple lock.%0A *%0A * @author apermyakov%0A * @version 1.0%0A * @since 21.11.2017%0A */%0A@ThreadSafe%0Apublic class SimpleLock %7B%0A%0A    /**%0A     * Field for thread, that lock method.%0A     */%0A    @GuardedBy(%22threadLock%22)%0A    private Long lockerThread = -1L;%0A%0A    /**%0A     * Field for lock.%0A     */%0A    @GuardedBy(%22itself%22)%0A    private final Object threadLock = new Object();%0A%0A    /**%0A     * Method for lock method by some thread.%0A     */%0A    public void lock() %7B%0A        synchronized (threadLock) %7B%0A            if (lockerThread != Thread.currentThread().getId()) %7B%0A                while (lockerThread != -1L) %7B%0A                    try %7B%0A                        threadLock.wait();%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A                lockerThread = Thread.currentThread().getId();%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Method for unlock method by thread.%0A     */%0A    public void unlock() %7B%0A        synchronized (threadLock) %7B%0A            lockerThread = -1L;%0A            threadLock.notifyAll();%0A        %7D%0A    %7D%0A%0A    /**%0A     * Main method.%0A     *%0A     * @param args args%0A     */%0A    public static void main(String%5B%5D args) %7B%0A        SimpleLock lock = new SimpleLock();%0A%0A        Thread first = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                lock.lock();%0A%0A                try %7B%0A                    System.out.println(%221 Do smt with object%22);%0A                    Thread.sleep(1000);%0A                    System.out.println(%221 Work done%22);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D finally %7B%0A                    lock.unlock();%0A                %7D%0A            %7D%0A        %7D);%0A%0A        Thread second = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                lock.lock();%0A%0A                try %7B%0A                    System.out.println(%222 Do smt with object%22);%0A                    Thread.sleep(1000);%0A                    System.out.println(%222 Work done%22);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D finally %7B%0A                    lock.unlock();%0A                %7D%0A            %7D%0A        %7D);%0A%0A        Thread third = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                lock.lock();%0A%0A                try %7B%0A                    System.out.println(%223 Do smt with object%22);%0A                    lock.lock();%0A                    Thread.sleep(1000);%0A                    System.out.println(%223 Work done%22);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D finally %7B%0A                    lock.unlock();%0A                %7D%0A            %7D%0A        %7D);%0A%0A        first.start();%0A        second.start();%0A        third.start();%0A    %7D%0A%7D%0A%0A

Task 1100