0% found this document useful (0 votes)
125 views11 pages

Lab Manual Course Code: CS2131 Semester-III: Object Oriented Programming Lab

This document contains a lab manual for an Object Oriented Programming lab course. It includes: 1. An introduction describing the scope of the lab which is to implement multithreading concepts in Java. 2. Several examples demonstrating how to create and use threads in Java. 3. Exercises for students which involve creating threaded programs to perform tasks like busy waiting, summing random numbers generated by multiple threads, and solving an inventory problem using synchronization. 4. Homework assignments involving creating threaded programs using the Thread class and Runnable interface and solving an inventory problem using synchronization.

Uploaded by

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

Lab Manual Course Code: CS2131 Semester-III: Object Oriented Programming Lab

This document contains a lab manual for an Object Oriented Programming lab course. It includes: 1. An introduction describing the scope of the lab which is to implement multithreading concepts in Java. 2. Several examples demonstrating how to create and use threads in Java. 3. Exercises for students which involve creating threaded programs to perform tasks like busy waiting, summing random numbers generated by multiple threads, and solving an inventory problem using synchronization. 4. Homework assignments involving creating threaded programs using the Thread class and Runnable interface and solving an inventory problem using synchronization.

Uploaded by

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

Department of Computer Science and Engineering

School of Computing and Information Technology

Lab Manual
Object Oriented programming Lab
Course Code: CS2131
Semester-III
Department of Computer Science and Engineering
School of Computing & Information Technology

Object Oriented Programming Lab


List of Experiments
Course Code: CS2131 Semester-III

1. Write java programs of multithreading programs using thread class and


runnable interface and also perform the operations on threads.
2. Demonstrate the concept of synchronization and inter-thread process
communication with real-world scenarios with java multithreading programs.
Department of Computer Science and Engineering
School of Computing & Information Technology

Lab 8: Multithreading

Objective:

To implement the concepts of Multithreading

Scope:

Multithreading is a widespread programming and execution model that allows multiple


threads to exist within the context of a single process. These threads share the process's
resources, but are able to execute independently. The threaded programming model
provides developers with a useful abstraction of concurrent execution. Multithreading
can also be applied to a single process to enable parallel execution on a multiprocessing
system.

Example 1: This program demonstrates how to implement threads in Java.


Department of Computer Science and Engineering
School of Computing & Information Technology
Department of Computer Science and Engineering
School of Computing & Information Technology

Example 2: This program demonstrates how to implement threads in Java.


Department of Computer Science and Engineering
School of Computing & Information Technology
Department of Computer Science and Engineering
School of Computing & Information Technology

Example 3: This program demonstrates how to implement threads in Java


Department of Computer Science and Engineering
School of Computing & Information Technology

Exercises:

Exercise 1:

Create an example of a “busy wait.” One thread sleeps for a while and then sets a
flag to true. The second thread watches that flag inside a while loop (this is the
“busy wait”) and when the flag becomes true, sets it back to false and reports the
change to the console. Note how much wasted time the program spends inside the
“busy wait” and create a second version of the program that uses wait( ) instead
of the “busy wait.”

Exercise 2:

Write a program that runs 5 threads, each thread randomizes a number between
1 and 10. The main thread waits for all the others to finish, calculates the sum of
the numbers which were randomized and prints that sum. You will need to
implement a Runnable class that randomizes a number and store it in a member
field. When all the threads have done, your main program can go over all the
objects and check the stored values in each object.

Exercise 3:

Modify the program in (Ex. 2) so that instead of each object keeping its own score,
you will use one collection to store all the results in.
Department of Computer Science and Engineering
School of Computing & Information Technology

Exercise 4:

XYZ company has offices in different cities and wants to lay down the wire, to connect them with
each other. The wire cost is different to connect different pairs of cities. The notation is represented
in the following figure.

Here edges are undirected and numbers over edge represent the cost of wire. When two edges
are connected to the same pair of cities and cost of wire is different than it is called parallel edge.
If start and end city of an edge are same, then it is called loop. Consider the following declaration.

class Edge { private String start; // start


node of an edge private String end; // End
node of an edge private int cost; // cost
of wire
Edge(String s, String e, int w)
{
start =s;
end =e;
cost =w;
} } // end of edge class
class Graph
{
private ArrayList <Edge> list_edges; // list of edges of a graph
Graph(ArrayList <Edge> n) // constructor
{
list_edges =n;
}
public void removeLoop()
{
Q.1.1 /* write the java code to remove loops from the
list_edges:ArrayList. */
}
public Edge findParallelEdge(Edge n)
{
Department of Computer Science and Engineering
School of Computing & Information Technology

Q.1.2 /* This function finds a parallel edge of n:Edge in


list_edges:ArrayList. If parallel edge exists it returns parallel edge
otherwise null. Write the java code*/
} public void
removeParallel()
{
Q.1.3 /* This method traverses list_edges:ArrayList and finds a parallel
edge. if an Edge’s parallel edge is found and Edge’s cost is higher than
parallel edge it removes Edge. Write the java code */
}
public void print()
{
Q.1.4 // write the java code to print edges of list_edges:ArrayList
}
}

Homework

Exercise 1
Write a program to create two threads. In this class we have one constructor used to start
the threads and run it. Check whether these two threads are run or not.

Exercise 2
Create a multithreaded program by using Runnable interface and then create, initialize
and start three Thread objects from your class. The threads will execute concurrently
and display the following String array elements.
String course [ ] = {“Java”, “J2EE”, “Spring”, “Struts”};

Exercise 3
Write a program for inventory problem to illustrate the usage of synchronized keyword.
Note: The output should be similar as mentioned below:
Thread1Thread[test thread,5,main]

Thread2Thread[test thread,5,main]
Quantity ordered :13

Quantity on hand :487

Total quantify taken away by way of order :13


Department of Computer Science and Engineering
School of Computing & Information Technology

Quantity ordered :91

Quantity on hand :396

Total quantify taken away by way of order :104

You might also like