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
78 changes: 78 additions & 0 deletions projects/DPetrukhin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<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>DPetrukhin</artifactId>
<version>1.0-SNAPSHOT</version>
<name>DPetrukhin</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--<mainClass>ru.mipt.diht.students.DPetrukhin.twitterstream.Main.class</mainClass>-->
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

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

<!-- import Twitter library -->
<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.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package ru.mipt.diht.students.dpetrukhin.threads;

import java.util.*;

/**
* Created by daniel on 19.12.15.
*/

public class BlockingQueue<T> {
private Object queueAccessSyncObj = new Object();
private Object actionSyncObj = new Object();
private int maxQueueSize;
private int queueSize;
private volatile long currentOfferCounter;
private volatile long currentTakeCounter;
private long offerCounter;
private long takeCounter;
private List<T> queue;

public BlockingQueue(final int newMaxQueueSize) {
maxQueueSize = newMaxQueueSize;
currentOfferCounter = 0;
currentTakeCounter = 0;
offerCounter = 0;
takeCounter = 0;
queueSize = 0;
queue = new LinkedList<>();
}

public final void offer(final List<T> list) {
offer(list, 0);
}

public final void offer(final List<T> list, final long timeout) {
final boolean existTimeLimit;
if (timeout > 0) {
existTimeLimit = true;
} else {
existTimeLimit = false;
}
long timeToStop = System.currentTimeMillis() + timeout;
long timeToSleep;

if (list.size() > maxQueueSize) {
return;
}
long orderNumber;

synchronized (actionSyncObj) {
orderNumber = offerCounter++;
if (offerCounter == Long.MAX_VALUE) {
offerCounter = 0;
}
}

try {
synchronized (actionSyncObj) {
while (true) {
if (currentOfferCounter == orderNumber) {
if (list.size() + queueSize <= maxQueueSize) {
synchronized (actionSyncObj) {
queue.addAll(list);
queueSize += list.size();
}

++currentOfferCounter;
actionSyncObj.notifyAll();
throw new InterruptedException("");
}
}
actionSyncObj.notifyAll();
if (existTimeLimit) {
timeToSleep = timeToStop - System.currentTimeMillis();
if (timeToSleep <= 0) {
++currentOfferCounter;
actionSyncObj.notifyAll();
throw new InterruptedException("");
}
actionSyncObj.wait(timeToSleep);
} else {
actionSyncObj.wait();
}
}
}
} catch (InterruptedException e) {
return;
}
}

public final List take(final int qnt) {
return take(qnt, 0);
}

public final List take(final int qnt, final long timeout) {
final boolean existTimeLimit;
if (timeout > 0) {
existTimeLimit = true;
} else {
existTimeLimit = false;
}
long timeToStop = System.currentTimeMillis() + timeout;
long timeToSleep;

if (qnt == 0) {
return new LinkedList<>();
}

if (qnt > maxQueueSize) {
return null;
}

long orderNumber;

synchronized (actionSyncObj) {
orderNumber = takeCounter++;
if (takeCounter == Long.MAX_VALUE) {
takeCounter = 0;
}
}

try {
synchronized (actionSyncObj) {
while (true) {
if (currentTakeCounter == orderNumber) {
if (qnt <= queueSize) {
List answer;
synchronized (actionSyncObj) {
answer = new LinkedList<>(queue.subList(0, qnt));
queue.subList(0, qnt).clear();
queueSize -= qnt;
}

++currentTakeCounter;
actionSyncObj.notifyAll();
return answer;
}
}
actionSyncObj.notifyAll();
if (existTimeLimit) {
timeToSleep = timeToStop - System.currentTimeMillis();
if (timeToSleep <= 0) {
++currentTakeCounter;
actionSyncObj.notifyAll();
throw new InterruptedException("");
}
actionSyncObj.wait(timeToSleep);
} else {
actionSyncObj.wait();
}
}
}
} catch (InterruptedException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.mipt.diht.students.dpetrukhin.threads;

/**
* Created by daniel on 19.12.15.
*/
final class Counter {
private Counter() {
}

private static int countLast = 0;
private static int totalNumber;

private static final int TIME = 1000;

private static Object syncObj = new Object();

private static void runner(final int myNumber) {
Thread thread = new Thread() {
@SuppressWarnings("checkstyle.magicnumber")
@Override
public void run() {
try {
synchronized (syncObj) {
while (true) {
if (myNumber == countLast) {
System.out.print("Thread-" + myNumber + "\n");
++countLast;
if (countLast == totalNumber) {

Thread.sleep(TIME); //just for view
countLast = 0;
}
syncObj.notifyAll();
} else {
syncObj.wait();
}
}
}
} catch (InterruptedException e) {
}
}
};

thread.start();
}

public static void main(final String[] arg) {
totalNumber = new Integer(arg[0]);
for (int i = 0; i < totalNumber; ++i) {
runner(i);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ru.mipt.diht.students.dpetrukhin.threads;

import java.util.Random;

/**
* Created by daniel on 19.12.15.
*/

final class Rollcall {
private Rollcall() {
}

private static Random random = new Random();
private static int counter = 0;
private static int answers = 0;
private static boolean exitFlag = false;
private static int totalNumber;

public static final int TIME = 1000;
public static final int PROBABILITY = 10;

private static Object syncObj = new Object();

private static void startThread() {
Thread thread = new Thread() {
@SuppressWarnings("checkstyle.magicnumber")
@Override
public void run() {
try {
synchronized (syncObj) {
while (true) {
if (exitFlag) {
throw new InterruptedException("");
}
if (counter < totalNumber) {

if (random.nextInt(PROBABILITY) == 0) {
System.out.print("No\n");
} else {
System.out.print("Yes\n");
++answers;
}
++counter;
syncObj.notifyAll();
}

syncObj.wait();
}
}
} catch (InterruptedException e) {
return;
}
}
};

thread.start();
}

public static void main(final String[] arg) {
totalNumber = new Integer(arg[0]);
counter = totalNumber;
for (int i = 0; i < totalNumber; ++i) {
startThread();
}

try {
synchronized (syncObj) {
while (true) {
if (counter == totalNumber) {
if (answers < totalNumber) {
System.out.print("Are you ready?\n");
answers = 0;
counter = 0;
Thread.sleep(TIME); //just for view
} else {
exitFlag = true;
syncObj.notifyAll();
throw new InterruptedException("");
}
syncObj.notifyAll();
} else {
syncObj.wait();
}
}
}
} catch (InterruptedException e) {
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.mipt.diht.students.dpetrukhin.twitterstream;

/**
* Created by daniel on 19.12.15.
*/
public class GeolocationSearch {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.mipt.diht.students.dpetrukhin.twitterstream;

/**
* Created by daniel on 19.12.15.
*/
public class JCommanderTwitterStream {
}
Loading