Multithreading
Q1. Write a java program that will create a thread and set the thread name, display the thread name, get
the thread id, check the thread is currently alive or not.
Q2. Write a java program that will create one child thread. The child thread has to display all even
numbers between m and n, and the main thread will display all odd numbers between m and n.
Q3. Write a java program that will create two threads. Set the priority to each thread and display it.
Q4. Write a java program that will compute dot product of two vector using multithreading.
Assume that the vectors are of size n and p is number of thread used and n is a multiple of p.
Q5. Simple Java thread program to compute the sum of N natural numbers
Objective: Write a Java program to compute the sum of N natural number
Description
This program prints the sum of first N natural numbers. This introduces the concept of a
Synchronized block. Each thread adds its assigned number to a global variable. When all the
threads are done, the global variable will contain the result. It uses a Synchronized block to make
sure that only one thread is updating the variable at any given time
Input : Number of Thread
Output: Sum of first N natural numbers, N is the number of threads specified
Q6. java thread program to search the minimum number in a given array
Objective : Write a Java thread program to search the Minimum number in a given array.
Description
This program find minimum element in a given set of element. This introduces the concept of a
Synchronized block. Each thread find minimum element in a block of element and compare to
global minimum element. When all the threads are done, the global variable will contain the
minimum element. It uses a Synchronized block to make sure that only one thread is updating the global
minimum variable at any given time
Input : Number of elements, Number of Thread.
Output: Minimum Element
Q7. Write a java program to compute matrix multiplication using multithreading.
Objective : Write a Java program to compute matrix multiplication using multithreading
Description: This is an implementation of Matrix-Matrix multiplication algorithm. Number of
row equally distributed to each thread. Each thread multiplies the corresponding elements and
writes the product into the resultant matrix. The thread accesses the elements of the row based on
its rows, which is allocated by the main thread in the order of their creation.
Input : Number of row and column for first Matrix Number of row and column for second
Matrix Number of thread
Output : Time taken in matrix multiplication
Q8. The producer-consumer problem is one of the most frequently encountered problems when we
attempt multithreaded programming.
Producers produce items.
Consumers consume the items produced by the producers.
Producers finish production and let the consumers know that they are done.
Note that in this producer-consumer problem the producer is running on a different thread than the ones
on consumer. This setup makes sense in two cases.
The steps to do the consumption of the item produced in independent and not dependent on other items.
Pradipta Kumar Pattanayak
Department of Computer Science.
Silicon Institute of Technology