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/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..2bfdee9 --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/BlockingQueue.java @@ -0,0 +1,108 @@ +package ru.mipt.diht.students.kitnoel.treads; + +/** + * Created by leonk on 19.12.15. + */ +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 { + + private Queue queue; + 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; + } + + 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(); + } + } + } + + 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; + } + } + } + + 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 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; + } + } + } +} \ No newline at end of file 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..3a9ff9d --- /dev/null +++ b/projects/kitnoel/src/main/java/ru/mipt/diht/students/kitnoel/treads/Call.java @@ -0,0 +1,81 @@ +package ru.mipt.diht.students.kitnoel.treads; + +/** + * Created by leonk on 19.12.15. + */ +import java.util.Random; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class Call { + + 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 Player extends Thread { + + @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(); + } + } + } + + + } + + 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++) { + players[i] = new Player(); + players[i].start(); + } + 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(); + } + } + } + } +} \ No newline at end of file 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 { + +} 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