Here's how the skeleton of my program looks.
public class ClassThreaded extends Thread { public static void main(String[] args) { for(int i = 0; i < 2; i++) { new ClassThreaded().start(); } } public void run() { startDetaching (); } private void startDetaching () { SELECT .... FROM mytable WHERE activityid IN (select ids from another_table) while (colllection.next()) { // This will update only one record. "UPDATE ... WHERE ...; } } }
So, in this case, is there a possibility that both threads will try to update the same record? or will it work at all?
Please advise...
Thanks
You will most likely be updating the same fields, despite the fact that you're creating an instance of 2 threads, since they're of the same class.
Like previously stated, try to make an array with the Table's ID's known ahead of time and the make an array that holds arrays (a double array, or list) and section off your ID's in separate arrays.
For example, Tables 1000-2000 will be in one array, 2000-3000 in another... 3000-4000 in another, so on and so forth.
From there, upon creation of your class, have a static number increment upon creation--
static int creationValue = 0;
int classNumber = 0;
//in constructor
class MyThread()
{
classNumber = creationValue;
creationValue++;
}
public int getNumber()
{
return classNumber;
}
and when you need to get a value, you will have an array of arrays defined already, all you need to do …