Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Анна Власова, 496: Threads + CQL + CQLex + TwitterStream + ModuleTests #72

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 projects/annnvl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twitter4j.properties
59 changes: 59 additions & 0 deletions projects/annnvl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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>annnvl</artifactId>
<version>1.0-SNAPSHOT</version>
<name>annnvl</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>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
</dependency>

<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.7</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ru.mipt.diht.students.annnvl.Threads.BlockingQueue;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BlockingQueue<T> {
private final int maxQueueSize;
private Queue<T> data;

BlockingQueue(int size) {
maxQueueSize = size;
data = new LinkedList<T>();
}

public final int size() {
return data.size();
}

synchronized void offer(List<T> e) {
while (data.size() + e.size() > maxQueueSize) {
Thread.yield();
}
data.addAll(e);
}

synchronized void offer(List<T> e, long timeout) {
timeout += System.currentTimeMillis();
while ((data.size() + e.size() > maxQueueSize) && (timeout - System.currentTimeMillis()) > 0) {
Thread.yield();
}
if (timeout > 0) {
data.addAll(e);
}
}

synchronized List<T> take(int n) {
while (data.size() < n) {
Thread.yield();
}
List<T> answer = new ArrayList<T>();
for (int i = 0; i < n; i++) {
answer.add(data.remove());
}
return answer;
}

synchronized List<T> take(int n, long timeout) {
timeout += System.currentTimeMillis();
while ((data.size() < n) && (timeout - System.currentTimeMillis()) > 0) {
Thread.yield();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это слишком общая функция, поток будет просыпаться чаще чем нужно.
Для синхронизации лучше использовать блоки synchronized и методы
Object.wait(long timeout) / notifyAll(), или может что-то еще, но потоки должны просыпаться по нашему указанию, а не т любого шороха.

}
if (timeout > 0) {
List<T> answer = new ArrayList<T>();
for (int i = 0; i < n; i++) {
answer.add(data.remove());
}
return answer;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mipt.diht.students.annnvl.Threads.Counter;

public class CountedThread extends Thread {
private static int numberOfThreads;
private static volatile int currentThread = 0;
private final int myNumber;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сначала пишут все поля, потом все методы, исправьте и в других местах


CountedThread(int mynum) {
myNumber = mynum;
}

public static void setNumberOfThreads(int num) {
numberOfThreads = num;
}

@Override
public final void run() {
while (true) {
while (myNumber != currentThread) {
Thread.yield();
}
System.out.println("Thread-" + (myNumber + 1) + "\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

println за вас строчку переводит, не нужно \n добавлять

currentThread = (currentThread + 1) % numberOfThreads;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.mipt.diht.students.annnvl.Threads.Counter;

public class Counter {
public static void main(String[] args) {
if (args.length < 1) {
throw new IllegalArgumentException("Please type number of threads");
}
int threads = Integer.parseInt(args[0]);
CountedThread.setNumberOfThreads(threads);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно в одну строчку, незачем расписывать

for (int i = 0; i < threads; i++) {
(new CountedThread(i)).start();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.mipt.diht.students.annnvl.Threads.RollCall;

public class RollCall {
public static void main(String[] args) {
if (args.length < 1) {
throw new IllegalArgumentException("Please type number of threads");
}
int threads = Integer.parseInt(args[0]);
RollCallerThread.setNumberOfThreads(threads);
for (int i = 0; i < threads; i++) {
(new RollCallerThread()).start();
}
while (true) {
RollCallerThread.makeEverybodyOk();
System.out.println("Are you ready?\n");
RollCallerThread.nextRollCall();
while (!RollCallerThread.everybodyasked()) {
Thread.yield();
}
if (RollCallerThread.isEverybodyOk()) {
RollCallerThread.yes();
RollCallerThread.nextRollCall();
return;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.mipt.diht.students.annnvl.Threads.RollCall;

import java.util.Random;

public class RollCallerThread extends Thread {
private static final int HUNDREED = 100;
private static final double LOWERBOUND = 0.1;
private static int numberOfThreads;
private static volatile int asked = 0;
private static volatile int timesShouldBe = -1; //какой раз мы проводим опрос
private static boolean yes4all = false;
private static boolean everybodyOk = true;
private Random rand;
private int timesNum; //какой раз мы вызываем конкретный процесс

RollCallerThread() {
rand = new Random(HUNDREED);
timesNum = 0;
}

public static void setNumberOfThreads(int num) {
numberOfThreads = num;
}

public static void yes() {
yes4all = true;
}

public static boolean isEverybodyOk() {
return everybodyOk;
}

public static void makeEverybodyOk() {
everybodyOk = true;
}

public static void nextRollCall() {
timesShouldBe++;
asked = 0;
}

public static boolean everybodyasked() {
return (asked == numberOfThreads);
}

@Override
public final void run() {
while (true) {
synchronized (this) {
while (timesNum != timesShouldBe) {
Thread.yield();
}
if (yes4all) {
return;
}
timesNum++;
if (rand.nextDouble() < LOWERBOUND) {
System.out.println("No\n");
everybodyOk = false;
} else {
System.out.println("Yes\n");
}
asked++;
}
}
}
}
20 changes: 11 additions & 9 deletions projects/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<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>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>pom</packaging>

<name>fizteh-java-2015</name>
<url>https://github.com/KhurtinDN/fizteh-java-2015</url>
Expand All @@ -28,8 +29,9 @@

<modules>
<module>dkhurtin</module>
<!--Add you module here-->
</modules>
<module>annnvl</module>
<!--Add you module here-->
</modules>

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

</project>
</project>