Ask Java Expert


Home >> Java

Through its implementation, this project will familiarize you with the creation and execution of threads, and with the use of the Thread class methods. In order to synchronize the threads you will have to use (when necessary), run( ), start( ), currentThread( ), getName( ), join( ), yield( ), sleep(time), isAlive( ), getPriority( ), setPriority( ), interrupt( ).

In synchronizing threads, do NOT use any semaphores, wait( ), notify( ) or notifyAll( ).

Triathlon

The triathlon competitors arrive at the stadium. There are three events that must be completed: running, bicycle riding and swimming. Once arrived, the competitors must wait (use busy waiting) for the race to start. The judge will announce the start of the race (use Boolean shared variable start) and mark the starting time in a database (for obtaining the time use the age( ) method). Each competitor runs for a random time between 20 and 40 units.

NOTE: starting with JDK 1.5 and up, operations on vectors are already synchronized, and executed in a Mutual Exclusion fashion. After a competitor completes the run, the time must be registered in the database (don’t need to have a real database but you need to save the necessary information). Between the events, each competitor is allowed to take a break of a random time of up to 10 units. Generate a rest period between 1 and 13 units (use sleep(randomTime)). If a competitor exceeds the allowed time, he will be disqualified. The next course is the bicycle event. All of the bicycles are stored at the starting location. Each competitor will wait (using busy waiting) for a bicycle to be available for pick up. The judge will instruct the competitors in a FCFS order to pick up their bikes. Preparing the bicycle takes some time. The competitor wants to prepare the bicycle as fast as he can. For five units of time only the competitor will increase its priority by 2 (use getPriority( ), setPriority( )). When ready to start the race, each competitor will log the starting time and bicycle for a random time between 30 and 45 units. When the competitor gets to the finish line, he will log the finish time in the database.

A new rest break will be allowed (implement it using the yield( ) method). The last course is the swimming event. It starts once the competitors finish the rest break following the run. There are numLanes available. Competitors will have to form up into groups of size equal to numLanes. When a group is ready, the race can start. The starting time must be registered in the database. Each involved competitor will swim for a random time between 10 and 15 units. Once the competitor finishes the race, he will register the time in the database.

Note: don’t worry about the possibility at some point of having two sets of swimmers in the swimming pool at the same time. Consider that each group uses a different swimming pool.

The competitors must wait until the judge announces the prizes (simulate wait using sleep of a long time - like 500 units, longer that the time it takes the judge to do the all computations). The last competitor to finish the race must inform the judge that the race is over before going to sleep. Use a counter to keep track of the number of competitors that have finished. The judge will resume all the competitors (by interrupting them from sleep – use interrupt( )) when done with the computations, and will announce the first prize for each event and the grand prize for the race. The competitors will leave one by one, in the order of their ID(use isAlive( ) and join( )). In your implementation you should also make use of the currentThread( ) and getName( ) methods.

Using Java programming, synchronize the two types of threads, judge, competitors, in the context of the problem. Closely follow the implementation requirements.

The number of competitors, numComp, numLanes should be read as command line arguments, e.g. –c -l .

Default values: numComp = 20

numLanes = 4

Use appropriate System.out.println( ) statements and the age( ) method to reflect the time of each particular action done by a specific thread. This is necessary for us to observe how the synchronization is working.

Note: a good value for 1 unit of time might be: 100ms

• Add the following lines to all the threads you make:

public static long time = System.currentTimeMillis();

public void msg(String m) {

System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m);

}

Whenever you want to print something from that thread do: msg("some message here");

• NAME YOUR THREADS or the above lines that were added would mean nothing. Here's how the constructors should look like (you may use any variant of this as long as each thread is unique and distinguishable):

// Default constructor

public RandomThread(int id) {

setName("RandomThread-" + id);

}

Java, Programming

  • Category:- Java
  • Reference No.:- M9663

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

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 ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

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 ...

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 ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 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