Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, students shall practice mass processing of a large number of

ID: 3889288 • Letter: I

Question

In this assignment, students shall practice mass processing of a large number of "user (or customer)" objects in Java (in an application solution called BigDataThreads.java).

You may use an ArrayList or an Array (or some other List/File types) for this assignment.

Please follow the detail requirements below (with the required steps #s in bold):

#1. Create something in java to store thousands of users (or customers) objects. You can use Array/ArrayList/etc for those objects. For this assignment, you can use a simple (dummy) users/customers with a unique ID and some random customer name plus other fields of your choice.

#2. Create 1,000 user/customer objects for the new customers. Place them into the list you declared in step #1. Write a Runnable task to accomplish this process of creating those

#3. In the meantime, continually remove the user/customer objects from your list. Write a Runnable task with a loop of 1,000 removal steps.

#4. (Optional) you may also have a processing thread if you choose to process the objects further between enter and exit. But you do not have to do #4 as this is optional.

#5. Make sure that the processing are concurrent/parallel in steps 2 and 3. /* If you implement the optional step 4 , it has to run in parallel with steps 2&3. */

Print the total # of user/customer(s) left in your list after steps 1~4.   You do not need to print each object details; only print the total # left in your list.  

Please submit screen captures to show that you have done at least 2 run(s) to see different #s at the end of each run before you implement next requirement in #6.

#6. Implement additional control to make sure the removal process is NOT ahead of the user/customer object creation process AND make sure that the creation process has to pause when the list has reach capacity. The creation process continues when one or more objects are removed to allow new objects. Then create more objects to flood the list capacity you got from step #1.

Submit another screen capture to show the total # of users/customers left after you added the additional thread control.

#7.  Make sure you also add comments in your java codes to document your solutions.

Explanation / Answer

Before 6

import java.io.*;
import java.util.*;

class user{
   public String name;
}

class Thread1 implements Runnable
{
   Thread mythread ;
   ArrayList<user> list;
   Thread1(ArrayList<user> l)
   {
     list = l;
      mythread = new Thread(this, "Thread1");
      System.out.println("Thread1 created");
      mythread.start();
   }
   public void run()
   {
      try
      {
        for (int i=0 ;list.size()<1000;i++)
        {
         
          list.add(new user());
          System.out.println("Adding items " + i);
          Thread.sleep(1);
         
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("Thread1 interrupted");
     }
     System.out.println("Thread1 run is over" );
   }
}

class Thread2 implements Runnable
{
   Thread mythread ;
   ArrayList<user> list;
   Thread2(ArrayList<user> l)
   {
      mythread = new Thread(this, "Thread2");
      System.out.println("Thread2 created");
      list = l;
      mythread.start();
   }
   public void run()
   {
      try
      {
        for (int i=0 ;i<1000;i++)
        {
          System.out.println("Removing objects " + i);
         
          list.remove(list.size() -1);
          Thread.sleep(1);        
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("Thread2 interrupted");
     }
     System.out.println("Thread2 run is over" );
   }
}
public class DemoThread
{
    public static void main(String args[])
    {
       ArrayList<user> list = new ArrayList<user>();
       Thread1 t1 = new Thread1(list);
        Thread2 t2 = new Thread2(list);
       try
       {
          while(t1.mythread.isAlive() || t2.mythread.isAlive())
          {
            System.out.println(" Size : " + list.size());
            Thread.sleep(1);
          }

       }
       catch(InterruptedException e)
       {
          System.out.println("Main thread interrupted");
       }
       System.out.println("Main thread run is over" );
    }
}

After 6

import java.io.*;
import java.util.*;

class user{
   public String name;
}

class Thread1 implements Runnable
{
   Thread mythread ;
   ArrayList<user> list;
   Thread1(ArrayList<user> l)
   {
     list = l;
      mythread = new Thread(this, "Thread1");
      System.out.println("Thread1 created");
      mythread.start();
   }
   public void run()
   {
      try
      {
        for (int i=0 ;list.size()<1000;i++)
        {
          while(list.size() == 1000){
             Thread.sleep(1);
          }
          list.add(new user());
          System.out.println("Adding items " + i);
         
         
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("Thread1 interrupted");
     }
     System.out.println("Thread1 run is over" );
   }
}

class Thread2 implements Runnable
{
   Thread mythread ;
   ArrayList<user> list;
   Thread2(ArrayList<user> l)
   {
      mythread = new Thread(this, "Thread2");
      System.out.println("Thread2 created");
      list = l;
      mythread.start();
   }
   public void run()
   {
      try
      {
        for (int i=0 ;i<1000;i++)
        {
          System.out.println("Removing objects " + i);
          while (list.size() == 0){
              Thread.sleep(1);
          }
          list.remove(list.size() -1);         
        }
     }
     catch(InterruptedException e)
     {
        System.out.println("Thread2 interrupted");
     }
     System.out.println("Thread2 run is over" );
   }
}
public class DemoThread
{
    public static void main(String args[])
    {
       ArrayList<user> list = new ArrayList<user>();
       Thread1 t1 = new Thread1(list);
        Thread2 t2 = new Thread2