Overall, this is a homework assignment for a graduate level class, so much of the assignment is omitted, but I think I gave enough to make my question clear. In short, the prompt is below.
Prompt:
Assume that you have a set of files (file_0...file_N) that are accessed by a set of threads (thread_0...thread_m).
There is also a sharedstatus table that indicates thestatus of each file (closed, open for reading, open for writing). When a thread needs to access a file it must first
check the status of the file via the status table.
Here are the options:
If the file is closed, immediate access is granted.
If the file is open for writing, the thread must wait on a queue for that file.
If the file is open for reading and the new request is also for reading, then a read counter for that file is incremented and access is granted.
If the file is open for reading and the new request is for writing, the thread is placed on a queue for accessing that file.
Use a Lock variable to synchronize access to the status table. The status table should be implemented as a class; therefore, threads will have to obtain the lock before calling the status table methods. The actual status table can be implemented with an array or ArrayList where the elements are objects with properties relating to a single file (status, read count, etc).
End Prompt
Now, given this, where I am stuck is what goes where? I assume clearly that we will need at least a main, a thread table, and a thread class. I generally prefer to keep the main clean, so I use a fourth class that does the main bulk of the work and simply have the main refer to it.
The main question is how is all of this structured? I tried to structure this many times, and so far all of them have lead to conflicts, or things that simply didn't make sense. My current iteration has everything in the main functioning class, but the thread is empty, leaving me pretty much one one class with encapsulated variables, and the other the one with the content.
More concisely, here are my questions.
You probably need a ReadWriteLock which does most of what you are being asked to do.
Where would the deciding logic be for a read/write operation? Inside or outside of the thread?
Each thread must use locks to lock the resource it needs so logic is in the thread (or use something else that is used by the threads).
Where would actually reading the file occur? Inside or outside of the thread?
In the thread - after you have locked the resource.
What, in general, would go into a thread in regards to this type of programming?
Only what belongs there - nothing else :)
What exactly do I NOT put into a thread?
UI (User Interface) code.