-1

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.

  • Where would the deciding logic be for a read/write operation? Inside or outside of the thread?
  • Where would actually reading the file occur? Inside or outside of the thread?
  • What, in general, would go into a thread in regards to this type of programming?
  • What exactly do I NOT put into a thread?
Xenorosth
  • 97
  • 1
  • 9
  • 1
    I have the feeling you mix 2 distinct concepts: Classes and Threads. And that's what confuses you. I suggest, you firstly identify your candidates for classes. That is: From the requirements text you identify entities and their responsibilities. For example: The "Statustable" - 1 class holding FileInformation. And there is your next Entity: The FileInformation class which holds read-counters, locks for one specific file etc ... These reside in a ArrayList in the Statustable class. So far so good. Next you have your Consumers ( Entities wanting to access Files ) ... – Fildor Oct 24 '16 at 13:31
  • Consumers are likely to be executed on multiple Threads. So they must synchronize against your critical resource (the Statustable). – Fildor Oct 24 '16 at 13:33
  • That's a good clarification. In this case I assume that means both the read and write must be put in the threads due to the need for them to happen at the same time. – Xenorosth Oct 24 '16 at 14:07

1 Answers1

1

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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Well, that's the question with this program. What DOES belong in there? More specifically, how would I know, in this program, what should, and shouldn't be in the thread. I can't figure out what the content of the thread actually is. – Xenorosth Oct 24 '16 at 13:26
  • There is no "content of the thread". There is only code that will be executed concurrently. Code of the shared resource will also be executed on that thread but also on another only not at the same time ... (not concurrently). – Fildor Oct 24 '16 at 13:37
  • So asking for clarification, does this mean the only things that should be in the threads are things that run concurrently? – Xenorosth Oct 24 '16 at 13:49
  • @Xenorosth Most web sites run completely in threads. The `main` thread is just sitting there waiting for them all to complete (and most of them dont). There is no real rule for what should and what should not be in the thread. Things you need to run concurrently should be inthreads but that is not the end. Given time you will understand. – OldCurmudgeon Oct 24 '16 at 15:37
  • It's homework. Using a reader/writer lock would be cheating: The OP is being asked to _implement_ a reader/writer lock. – Solomon Slow Oct 25 '16 at 01:26