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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>ladyae</module>
<module>nikitarykov</module>
<module>duha666</module>
<module>sertem96</module>
</modules>

<dependencies>
Expand Down
74 changes: 74 additions & 0 deletions sertem96/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<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>

<parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>sertem96</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.4</version>
</dependency>

<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.6.3</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>

<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.7</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>

</dependencies>

<developers>
<developer>
<name>Serebryakov Artem</name>
<email>[email protected]</email>
</developer>
</developers>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.fizteh.fivt.students.sertem96.reverser;

public class Reverser {
public static void main(String[] args) {
for (int i = args.length - 1; i >= 0; --i) {
String[] parts = args[i].split("\\s+");
for (int j = parts.length - 1; j >= 0; --j) {
System.out.print(parts[j] + " ");
}
}
System.out.println();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package ru.fizteh.fivt.students.sertem96.threads;

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 int maxSize;
private Queue<T> queue;
private Lock queueLock = new ReentrantLock();
private Lock stateChanged = new ReentrantLock();
private Condition popWait = stateChanged.newCondition();
private Condition pushWait = stateChanged.newCondition();

BlockingQueue(int maxSize) {
this.maxSize = maxSize;
queue = new ArrayDeque<>();
}

// push_back to the queue
public void offer(List<T> e) {
try {
offer(e, 0, false);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

// pop_front from the queue
public List<T> take(int n) {
try {
return take(n, 0, false);
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}

public void offer(List<T> e, long timeout) throws InterruptedException {
offer(e, timeout, true);
}

public List<T> take(int n, long timeout) throws InterruptedException {
return take(n, timeout, true);
}

private long getTimeout(long timeLimit) throws InterruptedException {
long currTime = System.currentTimeMillis();
if (currTime > timeLimit) {
throw new InterruptedException("Timeout");
}
return timeLimit - currTime;
}

public void offer(List<T> e, long timeout, boolean needTimeout) throws InterruptedException {
long timeLimit = System.currentTimeMillis() + timeout;

if (needTimeout) {
if (!stateChanged.tryLock(getTimeout(timeLimit), TimeUnit.MILLISECONDS)) {
throw new InterruptedException("Timeout");
}
} else {
stateChanged.lock();
}

try {
boolean added = false;
while (!added) {
try {
if (needTimeout) {
if (!queueLock.tryLock(getTimeout(timeLimit), TimeUnit.MILLISECONDS)) {
throw new InterruptedException("Timeout");
}
} else {
queueLock.lock();
}

if (queue.size() + e.size() <= maxSize) {
queue.addAll(e);
added = true;
}
} finally {
queueLock.unlock();
}

if (!added) {
if (needTimeout) {
if (!popWait.await(getTimeout(timeLimit), TimeUnit.NANOSECONDS)) {
throw new InterruptedException("Timeout");
}
} else {
popWait.await();
}
}
}
} finally {
pushWait.signalAll();
stateChanged.unlock();
}
}

List<T> take(int n, long timeout, boolean needTimeout) throws InterruptedException {
long timeLimit = System.currentTimeMillis() + timeout;
if (needTimeout) {
if (!stateChanged.tryLock(getTimeout(timeLimit), TimeUnit.MILLISECONDS)) {
throw new InterruptedException("Timeout");
}
} else {
stateChanged.lock();
}

try {
List answer = new ArrayList<>();
while (answer.size() < n) {
try {
if (needTimeout) {
if (!queueLock.tryLock(getTimeout(timeLimit), TimeUnit.MILLISECONDS)) {
throw new InterruptedException("Timeout");
}
} else {
queueLock.lock();
}
if (queue.size() >= n) {
for (int i = 0; i < n; i++) {
answer.add(queue.poll());
}
}
} finally {
queueLock.unlock();
if (answer.size() == n) {
return answer;
}
}
if (answer.size() < n) {
if (needTimeout) {
if (!pushWait.await(getTimeout(timeLimit), TimeUnit.NANOSECONDS)) {
throw new InterruptedException("Timeout");
}
} else {
pushWait.await();
}
}
}
return answer;
} finally {
popWait.signalAll();
stateChanged.unlock();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.fizteh.fivt.students.sertem96.threads;

public class Counter {

private static final Object MONITOR = new Object();

private static int currentId;

private static class ThreadCount extends Thread {

private int id, nextId;

ThreadCount(int id, int nextId) {
this.id = id;
this.nextId = nextId;
}

@Override
public void run() {
while (true) {
synchronized (MONITOR) {
while (id != currentId) {
try {
MONITOR.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread-" + String.valueOf(id + 1));
currentId = nextId;
MONITOR.notifyAll();
}
}
}
}

public static void main(String[] args) {
int n;
try {
n = Integer.valueOf(args[0]);
if (n <= 0) {
throw new NumberFormatException();
}
} catch (Exception e) {
System.err.println("Wrong number of threads");
return;
}
currentId = 0;
for (int i = 0; i < n; i++) {
ThreadCount thread = new ThreadCount(i, (i + 1) % n);
thread.start();
}
}
}

Loading