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

<build>
<finalName>Threads</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>ru.fizteh.fivt.students.bulgakova.Threads.Mainclass</mainClass>
<packageName>ru.fizteh.fivt.students.bulgakova.Threads</packageName>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.fizteh.fivt.students.bulgakova.Threads;

import javax.naming.LimitExceededException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.locks.ReentrantLock;


public class BlockingQueue<E> {

private int maxSize;
private Queue<E> elements;
private ReentrantLock locker;


public BlockingQueue(int _size) {
maxSize = _size;
locker = new ReentrantLock();
elements = new LinkedList<E>();
}

void offer(List<E> e) throws LimitExceededException {
if(e.size() + elements.size() <= maxSize) {
locker.lock();

for(E oneElem : e) {
elements.add(oneElem);
}
locker.unlock();
} else {
throw new LimitExceededException();
}
}

List<E> take(int n) throws LimitExceededException {
if(elements.size() >= n) {
locker.lock();

List<E> returningList = new ArrayList<E>();
for(int i = 0; i < n; i++) {
returningList.add(elements.remove());
}

locker.unlock();
return returningList;
} else {
throw new LimitExceededException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ru.fizteh.fivt.students.bulgakova.Threads;

import javax.naming.LimitExceededException;
import java.util.ArrayList;
import java.util.List;


public class Mainclass {

public static void main(String args[]) throws InterruptedException {

for (int i = 0; i < 3; i++) {
Rhymes rhymes = new Rhymes(5);
}


Muster muster = new Muster(5);
muster.Asking();


BlockingQueue<Integer> blockingQueue = new BlockingQueue<Integer>(5);
List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);

List<Integer> myList2;
try {
blockingQueue.offer(list1);
myList2 = blockingQueue.take(2);
for(Integer i : myList2) {
System.out.print(i + " ");
}
} catch (LimitExceededException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ru.fizteh.fivt.students.bulgakova.Threads;

import java.util.ArrayList;
import java.util.Random;


public class Muster {
private boolean allThreadsAreReady;
private int threadsCount;
private ArrayList<Boolean> answers;

public class AnsweringFlow extends Thread{
private int index;

public AnsweringFlow(int _index) {
index = _index;
}

@Override
public void run() {
double random = new Random().nextDouble();
if(random > 0.1) {
System.out.println("Yes");
answers.set(index, Boolean.TRUE);
} else {
System.out.println("No");
answers.set(index, Boolean.FALSE);
}
}
}

public Muster(int _threadsCount) {
threadsCount = _threadsCount;
}

public void Asking() throws InterruptedException{
allThreadsAreReady = false;


answers = new ArrayList<Boolean>();
for(int i = 0; i < threadsCount; i++) {
answers.add(false);
}


while (!allThreadsAreReady) {
ArrayList<AnsweringFlow> answeringThreads = new ArrayList<AnsweringFlow>();
System.out.println("\nAre you ready?");

for(int i = 0; i < threadsCount; i++) {
answeringThreads.add(new AnsweringFlow(i));
}

for(AnsweringFlow answeringFlow: answeringThreads) {
answeringFlow.run();
}

for(AnsweringFlow answeringFlow: answeringThreads) {
try {
answeringFlow.join();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}

allThreadsAreReady = true;
for(Boolean answer : answers) {
if(!answer) {
allThreadsAreReady = false;
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.fizteh.fivt.students.bulgakova.Threads;

import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static java.lang.Thread.currentThread;

public class Rhymes {
private static Object lock = new Object();
private static BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();

public Rhymes(int threadsCount) throws InterruptedException{
ArrayList<Thread> threads = new ArrayList<Thread>(threadsCount);
for (int i = 0; i < threadsCount; i++) {
threads.add(new Thread(new Flow(lock, blockingQueue), "Thread-" + i));
(threads.get(i)).start();
Thread.sleep(100);
}

Thread.sleep(100);

for (int i = 0; i < threadsCount; i++) {
synchronized (lock) {
lock.notify();
}
System.out.println(" " + blockingQueue.take());
threads.get(i).join();
}
}

public class Flow implements Runnable {
private Object lock;
private BlockingQueue<String> blockingQueue;

public Flow(Object _lock, BlockingQueue<String> _blockingQueue) {
blockingQueue = _blockingQueue;
lock = _lock;
}

public void run() {
synchronized (lock) {
try {
lock.wait();
blockingQueue.put(currentThread().getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

}
Loading