-
Notifications
You must be signed in to change notification settings - Fork 36
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
3c9f495
b370f23
8e773c0
c8117d4
8fa4700
7e50290
e4b8b2f
c8e9d06
61d9824
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
twitter4j.properties |
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(); | ||
} | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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(), или может что-то еще, но потоки должны просыпаться по нашему указанию, а не т любого шороха.