Define a class MyData that holds an instance variable x, a function to increment x by 5, and a getter to return the value of x. In a MyUtil class, define an aggregate relationship where MyUtil holds a MyData reference. MyUtil should extend Thread and provide an implementation for run that contains a for loop that iterates 100000 times calling the increment function of the myData reference. Define the main method in MyUtil that creates the instance of MyData. Define a data collection for storing MyUtil objects. In three separate for loops, (1) create and add five instances of MyUtil to the data collection previously defined. (2) Call the start function on each thread. (3) Ensure each thread completes before main completes. Print out the value of x from myData.  In a brief comment, describe what is happening when you execute the program several times. Also in the comment, what keyword could be used where to prevent the issue seen?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Define a class MyData that holds an instance variable x, a function to increment x by 5, and a getter to return the value of x. In a MyUtil class, define an aggregate relationship where MyUtil holds a MyData reference. MyUtil should extend Thread and provide an implementation for run that contains a for loop that iterates 100000 times calling the increment function of the myData reference. Define the main method in MyUtil that creates the instance of MyData. Define a data collection for storing MyUtil objects.

In three separate for loops, (1) create and add five instances of MyUtil to the data collection previously defined. (2) Call the start function on each thread. (3) Ensure each thread completes before main completes. Print out the value of x from myData.  In a brief comment, describe what is happening when you execute the program several times. Also in the comment, what keyword could be used where to prevent the issue seen?

Expert Solution
Step 1

NOTE: - ISSUE is discussed in step 4.

Different Classes Description: -

  1. MyData class comprises one instance variable x,  when the object of MyData class is created, x is initialized with 0 using the default constructor provided by the compiler. The increment() is provided to access the private data member x outside class and increment it by 5 each time when accessed.
  2. The MyUtil class has an aggregate relationship with MyData. When the object of MyUtil class is created, the instance data member myData also gets initialized. This class extends Thread class, so it is a must to override the run method.

The run() method is overridden to define the job of the thread. The job of the thread is running a for loop 10000 times and increasing the value of x by 5 each time.

Concepts involved: -

  1. In the first for loop object of MyUtil class is created, then the object is stored in Arraylist collection, then the start method is being called.
  2. The start() method is being called to start the thread, which is the current iteration MyUtil object. As soon start() is called the control goes to Thread Scheduler. Always the thread having maximum priority executes. When the thread gets the opportunity to execute, internally Thread class executes the run() method, thus the job of the current thread is performed.
  3. run() method: This method is overridden to define the task of the thread. When the thread gets the chance to execute by the thread scheduler, control goes to the run() method, here the name of the thread is printed by getting the name of the currently executing thread using Thread.currentThread.getName() method. The currentThread() is a static method of Thread class, which gives the currently executing thread object, then the name of the thread is printed using the getName() method.
  4. The process involved in steps 1,2,3, and 4 is repeated in the next 4 iterations of the first loop.
  5. Now the join() method is called by the main thread on all 5 threads being created in for loop. All these threads keep on executing in random order since the increment() method is not synchronized.
  6. This work is being done in the other two for loops as well. Then the join() method is being called on all MyUtil objects created in the other two for loops as well.
  7. The value of x is printed using the getX() method on MyData object, to get the actual value of x.
  8. In the end name of the currently executing thread is printed to verify if the main thread is still executing.
Step 2

//WORKING CODE

import java.util.ArrayList;

//MyUtil class is a child class of Thread class
public class MyUtil extends Thread {

 MyData myData;

 // MyData reference Set
 // via parameterized constructor
 MyUtil(MyData myData) {
  this.myData = myData;
 }

 // Override the run method with a
 // for loop that runs 10000 times and calls intCell.increment();
 public void run() {
  System.out.println(Thread.currentThread().getName() + " started !! ");
  int i = 0;
  for (; i < 10000; i++) {
   System.out.println(Thread.currentThread().getName() + " executing..");
   // call increment method in myData object
   // increment the value by 5
   myData.increment();
  }
 }

 public static void main(String[] args) throws InterruptedException {

  // create object of MyData class
  MyData dataObject = new MyData();

  // Store MyUtil objects in ArrayList
  ArrayList<MyUtil> utilList = new ArrayList<MyUtil>();

  // first for loop create 5 MyUtil objects
  // store in utilList
  int index = 0;
  for (; index < 5; index++) {
   // Create MyUtil objects and
   // pass MyData object as parameter to MyUtil constructor
   MyUtil utilObject = new MyUtil(dataObject);

   // add the object in Arraylist collection
   utilList.add(utilObject);

   // start the current utilObject thread
   utilObject.start();

  }
  // call join method on 5 utilObjects created
  // so that current utilObject finishes
  // before the main thread
  utilList.get(0).join();
  utilList.get(1).join();
  utilList.get(2).join();
  utilList.get(3).join();
  utilList.get(4).join();

  // Second for loop create 5 MyUtil objects
  // store in utilList
  index = 0;
  for (; index < 5; index++) {
   // Create MyUtil objects and
   // pass MyData object as parameter to MyUtil constructor
   MyUtil utilObject = new MyUtil(dataObject);

   // add the object in Arraylist collection
   utilList.add(utilObject);

   // start the current utilObject thread
   utilObject.start();

  }
  // call join method on 5 utilObjects created
  // so that current utilObject finishes
  // before the main thread
  utilList.get(5).join();
  utilList.get(6).join();
  utilList.get(7).join();
  utilList.get(8).join();
  utilList.get(9).join();

  // Third for loop create 5 MyUtil objects
  // store in utilList
  index = 0;
  for (; index < 5; index++) {
   // Create MyUtil objects and
   // pass MyData object as parameter to MyUtil constructor
   MyUtil utilObject = new MyUtil(dataObject);

   // add the object in Arraylist collection
   utilList.add(utilObject);

   // start the current utilObject thread
   utilObject.start();

  }
  // call join method on utilObject
  // so that current utilObject finishes
  // before the main thread
  utilList.get(10).join();
  utilList.get(11).join();
  utilList.get(12).join();
  utilList.get(13).join();
  utilList.get(14).join();
  // Finally, print the value of x: dataObject.getX();
  System.out.println("\nValue of X At End: " + dataObject.getX());

  // below method prints the name of current executing thread
  System.out.println(Thread.currentThread().getName() + " is still executing..");
 }

}

//MyData class
class MyData {
 // declare and initialize x variable with 0
 private int x = 0;

 // increment the val variable value by 5
 synchronized void increment() {
  x += 5;
 }

 // get the value of X
 int getX() {
  return this.x;
 }
}

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education