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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
*.iml
out/
408 changes: 0 additions & 408 deletions lab-01/README.md

This file was deleted.

72 changes: 72 additions & 0 deletions lab-02/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"generator" : {
"generating_time": 500
},
"cargo_types" : ["1", "2", "3", "4"],
"ingredients_count" : [6, 4, 2, 5],
"eating_time": 500,
"ships" : [
{
"capacity" : 160,
"cargo_type" : "2"
},
{
"capacity" : 200,
"cargo_type" : "1"
},
{
"capacity" : 130,
"cargo_type" : 2
},
{
"capacity" : 180,
"cargo_type" : 3
},
{
"capacity" : 93,
"cargo_type" : "1"
},
{
"capacity" : 111,
"cargo_type" : "4"
},
{
"capacity" : 211,
"cargo_type" : 2
}
],
"tunnel": {
"max_size" : 4
},
"docs": [
{
"unloading_speed" : 40,
"dock_capacity": [160, 200, 180, 100]
},
{
"unloading_speed" : 28,
"dock_capacity": [260, 100, 280, 300]
},
{
"unloading_speed" : 33,
"dock_capacity": [230, 150, 220, 176]
}
],
"hobos": [
{
"stealing_time": 100
},
{
"stealing_time": 200
},
{
"stealing_time": 300
},
{
"stealing_time": 200
},
{
"stealing_time": 400
}
]
}
49 changes: 49 additions & 0 deletions lab-02/src/Dock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

public class Dock implements Runnable {
static final Object lock = new Object();
long unloading_speed;
final long[] dock_capacity;
final AtomicLong[] curr_dock_capacities;
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
synchronized (lock) {
lock.wait();
}
Ship ship = Tunnel.tunnel.poll();
if (ship != null) {
Tunnel.tunnel_size.getAndDecrement();
System.out.println(ship + " is unloading");
long type = ship.cargo_type;
long capacity = Math.min(ship.capacity, dock_capacity[(int) type] - curr_dock_capacities[(int) type].get());
Thread.sleep((long) ((double) capacity / unloading_speed * 1000));
System.out.println("Capacity: " + capacity + " of type: " + type + " was unloaded");
curr_dock_capacities[(int) type].addAndGet(capacity);
ship.capacity -= capacity;
System.out.println(ship + ", unloading is end");
synchronized (ship.lock) {
ship.lock.notify();
}
}
}
} catch (InterruptedException e) {
System.out.println("Oops Dock");
Thread.currentThread().interrupt();
}
}

Dock(long unloading_speed, long[] dock_capacity) {
if (dock_capacity.length != Ship.cargo_types.length) {
throw new IllegalArgumentException("Incorrect length of dock_capacities");
}
this.unloading_speed = unloading_speed;
this.dock_capacity = Arrays.copyOf(dock_capacity, dock_capacity.length);
curr_dock_capacities = new AtomicLong[dock_capacity.length];
for (int i = 0; i < dock_capacity.length; ++i) {
curr_dock_capacities[i] = new AtomicLong(0);
}
}
}
32 changes: 32 additions & 0 deletions lab-02/src/Generator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;

public class Generator implements Runnable{

JSONObject jo;
static int generate(long number) {
return (int) (Math.random() * number);
}
Generator(JSONObject jo) { this.jo = jo; }

@Override
public void run() {
long generating_time = (long) ((JSONObject) jo.get("generator")).get("generating_time");
JSONArray ships = (JSONArray) jo.get("ships");
for (Object o : ships) {
JSONObject ship = (JSONObject) o;
Object cargo_type = ship.get("cargo_type");
if (cargo_type.getClass() == String.class) {
Main.ships.add(new Ship((long) ship.get("capacity"), (String) cargo_type));
} else {
Main.ships.add(new Ship((long) ship.get("capacity"), (long) cargo_type));
}
try {
Thread.sleep(generating_time);
} catch (InterruptedException e) {
System.out.println("Generator stopped");
Thread.currentThread().interrupt();
}
}
}
}
97 changes: 97 additions & 0 deletions lab-02/src/Hobos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class Hobos implements Runnable{
static long eating_time;
long stealing_time;

static long eat = 0;

static long ID = 0;
long ID_;

static long[] ingredients_count;
static AtomicLong[] curr_ingredients_count;

static boolean check_ingridients() {
for (int i = 0; i < curr_ingredients_count.length; ++i) {
if (curr_ingredients_count[i].get() < ingredients_count[i]) {
return false;
}
}
return true;
};
static void steal() {
long hobos_num = Main.hobos.size();
ArrayList<Thread> hobos_threads = new ArrayList<>();
for(long i = 0; i < hobos_num - 2; ++i) {
hobos_threads.add(new Thread());
}

while(!Thread.currentThread().isInterrupted()) {
long a, b;
a = Generator.generate(hobos_num);
b = Generator.generate(hobos_num);
while (a == b) {
b = Generator.generate(hobos_num);
}
System.out.println("Start to steal!!!");
for(int i = 0, j = 0, size = Main.hobos.size(); i < size; ++i) {
if (i != a && i != b) {
hobos_threads.set(j, new Thread(Main.hobos.get(i)));
hobos_threads.get(j).start();
++j;
}
}
while (!check_ingridients()) {}
System.out.println("Time to eat!!!");
++eat;
for (var thread : hobos_threads) {
thread.interrupt();
}
for (int i = 0; i < curr_ingredients_count.length; ++i) {
curr_ingredients_count[i].set(0);
}
try {
Thread.sleep(eating_time);
} catch (InterruptedException e) {
System.out.println("Oops Hobos");
Thread.currentThread().interrupt();
}
}

}

@Override
public void run() {
System.out.println("Start stole " + this);
while (!Thread.currentThread().isInterrupted()) {
int ing = Generator.generate(ingredients_count.length);
Dock dock = Main.docs.get(Generator.generate(Main.docs.size()));
while (curr_ingredients_count[ing].get() < ingredients_count[ing] &&
dock.curr_dock_capacities[ing].get() > 0 &&
!Thread.currentThread().isInterrupted()) {
curr_ingredients_count[ing].getAndIncrement();
dock.curr_dock_capacities[ing].getAndDecrement();
try {
Thread.sleep(stealing_time);
} catch (InterruptedException e) {
System.out.println("Oops Hobos");
Thread.currentThread().interrupt();
}
}
}
System.out.println("End stole " + this);
}

Hobos(long stealing_time) {
this.stealing_time = stealing_time;
ID_ = ++ID;
}

@Override
public String toString() {
return "Hobos: {" + ID_ + "}";
}
}
116 changes: 116 additions & 0 deletions lab-02/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

