20XW58 JPL PS7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

PSG COLLEGE OF TECHNOLOGY, COIMBATORE – 641 004

Department of Applied Mathematics and Computational Sciences


MSc Software Systems – Semester V
20XW57– Java Programming Lab
PROBLEM SHEET 7 – Multithreading

Completion Date: 14/09/2024

Note: Use Text Editors/IntelliJ IDEA/Apache NetBeans tools to develop, compile and execute the below
programs

1. Write a program to illustrate creation of threads using Runnable interface (start method start
each of the newly created thread. Inside the run method there is sleep ( ) for suspend the thread
for 500 milliseconds).
2. Write a program to create a class MyThread in this class a constructor, call the base class
constructor, using super and starts the thread. The run method of the class starts after this. It
can be observed that both main thread and created child thread are executed concurrently.
3. Write a java program to create five threads with different priorities. Send two threads of highest
priority in sleep state. Check the aliveness of the threads and mark which thread is long listing.
4. Write a multithreaded program that calculates various statistical values for a list of numbers.
This program will be passed a series of numbers on the command line and will then create
three separate worker threads. One thread will determine the average of the numbers, the
second will determine the maximum value, and the third will determine the minimum value.
For example, suppose your program is passed the integers 90 81 78 95 79 72 85
Output:
The average value is 82
The minimum value is 72
The maximum value is 95
The variables representing the average, minimum, and maximum values will be stored
globally. The worker threads will set these values, and the parent thread will output the values
once the workers have exited.
5. Write a program for inventory problem, to illustrate the usage of synchronized keyword.
6. Write a program for interthread communication process. In this they have three classes
consumer, producer and stock.

addStock( ) getStock( )
Producer Stock Consumer
notify( ) wait( )
7. Your English literature friend is very happy with the code you gave him. Now for his research,
he used your application to find character frequency in many novels. For larger novels, the
application takes a lot of time for computation. So he called you on a fine Sunday to discuss
this with you. He wanted to know whether you can improve the speed of the application.
You decided to modify the application by using multiple threads to reduce the computation
time. For this, accept the number of counters or threads at the beginning of the problem and
get the string for each counter or thread. Create a thread by extending the Thread class and
take the user entered string as input. Each thread calculates the character frequency for the
word assigned to that thread. All the counts are stored locally in the thread and once all the
threads are completed print the character frequency for each of the threads.
Create a class Main and test it.
Input and Output format:
Refer to sample Input and Output for formatting specifications.
Sample input and output:
Enter Number of Counters:2
Enter text for counter 1: FrequencyCounter
Enter text for counter 2: JavaTheCompleteReference
Counter 1 Result :
C:1 F:1 c:1 e:3 n:2 o:1 q:1 r:2 t:1 u:2 y:1
Counter 2 Result :
C:1 J:1 R:1 T:1 a:2 c:1 e:7 f:1 h:1 l:1 m:1 n:1 o:1 p:1 r:1 t:1 v:1

You might also like