From b14e6b255787cf7acd79f625e220341e63dc0cbc Mon Sep 17 00:00:00 2001 From: leonk Date: Sat, 19 Dec 2015 07:08:27 +0300 Subject: [PATCH 1/6] -- --- projects/kitnoel/pom.xml | 26 +++++++++++++++++++ .../students/kitnoel/reverser/Reverser.java | 16 ++++++++++++ projects/pom.xml | 9 ++++--- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 projects/kitnoel/pom.xml create mode 100644 projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/reverser/Reverser.java diff --git a/projects/kitnoel/pom.xml b/projects/kitnoel/pom.xml new file mode 100644 index 0000000..12b7d99 --- /dev/null +++ b/projects/kitnoel/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + ru.mipt.diht.students + parent + 1.0-SNAPSHOT + + ru.mipt.diht.students + kitnoel + 1.0-SNAPSHOT + kitnoel + http://maven.apache.org + + UTF-8 + + + + junit + junit + 3.8.1 + test + + + diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/reverser/Reverser.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/reverser/Reverser.java new file mode 100644 index 0000000..bb732bc --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/reverser/Reverser.java @@ -0,0 +1,16 @@ +package ru.mipt.diht.students.kitnoel.reverser; + +/** + * Created by leonk on 19.12.15. + */ +public class Reverser { + public static void main(String[] argv) { + for (int i = argv.length - 1; i >= 0; i--) { + String[] psd = argv[i].split("\\s"); + for (int j = psd.length - 1; j >= 0; j--) { + System.out.print(psd[j] + " "); + } + } + System.out.println(); + } +} diff --git a/projects/pom.xml b/projects/pom.xml index 5d14966..6df0818 100644 --- a/projects/pom.xml +++ b/projects/pom.xml @@ -1,5 +1,5 @@ - + + 4.0.0 ru.mipt.diht.students @@ -30,7 +30,8 @@ dkhurtin ale3otik Pitovsky - + kitnoel + @@ -133,4 +134,4 @@ - + \ No newline at end of file From 32337064d00a8b29775591e2b9be8c69d2604470 Mon Sep 17 00:00:00 2001 From: leonk Date: Sat, 19 Dec 2015 08:27:32 +0300 Subject: [PATCH 2/6] call --- .../diht/students/kitnoel/treads/Call.java | 79 +++++++++++++++++++ .../diht/students/kitnoel/treads/Counter.java | 46 +++++++++++ .../students/kitnoel/treads/Counter.java~ | 8 ++ 3 files changed, 133 insertions(+) create mode 100644 projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java create mode 100644 projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java create mode 100644 projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java~ diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java new file mode 100644 index 0000000..6326d1f --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java @@ -0,0 +1,79 @@ +package ru.mipt.diht.students.kitnoel.treads; + +/** + * Created by leonk on 19.12.15. + */ +import java.util.Random; + +public class Call { + + public static class Runner { + private static int curN = 0; + private static boolean finish = false; + private static boolean alive = true; + private static Random random = new Random(); + + private static class ThreadClass extends Thread { + private volatile int number; + + @Override + public void run() { + while (alive) { + if (curN + 1 == number) { + int x = random.nextInt(10); + if (x < 1) { + finish = false; + System.out.println("No"); + } else { + System.out.println("Yes"); + } + curN++; + } else { + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } + } + } + + ThreadClass(int num) { + this.number = num; + } + } + + void run(int n) { + random = new Random(); + curN = n; + ThreadClass[] threads = new ThreadClass[n]; + for (int i = 0; i < n; i++) { + threads[i] = new ThreadClass(i + 1); + threads[i].start(); + } + while (true) { + if (curN == n) { + if (finish) break; + finish = true; + System.out.println("Are you ready?"); + curN = 0; + } else { + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } + } + alive = false; + } + } + + public static void main(String[] args) { + int n = 0; + if (args.length > 0) { + n = Integer.valueOf(args[0]); + } + new Runner().run(n); + } +} diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java new file mode 100644 index 0000000..17b4399 --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java @@ -0,0 +1,46 @@ +package ru.mipt.diht.students.kitnoel.treads; + +/** + * Created by leonk on 19.12.15. + */ +public class Counter { + + private static volatile int currentID; + + private static Object synch = new Object(); + + private static class Runner implements Runnable { + private int id, size; + + + Runner(int id, int size) { + this.id = id; + this.size = size; + } + + @Override + public void run() { + while (true) try { + synchronized (synch) { + while (id != currentID) synch.wait(); + System.out.println("Thread-" + String.valueOf(id)); + currentID++; + if (currentID > size) currentID %= size; + synch.notifyAll(); + } + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + } + } + + public static void main(String[] args) { + int n; + n = Integer.valueOf(args[0]); + currentID = 1; + for (int i = 0; i < n; i++) { + Thread Runner = new Thread(new Runner(i + 1, n)); + Runner.start(); + } + } +} diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java~ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java~ new file mode 100644 index 0000000..6c8f809 --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Counter.java~ @@ -0,0 +1,8 @@ +package ru.mipt.diht.students.kitnoel.treads; + +/** + * Created by leonk on 19.12.15. + */ +public class Counter { + +} From 6c0ced5f4c15221d778ebae70c87982a592cd4ae Mon Sep 17 00:00:00 2001 From: leonk Date: Sat, 19 Dec 2015 08:38:50 +0300 Subject: [PATCH 3/6] blocking que --- .../kitnoel/treads/BlockingQueue.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java new file mode 100644 index 0000000..95f7780 --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java @@ -0,0 +1,39 @@ +package ru.mipt.diht.students.kitnoel.treads; + +/** + * Created by leonk on 19.12.15. + */ +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class BlockingQueue { + private int maxQueueSize; + private Queue queue; + BlockingQueue(int maxSize) { + maxQueueSize = maxSize; + queue = new LinkedList<>(); + } + public synchronized void offer(List L) throws InterruptedException { + while (queue.size() + L.size() > maxQueueSize) { + wait(); + Thread.sleep(100); + } + for (int i = 0; i < L.size(); ++i) { + queue.add(L.get(i)); + } + notifyAll(); + } + public synchronized List take(int n) throws InterruptedException { + while (queue.size() < n) { + wait(); + Thread.sleep(100); + } + List ans = new LinkedList<>(); + for (int i = 0; i < n; ++i) { + ans.add(queue.remove()); + } + notifyAll(); + return ans; + } +} From 8a98c629e0753dac84a6417562328bda857ac822 Mon Sep 17 00:00:00 2001 From: leonk Date: Sat, 19 Dec 2015 08:47:25 +0300 Subject: [PATCH 4/6] fix queue --- .../ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java index 95f7780..d8b6124 100644 --- a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java @@ -7,7 +7,7 @@ import java.util.List; import java.util.Queue; -public class BlockingQueue { +class BlockingQueue { private int maxQueueSize; private Queue queue; BlockingQueue(int maxSize) { @@ -35,5 +35,4 @@ public synchronized List take(int n) throws InterruptedException { } notifyAll(); return ans; - } -} + } \ No newline at end of file From e63fadfebaaa3d4da78d25f351f3bff1fee79c42 Mon Sep 17 00:00:00 2001 From: leonk Date: Sat, 19 Dec 2015 08:54:21 +0300 Subject: [PATCH 5/6] fix call --- .../diht/students/kitnoel/treads/Call.java | 116 +++++++++--------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java index 6326d1f..3a9ff9d 100644 --- a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java @@ -4,76 +4,78 @@ * Created by leonk on 19.12.15. */ import java.util.Random; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; public class Call { - public static class Runner { - private static int curN = 0; - private static boolean finish = false; - private static boolean alive = true; - private static Random random = new Random(); + private static volatile boolean allReady; + private static boolean finish; + private static volatile Random chance = new Random(); + private static CyclicBarrier beginAnswer; + private static CyclicBarrier endAnswer; - private static class ThreadClass extends Thread { - private volatile int number; + private static class Player extends Thread { - @Override - public void run() { - while (alive) { - if (curN + 1 == number) { - int x = random.nextInt(10); - if (x < 1) { - finish = false; - System.out.println("No"); - } else { - System.out.println("Yes"); - } - curN++; - } else { - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } - } + @Override + public void run() { + while (!finish) { + try { + beginAnswer.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + if (chance.nextInt(10) > 1) { + System.out.println("Yes"); + } else { + System.out.println("No"); + allReady = false; + } + try { + endAnswer.await(); + } catch (InterruptedException | BrokenBarrierException ex) { + ex.printStackTrace(); } - } - - ThreadClass(int num) { - this.number = num; } } - void run(int n) { - random = new Random(); - curN = n; - ThreadClass[] threads = new ThreadClass[n]; + + } + + public static void main(String[] args) { + if (args.length >= 1) { + finish = false; + int n = Integer.valueOf(args[0]); + beginAnswer = new CyclicBarrier(n + 1); + endAnswer = new CyclicBarrier(n + 1); + Player[] players = new Player[n]; for (int i = 0; i < n; i++) { - threads[i] = new ThreadClass(i + 1); - threads[i].start(); + players[i] = new Player(); + players[i].start(); } - while (true) { - if (curN == n) { - if (finish) break; - finish = true; - System.out.println("Are you ready?"); - curN = 0; - } else { - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - ex.printStackTrace(); + finish = false; + while (!finish) { + System.out.println("Are you ready?"); + allReady = true; + try { + beginAnswer.await(); + } catch (BrokenBarrierException | InterruptedException e) { + e.printStackTrace(); + } + beginAnswer.reset(); + try { + endAnswer.await(); + if (allReady) { + finish = true; + for (int i = 0; i < n; i++) { + players[i].join(); + } + System.exit(0); } + } catch (BrokenBarrierException | InterruptedException e) { + e.printStackTrace(); } } - alive = false; - } - } - - public static void main(String[] args) { - int n = 0; - if (args.length > 0) { - n = Integer.valueOf(args[0]); } - new Runner().run(n); } -} +} \ No newline at end of file From 68a297b60ceb82deaa1ad68e7769e7e0fc1e9c6a Mon Sep 17 00:00:00 2001 From: leonk Date: Sat, 19 Dec 2015 08:58:13 +0300 Subject: [PATCH 6/6] fix que 2 --- .../kitnoel/treads/BlockingQueue.java | 116 ++++++++++++++---- 1 file changed, 93 insertions(+), 23 deletions(-) diff --git a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java index d8b6124..2bfdee9 100644 --- a/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java @@ -3,36 +3,106 @@ /** * Created by leonk on 19.12.15. */ -import java.util.LinkedList; +import java.util.ArrayDeque; +import java.util.ArrayList; import java.util.List; import java.util.Queue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class BlockingQueue { -class BlockingQueue { - private int maxQueueSize; private Queue queue; - BlockingQueue(int maxSize) { - maxQueueSize = maxSize; - queue = new LinkedList<>(); + private int maxSize; + private final Lock lock = new ReentrantLock(); + private final Condition notEnoughSpace = lock.newCondition(); + private final Condition notEnoughElements = lock.newCondition(); + private final Object offerSynchronizer = new Object(); + private final Object takeSynchronizer = new Object(); + + BlockingQueue(int size) { + queue = new ArrayDeque(); + maxSize = size; } - public synchronized void offer(List L) throws InterruptedException { - while (queue.size() + L.size() > maxQueueSize) { - wait(); - Thread.sleep(100); + + void offer(List toAdd) throws InterruptedException { + synchronized (offerSynchronizer) { + lock.lock(); + try { + while ((queue.size() + toAdd.size()) > maxSize) { + notEnoughSpace.await(); + } + queue.addAll(toAdd); + notEnoughElements.signalAll(); + } finally { + lock.unlock(); + } } - for (int i = 0; i < L.size(); ++i) { - queue.add(L.get(i)); + } + + List take(int n) throws InterruptedException { + synchronized (takeSynchronizer) { + lock.lock(); + List ans = new ArrayList(); + try { + + while (queue.size() < n) { + notEnoughElements.await(); + } + for (int i = 0; i < n; ++i) { + ans.add(queue.remove()); + } + notEnoughElements.signalAll(); + } finally { + lock.unlock(); + return ans; + } } - notifyAll(); } - public synchronized List take(int n) throws InterruptedException { - while (queue.size() < n) { - wait(); - Thread.sleep(100); + + void offer(List toAdd, long timeout) throws InterruptedException { + synchronized (offerSynchronizer) { + lock.lock(); + long waitingTime = timeout; + final long startTime = System.currentTimeMillis(); + try { + while (queue.size() + toAdd.size() > maxSize && waitingTime > 0) { + notEnoughElements.await(waitingTime, TimeUnit.MILLISECONDS); + waitingTime = timeout - (System.currentTimeMillis() - startTime); + } + if (queue.size() + toAdd.size() <= maxSize) { + queue.addAll(toAdd); + notEnoughElements.notifyAll(); + } + } finally { + lock.unlock(); + } } - List ans = new LinkedList<>(); - for (int i = 0; i < n; ++i) { - ans.add(queue.remove()); + } + + List take(int n, long timeout) throws InterruptedException { + synchronized (takeSynchronizer) { + lock.lock(); + List ans = new ArrayList(); + long waitingTime = timeout; + final long startTime = System.currentTimeMillis(); + try { + while (queue.size() < n && waitingTime > 0) { + notEnoughElements.await(waitingTime, TimeUnit.MILLISECONDS); + waitingTime = timeout - (System.currentTimeMillis() - startTime); + } + if (queue.size() >= n) { + for (int i = 0; i < n; ++i) { + ans.add(queue.remove()); + } + notEnoughElements.notifyAll(); + } + } finally { + lock.unlock(); + return ans; + } } - notifyAll(); - return ans; - } \ No newline at end of file + } +} \ No newline at end of file