I'm new to Java threads. I'm solving a question where I've been given a Solution.java class which executes 3 java threads from 3 different Thread extended classes that I've wrote. Following is the way that the predefined class is using to start threads:
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());
}
}
Aim of the question is to run task1 and task2 simultaneously. And then run task3 after complete execution of both task1 and task2. Following is my thread classes implementation:
class Task1 extends Thread{
public static int a;
public static int beg;
@Override
public void run(){
for(int k=beg;k<a;k++){
int idx = Integer.parseInt(Solution.i);
Solution.threadArray[idx] = k;
idx++;
Solution.i = Integer.toString(idx);
}
}
}
class Task2 implements Runnable{
public static int a;
public static int beg;
@Override
public void run(){
for(int k=beg;k<a;k++){
int idx = Integer.parseInt(Solution.i);
Solution.threadArray[idx] = k;
idx++;
Solution.i = Integer.toString(idx);
}
}
}
class Task3 implements Runnable{
public static int a;
public static int beg;
@Override
public void run(){
for(int k=beg;k<300;k++){
int idx = Integer.parseInt(Solution.i);
Solution.threadArray[idx] = k;
idx++;
Solution.i = Integer.toString(idx);
}
}
}
The issue is that only task1 is executing. No other thread is running and I don't understand why. I'll be really glad if someone could help me out with this one.
Edit: I've added the complete Solution.java class. Also I've changed a little bit of code in classes implemented by me. I'm sorry for not testing it earlier, but all the threads are running. Only issue is that for loop in second thread is not running. I used System.out.println inside and outside the for loop and that's how I got to know it.