Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions projects/kitnoel/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>kitnoel</artifactId>
<version>1.0-SNAPSHOT</version>
<name>kitnoel</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<T> {

private Queue<T> 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<T>();
maxSize = size;
}

void offer(List<T> 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<T> take(int n) throws InterruptedException {
synchronized (takeSynchronizer) {
lock.lock();
List<T> ans = new ArrayList<T>();
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<T> 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<T> take(int n, long timeout) throws InterruptedException {
synchronized (takeSynchronizer) {
lock.lock();
List<T> ans = new ArrayList<T>();
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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.mipt.diht.students.kitnoel.treads;

/**
* Created by leonk on 19.12.15.
*/
public class Counter {

}
9 changes: 5 additions & 4 deletions projects/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.mipt.diht.students</groupId>
Expand Down Expand Up @@ -30,7 +30,8 @@
<module>dkhurtin</module>
<module>ale3otik</module>
<module>Pitovsky</module>
</modules>
<module>kitnoel</module>
</modules>

<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -133,4 +134,4 @@
</plugins>
</build>

</project>
</project>