0
  1. I have to insert the elements using three threads by creating three classes, namely Task1,Task2 and Task3. The values to be inserted into the array are 0,1,2,....299.

  2. Override the run method in the threads. Three integer i,j, and k representing the number of elements each thread should append inside the given array.

Thread one should append 0 to i-1 inside the array,thread two should append i to i+j-1 inside the array,and the third thread should append i+j to 299 inide the array.

  1. Threads one and two must run simultaneously, and the values of the threads one and two must be inserted inside the indices of the array from 0 to i+j-1 randomly.The third thread should start only after the first two threads have been executed completely.

  2. In these code three task are given. first task and second task start executing the thread at the same time and after completion of first two task then only third task start. If these situation getting correct then test() method return true.

  3. public static final int[] threadArray = new int[300]; how I add random number into these array using Task1 Task2 and Task3 class.

Input :

80

130

90

Output :

True

     import java.util.Scanner;

   class Task1 extends Thread
   {
    static int a = 0;
    static int beg = 0;
    
    public void run() 
    {
        for(int i=a;i<=beg;i++)
        {
            Solution.threadArray[i] = i;  
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
     }
   }

   class Task2 extends Thread
   {
     static int a = 0;
     static int beg = 0;
    
    @Override
    public void run() 
    {
        // TODO Auto-generated method stub
        for(int i=a;i<=beg;i++)
        {
            Solution.threadArray[i] = i;
        }   
     }
    
   }

   class Task3 extends Thread
   {
    static int a = 0;
    static int beg = 0;
    public void run() 
    {
        // TODO Auto-generated method stub
        for(int i=a;i<=beg;i++)
        {
            Solution.threadArray[i] = i;
        }   
     }
    

   }

   public class Solution 
   {
      public static final int[] threadArray = new int[300];
    
    public static volatile String i = 0+"";

    public boolean test() throws InterruptedException
    {
        Task1 task1 = new Task1();
        Task2 task2 = new Task2();
        Task3 task3 = new Task3();
        Thread task2Thread = new Thread(task2);
        Thread task3Thread = new Thread(task3);
        task1.start();
        task2Thread.start();
        task1.join();
        task2Thread.join();
        task3Thread.start();
        int first = Task1.a+Task2.a;
        int containsSecondThread = Task1.a;
        String oneAndTwo = "";
        String sizeOfTask1 = "";
        for(int i=0;i<first;i++)
        {
            oneAndTwo += threadArray[i]+" ";
        }
        for(int i=0;i<containsSecondThread;i++)
        {
            sizeOfTask1 += threadArray[i]+" ";
        }
        int begOfTask3 = Task3.beg;
        String checkingString = "";
        for(int i=begOfTask3;i<threadArray.length;i++)
        {
            checkingString += i + " ";
        }
        String task3String = "";
        for(int j = begOfTask3;j<threadArray.length;j++)
        {
            task3String += threadArray[j]+" ";
        }
        if((!oneAndTwo.contains(begOfTask3+"") && sizeOfTask1.contains(Task2.beg+"")) || task3String.equals(checkingString))
        {
            return true;
        }
        return false;
    }
    
    public static void main(String[] args) throws InterruptedException 
    {
        Scanner sc= new Scanner(System.in);
        
        Solution solution = new Solution();
        int one = sc.nextInt();
        Task1.a = one;
        Task1.beg = 0;
        int two = sc.nextInt();
        Task2.a = two;
        Task2.beg = one;
        int three = sc.nextInt();
        Task3.a = three;
        Task3.beg = one+two;
        System.out.print(solution.test());
    }
   }
shs
  • 21
  • 10

2 Answers2

1

First, some observations regarding your code: Instead of using static variables in the classes (i.e., Task1, Task2, and Task3) that extend the class Thread (to understand why have a look at Why are static variables considered evil?):

static int a = 0;
static int beg = 0;

use non-static final fields, and initialize them via the constructor:

class Task1 extends Thread
{
    private final int begin;
    private final int end;

    Task1(int begin, int end){
        this.begin = begin;
        this.end = end;
    }

     public void run(){
        for(int i=begin; i<= end; i++)
           ....
    }
}

adapt the main method accordingly:

public static void main(String[] args){
       ...
       Task1 task1 = new Task1(begin, end);
}

and then pass the tasks-related objects as parameters of to the test method:

public boolean test(Task1 task1, Task2 task2, Task3 task3){ 
      ...
}

For the concatenation of the strings use StringBuilder:

StringBuilder oneAndTwo = new StringBuilder();
for(int i=0;i<first;i++)
{
    oneAndTwo.append(threadArray[i]).append(" ");
}

This looks wrong:

    Task1.a = one;
    Task1.beg = 0;

by looking at the loop of the run method from Task1, this means that, if Task1.a is not a negative number, then Task1 will not do any work.

To use the threads to generate the random values of the array:

int[] threadArray = new int[300];

you can start by extracting a method to generate those random values, based on formula:

 r.nextInt(high-low) + low;

this formula generates a random value between low and high.

Adapt the tasks, accordingly:

class Task1 extends Thread
{
    private final Random random_values = new Random();
    private final int low;
    private final int high;
    ...
    
    public int generate_random(){
           return r.nextInt(high-low) + low;
    }

    public void run()
    {
        for(....)
        {
            Solution.threadArray[i] = generate_random();
            ...
        }
    }
}

Make sure to pass to the threads the information about the range of the random values to be generated (i.e., the low and high parameters), and the reference to the array that will be filled up with those random values (i.e., array int[] threadArray) . Also make sure that you split the iterations int[] threadArray among the threads. Therefore, each thread should generate a chunk of the random values. An example of such distribution would be:

Thread 1 : 0 to 100;
Thread 2 : 100 to 200;
Thread 3 : 200 to 300;

You can make this more robust and divide the array length by the number to threads and assign the work among threads, accordingly.

I could have provided you with the entire solution, but I feel that is better instead if I give you the pointers so that you can do it in your own.

EDIT: Based on the new edit of your question:

You just need to adapt the Task classes as follows:

class Task1 extends Thread {
    static int a = 0;
    static int beg = 0;

    public void run(){
        for(int i=beg;i < a;i++)
            Solution.threadArray[i] = i;
    }
}

class Task2 extends Thread {
    static int a = 0;
    static int beg = 0;
    
    public void run(){
        for(int i=beg; i< beg + a;i++)
            Solution.threadArray[i] = i;
    }
}

class Task3 extends Thread{
    static int a = 0;
    static int beg = 0;
    public void run(){
        for(int i=beg;i< a + beg;i++)
            Solution.threadArray[i] = i;
    }
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
1

Thread1 and Thread2 are supposed to access Common Resource in threadArray[0... Task1.a+Task2+a]. So we have to make use of static volatile variable i declared in Solution Class.

class Task1 extends Thread
{
    static int a=0,beg=0;
    public void run()
    {
        int k=Task1.beg;
        int i1=0;
        while(i1<Task1.a)
        {
            Solution.threadArray[Integer.parseInt(Solution.i)]=k++;
            int a1=Integer.parseInt(Solution.i);
            a1++;i1++;
            Solution.i=a1+"";
            try{
                Thread.sleep(1);
            }
            catch(InterruptedException e){}
        }
    }
}
class Task2 extends Thread
{
    static int a=0,beg=0;
    public void run()
    {
        int y=0;
        int k=Task2.beg;
        while(y<Task2.a)
        {
            Solution.threadArray[Integer.parseInt(Solution.i)]=k++;
            int a1=Integer.parseInt(Solution.i);
            a1++;y++;
            Solution.i=a1+"";
            try{
                Thread.sleep(1);
            }
            catch(InterruptedException e){}
        }
    }
}

Thread3 work independently after First 2 threads complete.

class Task3 extends Thread
{
    static int beg=0,a=0;
    public void run()
    {
        for(int i=Task3.beg;i<Task3.beg+Task3.a;i++)
            {
                Solution.threadArray[i]=i; 
            }
        
    }
}
Atul kumar
  • 11
  • 1