public class Main {
static double eps = 1e-6;
static ArrayList<Ship> ships = new ArrayList<>();
static ArrayList<Dock> docs = new ArrayList<>();
static ArrayList<Hobos> hobos = new ArrayList<>();
public static void main(String[] args) {
try {
Object obj = new JSONParser().parse(new FileReader(args[0]));
JSONObject jo = (JSONObject) obj;

JSONObject jtunnel = (JSONObject) jo.get("tunnel");
Tunnel tunnel = new Tunnel(((Long) jtunnel.get("max_size")).intValue());

JSONArray jcargo_types = (JSONArray) jo.get("cargo_types");
int size = jcargo_types.size();
Ship.cargo_types = new String[size];
for (int i = 0; i < size; ++i) {
Ship.cargo_types[i] = (String) jcargo_types.get(i);
}

Thread generator = new Thread(new Generator(jo));
generator.start();

JSONArray jdocs = (JSONArray) jo.get("docs");
for (Object o: jdocs) {
JSONObject jdock = (JSONObject) o;
JSONArray jdock_capacity = (JSONArray) jdock.get("dock_capacity");
size = jdock_capacity.size();
long[] dock_capacity = new long[size];
for (int i = 0; i < size; ++i) {
dock_capacity[i] = (long) jdock_capacity.get(i);
}

docs.add(new Dock((long) jdock.get("unloading_speed"), dock_capacity));
}

Hobos.eating_time = (long) jo.get("eating_time");

JSONArray jingredients_count = (JSONArray) jo.get("ingredients_count");
size = jingredients_count.size();
Hobos.ingredients_count = new long[size];
Hobos.curr_ingredients_count = new AtomicLong[size];
for (int i = 0; i < size; ++i) {
Hobos.ingredients_count[i] = (long) jingredients_count.get(i);
Hobos.curr_ingredients_count[i] = new AtomicLong(0);
}
JSONArray jhobos = (JSONArray) jo.get("hobos");
for (Object o : jhobos) {
JSONObject jhobo = (JSONObject) o;
hobos.add(new Hobos((long) jhobo.get("stealing_time")));
}

ArrayList<Thread> ship_threads = new ArrayList<>();
ArrayList<Thread> dock_threads = new ArrayList<>();

size = docs.size();
for (int i = 0; i < size; ++i) {
dock_threads.add(new Thread(docs.get(i)));
dock_threads.get(i).start();
}

Thread process = new Thread(tunnel::process);
Thread hobos = new Thread(Hobos::steal);
process.start();
hobos.start();

size = ships.size();
for (int i = 0; i < size || generator.isAlive(); size = ships.size()) {
if (i < size) {
int finalI = i;
ship_threads.add(new Thread(() -> {
tunnel.enter(ships.get(finalI));
}));
ship_threads.get(finalI).start();
++i;
}
}

for (var thread : ship_threads) {
try {
thread.join();
} catch (InterruptedException exc) {
System.out.println("Oops");
}
}

process.interrupt();

for (var thread : dock_threads) {
thread.interrupt();
}

hobos.interrupt();

System.out.println("Interupted");
for (var dock : docs) {
System.out.println(Arrays.toString(dock.curr_dock_capacities));
}
System.out.println(Hobos.eat);
} catch (IOException | ParseException exc) {
System.out.println("Oops");
}
}
}
Loading