I am using threading in my code and initializing it inside a while loop.
it executed successfully for 31 iterations, however after that for 5 remaining iterations it throws null pointer exception.
threads[i] = new Thread(new MyTask(a, b, c, keyValuePairs, d)); threads[i].start();
and then using thread join
try {
for (Thread thread : threads) {
thread.join(); // Wait for each thread to finish
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Thread Execution failed");
}
full code snip
for (int j = 0; j < LoopCount; j++) {
if (cnt >= 15) {
op = 15;
} else {
op = cnt;
}
cnt = cnt - 15;
Thread[] threads = new Thread[op];
ResultSet resultSet = stmt.executeQuery(sqlQuery);
int i = 0;
long a = 0;
String b;
String c;
String d;
while (resultSet.next() && i < op) {
a = resultSet.getLong("a");
b = resultSet.getString("b");
c = resultSet.getString("c");
d = resultSet.getString("d");
sqlQuery3 = "UPDATE IBL_PAYMENT_STG_TBL SET RESPONSEFLAG = 'IP' WHERE xyz = " + a;
UpdateStmt.executeQuery(sqlQuery3);
threads[i] = new Thread(new MyTask(a, b, c, keyValuePairs, d));
threads[i].start();
i++;
}
resultSet.close();
try {
for (Thread thread : threads) {
thread.join(); // Wait for each thread to finish
}
} catch (Exception e) {
e.printStackTrace();``your text``
System.out.println("Thread Execution failed");
}
}
New contributor
Abhishek Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.