0% found this document useful (0 votes)
65 views5 pages

UE20CS352 Lab Assignment 8.1Q

The document describes a Java program that simulates a race between multiple runners represented as threads. Each thread will print the distance covered by its runner after each second, with the distance increasing randomly between 5-10 meters. The program ends when one runner reaches 1000 meters, at which point it prints the top 3 finishers. The requirements specify creating threads for each runner, reading the number of runners, having each thread print the distance covered each second, ending when a runner hits 1000 meters, and running the program multiple times with different numbers of runners.

Uploaded by

Krishna Basutkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views5 pages

UE20CS352 Lab Assignment 8.1Q

The document describes a Java program that simulates a race between multiple runners represented as threads. Each thread will print the distance covered by its runner after each second, with the distance increasing randomly between 5-10 meters. The program ends when one runner reaches 1000 meters, at which point it prints the top 3 finishers. The requirements specify creating threads for each runner, reading the number of runners, having each thread print the distance covered each second, ending when a runner hits 1000 meters, and running the program multiple times with different numbers of runners.

Uploaded by

Krishna Basutkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

UE20CS352 – OOAD with Java

Lab Assignment – 8 Multi-Threading


in Java
NAME: BHUPATI B RATHOD
SRN: PES2UG20CS803
SEC: J

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.

5. Execute the code at least 3 times with different number of runners.

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.

Few more advantages of Multithreading are:


Multithreading saves time as you can perform multiple operations together.
The threads are independent, so it does not block the user to perform multiple
operations at the same time and also, if an exception occurs in a single thread,
it does not affect other threads.
Life Cycle of a Thread
There are five states a thread has to go through in its life cycle. This life cycle is
controlled by JVM (Java Virtual Machine). These states are:
New
Runnable
Running
Non-Runnable (Blocked)
Terminated

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:

You might also like