java多线程代写 java多线程编程代写
Guidance notes for Assessed Coursework 2 General advice
-
1) The material needed for the project is covered in the lecture notes about concurrent programing, slides 1-154. Before the first practical class devoted to the project we would have reached up to slide 164 (almost to the end of the concurrency part). However, Exercise 1 of the coursework specification can be attempted before you have listened to Lecture 12.
-
2) A very helpful handout for this project is the third handout entitled “schedulingTasks.txt” (available on Blackboard). This handout is an extension of the lecture notes program on slides 109 – 113. The only missing class needed to run the program provided in this handout is BinarySemaphore class, the class that you will be designing in Exercise 1. In the program provided in the “schedulingTasks.txt” handout, the threads are synchronised using semaphores (only BinarySemaphore objects are used in this case, no general purpose counting semaphores, of Semaphore class, were needed here).
-
3) The Semaphore class provided in the specification should be a part of your program. Copy and paste it to your program without changing anything. Don’t add any new methods to it, like, for example, getValue() method. Semaphores have only two methods available: a method P to decrement a semaphore (or wait if the semaphore is at 0) and a method V to increment a semaphore (see slides 92-93 of the concurrency lecture notes).
Advice for Exercise 1
When building any subclass of a given class, you need to ask yourself the following questions:
-
1) Do I need any more fields in my BinarySemaphore class?
-
2) How should I use the constructors of the super-class in the constructors of my sub-class?
-
3) Do I need any more methods in my BinarySemaphore class? Can I inherit the existing
methods of the Semaphore class in my BinarySemaphore class or should I override them in the sub-class? To help you in your decision, you should remember that the value of a BinarySemaphore object can only be 0 or 1. Therefore, you can check how the methods of the super-class behave, and whether they need adjustments, if invoked on a BinarySemaphore object, whose value is 0, and how they behave when the value of a BinarySemaphore object is 1. Do remember that the V method invoked on a BinarySemaphore whose value is 1 should have no effect.
Advice for Exercise 2
The program you will be writing here will have 5 threads: main and the four threads printing different letters. The advice is that it would be better to have different classes for the threads that print letters as it will be difficult to generalise their behaviour within one class; they will share different semaphores for synchronisation (see an example of threads sharing semaphores in the program provided in “schedulingTasks.txt” handout on Blackboard).
You should approach your work on Exercise 2 step-by-step.
1) First write your program by ignoring the 3 synchronisation constraints that should be implemented; leave them for later on. So, at this point you will have no semaphores. Write the programs for your threads, so they run in infinite “while (true)” loops (as it is done in the program of “schedulingTasks.txt” handout). The main thread should at some point terminate all the threads by calling System.exit(0) as suggested by the specification on
page 2 (see also the main method in the “schedulingTasks.txt” handout again). It would be better if your threads print using print(...) statements rather than println(..) statements, as otherwise you would have a lot of scrolling to do when inspecting your outputs. The length of an output sequence is determined by the number of milliseconds your threads are sleeping, especially the main method. In this first version of the program, without synchronisation, your program will produce, each time it is executed, a sequence of letters W, X, y and z. Each time you run your program this sequence might be different as you already should know from working on the Familiarisation Exercises.
-
2) Some of the sequences produced by the first version of the program won’t satisfy the three synchronisation constraints listed in the specification. Now it is the time to implement them. First, notice that all the constraints are independent from each other and can be implemented separately. To implement the first constraint, which involves two out of four letter printing threads, you might comment out, for a while, the code that was written for the other threads (the ones printing y and z). Now you need to think how to synchronise the threads printing W and X to exclude all the sequences apart from: WXWXWXWXWXWXWX... and so on. You need to achieve the synchronisation using semaphores. You need to think how many semaphores you need, of what kind (semaphores of BinarySemaphore class or general purpose counting semaphores of the class Semaphore), and how to initialise your semaphores. Do remember that your semaphores are counters, so you do not need any extra counters in your program. Your semaphores are supposed to have full knowledge about the current situation. On the slide 92 of the concurrency lecture notes it is said that semaphores are defined as counters that count availability of shared resources or the occurrence of events. You can imagine that in your program your semaphores count “permissions” that threads give each other: “I have printed, so you can print now, I am giving you a permission”. You need to think, which thread is giving a “permission” and which thread is taking/consuming it.
-
3) Second synchronisation constraint can be implemented in a similar fashion as the first one. After implementing both constraints separately, you can now uncomment previously commented parts of the program. When you run your program with the four letter printing threads and two first constraints implemented, you will be able to check whether the output sequence satisfies the first constraint by ignoring letters y and z in it; and the second one by ignoring letters W and X in it.
-
4) Now is the time to implement the third constraint. It involves three letter printing threads, the ones printing y, z and W. Again, you need to think what kind of semaphores you require to synchronise the three threads and you need to interpret the value(s) in the semaphore object(s) as “permissions” that threads give each other to continue their executions.
