Professional Documents
Culture Documents
FCCSE
Homework
discipline
Variant #6
2
User's Manual for working with the program
First of all, user should enter a path and name of file where he wants to save and
read data
@Override
public String toString() {
return "APARTMENT:\nApartment's number: "+ apartmentNumber+
"\nArea: "+area+
"\nFloor number: "+floor+
"\nRoom number: "+roomsNumber+
4
"\nConstruction period: "+operationPeriod+
"\nConstruction type: "+constructionType;
}
}
LockRead.java
package com.company;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Override
5
public void run() {
Lock lock = new ReentrantLock();
try {
lock.lock();
FileOutputStream fos = new FileOutputStream(Path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(apartmentArrayList);
oos.close();
System.out.println("Serialized with lock");
} catch (IOException e) {
e.printStackTrace();
}
finally {
lock.unlock();
}
}
}
Main.java
package com.company;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
String path=br.readLine();
path += ".bin";
ArrayList<Apartment> apartmentArrayList = new ArrayList();
apartmentArrayList.add(new Apartment(32,2,4,17,110,"cottage"));
apartmentArrayList.add(new Apartment(312,1,3,17,100,"cottage"));
apartmentArrayList.add(new Apartment(12,2,5,17,160,"cottage"));
apartmentArrayList.add(new Apartment(2,3,7,27,200,"cottage"));
apartmentArrayList.add(new Apartment(2222222,3,7,27,200,"cottage"));
case 4: {
break;
}
}
System.out.println("What do you want to do: \n\t1. Serialize and
deserialize existing list of apartments USING WAIT/NOTIFY " +
"\n\t2.Deserialize existing list of apartments USING
LOCK\n\t3.Add a new apartment to the list\n\t4.Close program");
option=sc.nextInt();
}
}
private static void WaitThread(ArrayList<Apartment>
apartmentArrayList,String path) throws InterruptedException {
Object sync = new Object();
new Thread(() -> {
synchronized (sync) {
try {
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(apartmentArrayList);
oos.close();
System.out.println("Object was serialized.");
sync.wait();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
7
e.printStackTrace();
}
}
}).start();
Thread.sleep(500);
}
private static void LockThread(ArrayList<Apartment>
apartmentArrayList,String path)
{
LockWrite lockWrite = new LockWrite(apartmentArrayList,path);
lockWrite.run();