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
130 changes: 130 additions & 0 deletions projects/elinrin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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>

<parent>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>ru.mipt.diht.students</groupId>
<artifactId>ElinRin</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<name>ElinRin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>

<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>

<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>[4.0,)</version>
</dependency>

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

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

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>objenesis</artifactId>
<groupId>org.objenesis</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.1</version>
<scope>test</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.1</version>
<scope>test</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<artifactId>mockito-all</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>


<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>

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

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

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

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ru.mipt.diht.students.elinrin.threads;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class BlockingQueue<T> {
private int maxQueueSize;
private Queue<T> queue;
private Lock queueLock = new ReentrantLock();
private Lock offerLock = new ReentrantLock();
private Lock takeLock = new ReentrantLock();
private Object pushWait = new Object();
private Object popWait = new Object();

public final void offer(final List<T> list) {
offerLock.lock();
try {
Integer last = 0;
while (last != list.size()) {
synchronized (popWait) {
while (queue.size() == maxQueueSize) {
try {
popWait.wait();
} catch (InterruptedException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
try {
queueLock.lock();
while (last < Integer.min(last + maxQueueSize - queue.size(), list.size())) {
queue.add(list.get(last));
last++;
}
} finally {
queueLock.unlock();
}
}
}
} finally {
synchronized (pushWait) {
pushWait.notifyAll();
}
offerLock.unlock();
}
}

public final List<Object> take(final int n) {
takeLock.lock();
try {
List<Object> elements = new ArrayList<>();
while (elements.size() != n) {
synchronized (pushWait) {
while (queue.size() == 0) {
try {
pushWait.wait();
} catch (InterruptedException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
try {
queueLock.lock();
while (queue.size() > 0 && elements.size() != n) {
elements.add(queue.poll());
}
} finally {
queueLock.unlock();
}
}
}
return elements;
} finally {
synchronized (popWait) {
popWait.notifyAll();
}
takeLock.unlock();
}
}

public BlockingQueue(final int maxSize) {
maxQueueSize = maxSize;
queue = new ArrayDeque<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.mipt.diht.students.elinrin.threads;

import ru.mipt.diht.students.elinrin.threads.exception.HandlerOfException;


public class Counter {

private static volatile int printId;

public static void main(final String[] args) {
int number;
number = Parse.parse(args);

for (int id = 0; id < number; id++) {
CounterThread thread = new CounterThread(id, (id + 1) % number);
thread.start();
}
}


private static Object working = new Object();

private static class CounterThread extends Thread {
private int threadId, nextThreadId;

CounterThread(final int id1, final int id2) {
threadId = id1;
nextThreadId = id2;
}

@Override
public void run() {
while (true) {
synchronized (working) {
while (threadId != printId) {
try {
working.wait();
} catch (InterruptedException e) {
HandlerOfException.handler(e);
}
}
System.out.println("Thread-" + (threadId + 1));
printId = nextThreadId;
working.notifyAll();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ru.mipt.diht.students.elinrin.threads;


import ru.mipt.diht.students.elinrin.threads.exception.HandlerOfException;
import ru.mipt.diht.students.elinrin.threads.exception.UserException;

import java.util.Scanner;

import static java.lang.Thread.sleep;

public class Parse {

static final String USER_MOD = "user";
static final int SLEEP_TIME = 500;

private static int checkArgument(final String arguments) throws UserException {
int number;
try {
number = Integer.valueOf(arguments);
if (number <= 0) {
throw new UserException("Expected positive number");
} else {
return number;
}
} catch (NumberFormatException e) {
throw new UserException("Expected integer number");
}
}

public static int parse(final String[] args) {
int number = 0;
if (args.length != 1) {
try (Scanner in = new Scanner(System.in)) {
while (true) {
System.out.print("$ Counter ");
String arguments = in.nextLine().trim();
try {
number = checkArgument(arguments);
break;
} catch (UserException e) {
HandlerOfException.handler(e, USER_MOD);
try {
sleep(SLEEP_TIME);
} catch (InterruptedException e1) {
HandlerOfException.handler(e1);
}
}
}
}
} else {
try {
number = checkArgument(args[0]);
} catch (UserException e) {
HandlerOfException.handler(e);
}
}
return number;
}


}
Loading