UE20CS352 Lab Assignment 8.1Q
UE20CS352 Lab Assignment 8.1Q
Problem Description:
Write a Java program that simulates a race between multiple runners. Each runner should
be represented as a separate thread, and the program should output the current distance
each runner has covered after each second. The distance each runner covers in each second
should be determined randomly. The program should stop once one of the runners reaches
a distance of 1000 meters. The program should then print the top 3 runners of the race.
Requirements:
1. The program should read input as to how many runners are running the race.
2. The program should create a separate thread for each runner (optionally add names
for each thread to represent the runner).
3. Each thread should print the total distance run so far by the runner after each
second. The distance each runner covers in each second should be determined
randomly (approx. between 5 – 10 m).
4. The program should stop once one of the runners reaches a distance of 1000 meters.
The program should then print the top 3 runners of the race.
Submission Document:
The submission should contain a write up about Multithreading in Java (min 1 page to max 2
pages), the problem statement, the code and screenshot of execution output (execute the
code at least 3 times with different number of runners).
MULTITHREADING.
Multithreading is a Java feature that allows concurrent execution of two or
more parts of a program for maximum utilization of CPU. Each part of such
program is called a thread. So, threads are light-weight processes within a
process.
1. New
In this state, a new thread begins its life cycle. This is also called a born thread.
The thread is in the new state if you create an instance of Thread class but
before the invocation of the start() method.
2. Runnable
A thread becomes runnable after a newly born thread is started. In this state, a
thread would be executing its task.
3. Running
When the thread scheduler selects the thread then, that thread would be in a
running state.
4. Non-Runnable (Blocked)
The thread is still alive in this state, but currently, it is not eligible to run.
OUTPUT: