Multithreading with an database (mysql or oracle)
Am writing a multithreading application that has several threads(approxiamately 25) with each thread performing a specific process and then updating the database in the which then gives the next thread the permission to process another pr开发者_StackOverflow中文版ocess and do the same. Basically, thread1 does process then updates db as complete then when thread two reads the db as complete it begins processing and the process continues until thread 25. Anyone know how this is possible in java?
You can retrieve data (using select query) with a simple logic in each threads, but for updating the data, it is recommended to execute the query in a synchronized block. Since other threads are dependent on the data being modified by this thread, it may cause problem while fetching the records.
Why 25 exactly?
Anyway what you are saying is certainly possible but you might want to create a special thread which gets the resulting value each thread has computed and submit it to the database.
That way you need to have one thread which may access the database and all the other threads don't have to wait for the database.
Your question is incredibly general but the approach I'd take would be something like:
designate one thread as the controller thread. It's job is to listen for the worker threads to complete their processing. The simplest way to do with is with a semaphore object and the
wait
/notify
methods - the controller thread would take a lock on the semaphore and then callwait
.create your worker threads, each with its own semaphore object against which each thread takes a lock on and again calls
wait
.the trigger to start the processing (this could be the application running, the user clicking a button, etc) obtains a lock on the controller's semaphore and calls
notify
against it waking the controller thread. The controller's job is to pick one of the worker threads from the pool, obtain a lock on its semaphore and the callnotify
causing the worker to awake. The controller then callswait
on its own semaphore.the worker thread can then read the database, does the processing and write back to the database before it calls
notify
on the controller's semaphore causing the process to start again with the controller callingnotify
on one of the worker threads' semaphores andwait
against its own .
Finally a word of warning, this is a very brief outline of what's required to implement the general behaviour you've described. Threading is possbily the most misunderstood topics in computer science imho and very very easy to get wrong. Before leaping into a multi-threaded system make sure at the very least you've read Brian Goatz's - Java Concurrency In Practice
I am not sure I get your requirements fully, but here is a high level algorithm:
Create the required number of threads and put them in an Array.
Now starting from the second thread to the last, each thread must do a
join()
on the previous thread; this will facilitate that each thead runs it processing and updating part after its previous has finished.
Possible code:
Thread class declaration
class WaitingThread extends Thread
{
private Thread previousThread;
public WaitingThread(final Thread previous)
{
this.previousThread = previous;
}
public void run()
{
doParallelTask();
if(previousThread != null)
{
previousThread.join();
}
doProcesingAndUpdating();
}
}
The array declaration and initialisation;
final Thread [] threads = new Thread[25];
for(int i = 0; i < 25; i++)
{
if(i == 0)
{
threads[i] = new WaitinThread(null);
}
else
{
threads[i] = new WaitingThread(threads[i - 1]);
}
}
Hope this helps.
PS: I must admit, in the above model it makes more sense to do the doParallelTask()
using multiple threads, and doing the 25 doProcessingAndUpdate()
using a single thread.
精彩评论