This project demonstrates Java multithreading concepts and facilities for managing orders in an online shopping application, similar to Emag. The byte implementation variant was chosen for this assignment.
-
FileChannels: Allow mapping of data chunks from input files into memory.
-
HelperObject class: Encapsulates thread properties including:
- MappedByteBuffer
- Size
- Position
-
setNewBuffer() method: Implements the bonus feature for dividing work between threads.
-
Executor: Uses a fixed thread pool of size nrThread (passed as a program parameter).
-
Main method: Serves as the entry point and sets up the entire implementation.
- Models the logic for level 1 threads.
- In the run() method:
- Each thread obtains its chunk from the command file and saves it in its buffer.
- Uses synchronization to ensure thread-safety when calling setNewBuffer().
- The last parent thread (highest id) starts after other threads finish processing.
- Uses a barrier to ensure all threads start equally from a certain point.
- Implements a latch object to limit the number of concurrent threads to P on each level.
- Creates and manages level 2 execution threads.
- Models the logic for level 2 threads.
- Similar reasoning to RunnableOrderThread.
- Addresses challenges in MappedByteBuffer to String conversion using the duplicate() method.
The algorithm design is based on two critical aspects:
- Generation: Handles cases where a thread's chunk might contain an incomplete command (e.g., "o_dwri3").
- Correction: Checks if the last character in the chunk is "o", and eliminates the problem from that buffer if it's not.
The correction phase modifies the size at each step, updating the new position at the end.
- All started files and channels are closed at the end of the program in the main method.