In order to avoid repetitive execution of tasks in Java multithreaded execution, shared member variables can be processed through the Synchronized keyword, with the following code:
//Multithreading task Method
Private voidProcessmultitask (FinalList<logstattask> list,intThreadCount)throwsException {
Process Task List based on number of threads set
for(inti = 0; i < ThreadCount; i++) {Taskthread thread=Newtaskthread (list); Thread.Start (); } }
//Task Processing thread class classTaskthreadextendsThread {PrivateList<logstattask> taskList =NULL;
//Create constructors based on thread class member variables PublicTaskthread (list<logstattask>taskList) { Super(); This. taskList =taskList; } Public voidrun () { while(true) {Logstattask task;
//Lock the Tasklist Task table object to ensure that only one thread is manipulating it synchronized(taskList) {
//If the task list is empty, exit execution if(Collectionutils.isempty (taskList)) { Break; }
//Remove a pending task from the Task task list and remove the task from the Tasks table= Tasklist.remove (0); } //Release the lock on Tasklist so that other threads can operate on it, and this thread will handle it according to the task accordingly } } }
Java multithreaded execution tasks, handling security issues for shared member variables