Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Threads and Synchronization

Whenever we have multiple entities working together to get something done, we have the problem of synchronization between the entities. Processes working together, or threads in one or many processes working together, need tools to protect critical sections and to signal one another when important events occur. Java provides thread and synchronization mechanisms which programmers can use to coordinate their activities. The following problem requires that many threads coordinate their activities to assure that the program runs correctly.

A university wants to demonstrate its political correctness by applying the Supreme Court's "Separate but equal is inherently unequal" doctrine to gender as well as race. As such, the university has decided that both genders will use the same bathroom facilities. However, in order to preserve some tradition, it decrees that when a woman is in the bathroom, only other women may enter, and when a man is in the bathroom, only other men may enter. Each politically correct bathroom will have a sign on the outside indicating what state the bathroom is in; Empty, Women Present, or Men Present.

Your job is to implement a multi-threaded simulation of this politically correct bathroom. You may use whatever counters and synchronization mechanisms you need to make sure that your politically correct bathroom works according to the university decree.

Your implementation must generate some status report whenever anyone enters or leaves the bathroom, which should include the sex of the person entering or leaving. This status report should include the state of the bathroom, the number of men and/or women waiting, and the number and sex of the occupants currently in the bathroom.

Once a person (thread) enters the bathroom, that person's stay in the bathroom should be determined by a random number. The minimum stay should be 2 seconds, and the maximum stay should be 5 seconds.

Once a person (thread) has left the bathroom, use random numbers to determine when that thread will try to gain access again. I suggest that the wait should range from 4 seconds to 10 seconds.

Your program should start 5 male threads and 5 female threads. Each thread should visit the bathroom 10 times. Analyze the output to make sure things are working correctly. Capture a sample of your program's status reports. Also, print out your source code. Demonstrate your program to the professor, and get a sign off.

Turn in your output sample and source code, as well as your signed-off coversheet.

Basic Program Structure for PCB (Politically Correct Bathroom)

PCB Class

This class really does all the work of synchronizing access to the PCB. The first question to ask is what information the PCB needs to keep. The PCB has a state, which reflects whether it is empty or whether there are males or females inside. It also must keep track of the number of occupants which are inside, and the number who are waiting to get inside. There can be males or females inside or waiting.

The PCB must also have a means of blocking a thread that is trying to gain access if the state of the PCB does not permit access for this thread at this time. In order to block a thread, the PCB can use the wait method, which will be used to block threads that must wait until the state of the PCB permits access. The PCB should keep a counter which indicates how many males or females are blocked and waiting to get into the PCB.

Since the PCB has multiple member variables which must be read and written by multiple threads, we must synchronize access to these member variables by making the methods of this class synchronized methods.

The constructor for the PCB class must initialize all member variables, including all the state and counter variables.

There should be a maleEnters( ) method and a femaleEnters( ) method. These methods check the state to see whether the male or female thread is allowed to enter or must be blocked. Remember, reading and/or writing the member variables must be done inside a critical section which is protected by the synchronized methods. State and/or counter variables must be updated appropriately. If the male or female must wait to enter, the wait method is used to block the thread. Returning from one of these methods indicates that the thread was successful in entering the PCB.

There should also be a femaleExits( ) method and a maleExits( ) method. These methods update the counter and state member variables appropriately. If the thread exiting the bathroom is the last one out, the state member variable must change, and any waiting threads must be unblocked so that they can attempt to enter again. The notifyAll method can be used to wake up all the threads that are blocked and waiting to get into the PCB.

Remember that something in your simulation must output the state and counter info frequently, or whenever any thread enters or exits the PCB.

MaleThread and FemaleThread Classes

These classes contain the code that simulates the actions of a male or female thread entering and exiting the PCB. These two classes can either inherit from the Thread class or implement the Runnable interface. The constructor for these two classes should accept a PCB object so that all the male and female threads are using the same PCB object. The run method of these two classes should contain a loop which is executed 10 times. Inside the loop, a thread would sleep for a random amount of time between 4 and 10 seconds, and then call the appropriate entry method on the PCB. Once that method returns, the thread has gained access to the PCB and should wait for between 2 and 5 seconds before calling the appropriate exit method on the PCB object.

Main Test Program

This program controls the simulation. The main program must create a PCB object to be used by the male and female threads. It must also create 5 MaleThread objects and 5 FemaleThread objects and start all the male and female threads executing.

It is up to you how to display the output from your simulation. You can use a console window and report the status every time a thread enters, starts waiting, stops waiting, or exits the PCB object. Or you can use a simple GUI and use the main thread to periodically (every 100 msec) update a display that shows in some way the threads that are in the PCB or that are waiting to get in, and whether they are male or female threads.

Java, Programming

  • Category:- Java
  • Reference No.:- M91378687
  • Price:- $140

Guranteed 48 Hours Delivery, In Price:- $140

Have any Question?


Related Questions in Java

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As