Indian cricket team Players at an winning celebration dine at a circular table on which there is a single fork between each Player. Players either eat or think, and always start dinner thinking. To eat, a Player first picks up the fork immediately to his left and, once successful, picks up the fork immediately to his right. When a required fork is not on the table, the Player waits, neither eating nor thinking, until the fork is returned to the table. After eating, a Player returns both forks to the table. No cutlery is required to think. Your task is to model the above scenario in Java. (a) Write a class called Fork with two public methods, ForkpickUp and ForkputDown. The methods should take no arguments and return no result. An instance of Fork should act as a lock to prevent concurrent access. In other words, once ForkpickUp has been called, all further calls to ForkpickUp should block until ForkputDown is called; when ForkputDown is called, one caller (if any) who is blocked should proceed. (b) Write a class called Players which inherits from the Thread class and implements the abstract method run. The Player class should have a single constructor which takes two Fork objects, one representing the fork to the Player’s left, and one to the right. When run, an instance of Player should think for ten seconds, eat for ten seconds and think for ten seconds before terminating.
Indian cricket team Players at an winning celebration dine at a circular table on which there is a single fork between each Player. Players either eat or think, and always start dinner thinking. To eat, a Player first picks up the fork immediately to his left and, once successful, picks up the fork immediately to his right. When a required fork is not on the table, the Player waits, neither eating nor thinking, until the fork is returned to the table. After eating, a Player returns both forks to the table. No cutlery is required to think. Your task is to model the above scenario in Java. (a) Write a class called Fork with two public methods, ForkpickUp and ForkputDown. The methods should take no arguments and return no result. An instance of Fork should act as a lock to prevent concurrent access. In other words, once ForkpickUp has been called, all further calls to ForkpickUp should block until ForkputDown is called; when ForkputDown is called, one caller (if any) who is blocked should proceed. (b) Write a class called Players which inherits from the Thread class and implements the abstract method run. The Player class should have a single constructor which takes two Fork objects, one representing the fork to the Player’s left, and one to the right. When run, an instance of Player should think for ten seconds, eat for ten seconds and think for ten seconds before terminating.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps