LINUX.ORG.RU

История изменений

Исправление DiKeert, (текущая версия) :

Вот код

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    private static final CountDownLatch latch = new CountDownLatch(1);

    static class CompletionMonitor {
        private final CountDownLatch latch = new CountDownLatch(1);

        public void complete() {
            latch.countDown();
        }

        public void await() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean await(long timeout, TimeUnit unit) {
            try {
                return latch.await(timeout, unit);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    static class Worker implements Runnable {
        private final CompletionMonitor monitor;
        private final boolean wait;

        public Worker() {
            this.monitor = new CompletionMonitor();
            wait = false;
        }

        public Worker(CompletionMonitor monitor) {
            this.monitor = monitor;
            wait = true;
        }

        public CompletionMonitor getMonitor() {
            return monitor;
        }

        @Override
        public void run() {
            if(wait) {
                System.out.println(Thread.currentThread().getName() + ": Waiting for completion");
                monitor.await();
                System.out.println(Thread.currentThread().getName() + ": Wait completed, working now.");
            } else {
                System.out.println(Thread.currentThread().getName() + ": Starting right now");
                System.out.println(Thread.currentThread().getName() + ": Working!");
                try {
                    Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    //we really can't be fucked processing that exception
                } finally {
                    System.out.println(Thread.currentThread().getName() + ": Completing!");
                    monitor.complete();
                }
            }
        }
    }


    public static void main(String[] args) {
        ExecutorService e = Executors.newFixedThreadPool(2);
        Worker worker1 = new Worker();
        Worker worker2 = new Worker(worker1.getMonitor());
        e.submit(worker1);
        e.submit(worker2);
    }
}

Исправление DiKeert, :

Вот код

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    private static final CountDownLatch latch = new CountDownLatch(1);

    static class CompletionMonitor {
        private final CountDownLatch latch = new CountDownLatch(1);

        public void complete() {
            latch.countDown();
        }

        public void await() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean await(long timeout, TimeUnit unit) {
            try {
                return latch.await(timeout, unit);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    static class Worker implements Runnable {
        private final CompletionMonitor monitor;
        private final boolean wait;

        public Worker() {
            this.monitor = new CompletionMonitor();
            wait = false;
        }

        public Worker(CompletionMonitor monitor) {
            this.monitor = monitor;
            wait = true;
        }

        public CompletionMonitor getMonitor() {
            return monitor;
        }

        @Override
        public void run() {
            if(wait) {
                System.out.println(Thread.currentThread().getName() + ": Waiting for completion");
                monitor.await();
                System.out.println(Thread.currentThread().getName() + ": Wait completed, working now.");
            } else {
                System.out.println(Thread.currentThread().getName() + ": Starting right now");
                System.out.println(Thread.currentThread().getName() + ": Working!");
                try {
                    Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    //we really can't be fucked processing that exception
                } finally {
                    System.out.println(Thread.currentThread().getName() + ": Completing!");
                    monitor.complete();
                }
            }
        }
    }


    public static void main(String[] args) {
        ExecutorService e = Executors.newFixedThreadPool(2);
        Worker worker1 = new Worker();
        Worker worker2 = new Worker(worker1.getMonitor());
        e.submit(worker1);
        e.submit(worker2);
    }
}

Исходная версия DiKeert, :

Вот код

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

    private static final CountDownLatch latch = new CountDownLatch(1);

    static class CompletionMonitor {
        private final CountDownLatch latch = new CountDownLatch(1);

        public void complete() {
            latch.countDown();
        }

        public void await() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        public boolean await(long timeout, TimeUnit unit) {
            try {
                return latch.await(timeout, unit);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    static class Worker implements Runnable {
        private final CompletionMonitor monitor;
        private final boolean wait;

        public Worker() {
            this.monitor = new CompletionMonitor();
            wait = false;
        }

        public Worker(CompletionMonitor monitor) {
            this.monitor = monitor;
            wait = true;
        }

        public CompletionMonitor getMonitor() {
            return monitor;
        }

        @Override
        public void run() {
            if(wait) {
                System.out.println(Thread.currentThread().getName() + ": Waiting for completion");
                monitor.await();
                System.out.println(Thread.currentThread().getName() + ": Wait completed, working now.");
            } else {
                System.out.println(Thread.currentThread().getName() + ": Starting right now");
                System.out.println(Thread.currentThread().getName() + ": Working!");
                try {
                    Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    //we really can't be fucked processing that exception
                } finally {
                    System.out.println(Thread.currentThread().getName() + ": Completing!");
                    monitor.complete();
                }
            }
        }
    }


    public static void main(String[] args) {
        ExecutorService e = Executors.newFixedThreadPool(2);
        Worker worker1 = new Worker();
        Worker worker2 = new Worker(worker1.getMonitor());
        e.submit(worker1);
        e.submit(worker2);
    }
}