Hi all,

I have to write a program which utilizes threads. In my program I have to connect to a Database and execute a massive number of UPDATES on a table--arround 2000 to 5000 records needs to be updated.

I am planning to use threads. So, I can spawn multiple threads to optimize the process.

I have some questions and I hope someone shed light on this area.


Senario:
I have a list of Ids (2000 to 5000) in a Database table and I need to read those values and UPDATE another table based on those values read from the first table,

If I open two threads, how can I pass/read the list of values from my first table to the program/threads?

Should I read/load them in memory first, in some Array or HashMap or something?

Please advise...

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 could also create multiple timers and register an action listener on your class.

The Timer object is meant to activate the action listener of the class in the specified milliseconds.

So you can have one actionlistener and multiple timers with different commands.

About your question of whether you need an array or not... I really doubt you should need your array if, for example, you are directly copying and pasting a table of section A to another table of section A for example. If you're going to be placing values in the exact same section of another table, there should be no reason for additional work of storing values that will be in similar locations.

If you have a way to access the section in your table (for example, by name or number) and the names of the first table are the same as the second, then I suppose you could store - in advance - the names for the table the thread will allocate its next value to then iterate through the names and repeat the process.

I haven't played around with the callable interface that Ezzaral suggested, but I doubt he'd give you any bad advice. My first bet would be to keep a list of references to your table (divided into different array, equal to the amount of threads you want to use) then have each thread iterate through each section.

Note: I suppose you could implement the callable interface in this way--

public class MyClass<T> implements Calllable<T>

and for an example I will use a fictitious array and thread object that will be associated with each other--

//assuming you have an inner class thread--
   MyThread thread2 = null;
   ArrayList<T> calledValues2 = null; //will use stored information for tables
   int indice = 0;

--and you implement the Callable<T> interface in your MyThread class and do something like this--

public T call() throws Exception
{
        return calledValues2.get(indice++);
       //will retrieve the stored information and incrmeent indice
}

--and just keep calling it for the specified Thread class.

Hopefully that wasn't too confusing. If you understand Threads and how to connect to a Database I assume you will understand.

Alex,

Thanks for the information:

>>My first bet would be to keep a list of references to your table (divided into different array, equal to the amount of threads you want to use) then have each thread iterate through each section.


That's what I intend to do. Say if I load all my Ids in memory, say in an arrayList or HashMap, equal to the number of my threads. Then, when I call each thread, who do I pass the corresponding list of Ids to the thread.

e.g. here's how I call the thread

public static void main(String[] args) {
	for(int i = 0; i < 2; i++) {
		new ClassThreaded().start();        
	}
}

Can you elaborate on how to pass a list of Ids into each thread?


Thanks,

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 is get the right one for the range of tables you're updating--

//declared somewhere globally
ArrayList<String[]> arrayOfIDs;

and reference the particular array in this way--

//in your thread class
int indice=0;

public String getTableID()
{
  
      return arrayofIDs.get(getNumber())[indice++];
}

Thanks, Alex. Let me give it a shot, and will update this thread accordingly, afterward.

This article has been dead for over six months. Start a new discussion instead.