Java multithreaded execution tasks, handling security issues for shared member variables

Source: Internet
Author: User
Tags thread class

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.