Home>

Code writing logic,Suppose you need to send text messages to 11 users.Each thread sends a text message to two users,Implement multi-threaded concurrent processing

Create entity user

package www.it.com.test;
/**
 * @author wangjie
 * @date 2019/11/20 17:10
 * @description
 * @companyShiwen Software Co., Ltd.
 * /
public class user {
  private string name;
  private integer id;
  public string getname () {
    return name;
  }
  public void setname (string name) {
    this.name=name;
  }
  public integer getid () {
    return id;
  }
  public void setid (integer id) {
    this.id=id;
  }
  public user () {
  }
  public user (string name, integer id) {
    this.name=name;
    this.id=id;
  }
  @override
  public string tostring () {
    return "user {" +
        "name =" "+ name +" \ "" +
        ",}";
  }
}

Business code

package www.it.com.test;
import java.util.arraylist;
import java.util.list;
/**
 * @author wangjie
 * @date 2019/11/20 17:09
 * @description
 * @companyShiwen Software Co., Ltd.
 * /
public class branchthread {
  public static void main (string [] args) {
    //Prepare the data source
    list<user>userlist=getinituser ();
    //Define how many users each thread processes
    integer count=2;
    //Determine how many threads are needed based on the number of users
    list<list<user>>lists=splitlist (userlist, count);
    //Pass the user to each thread for execution
    for (list<user>list:lists) {
      userthread userthread=new userthread (list);
      thread thread=new thread (userthread);
      //start thread
      thread.start ();
    }
  }
  /**
   * Initial data
   * @return
   * /
  public static list<user>getinituser () {
    arraylist<user>list=new arraylist<>();
    for (int i=0;i<11;i ++) {
      user user=new user ();
      user.setid (i);
      string name=string.valueof (i);
      user.setname (name);
      list.add (user);
    }
    return list;
  }
  /**
   * Calculate how many threads are needed
   * @param list
   * @param pagesize
   * @param<t>
   * @return
   * /
  public static<t>list<list<t>
    int listsize=list.size ();
    int page=(listsize + (pagesize-1))/pagesize;
    list<list<t>>listarray=new arraylist<list<t>>();
    for (int i=0;i<page;i ++) {
      list<t>sublist=new arraylist<t>();
      for (int j=0;j<listsize;j ++) {
        int pageindex=((j + 1) + (pagesize-1))/pagesize;
        if (pageindex == (i + 1)) {
          sublist.add (list.get (j));
        }
        if ((j + 1) == ((j + 1) * pagesize)) {
          break;
        }
      }
      listarray.add (sublist);
    }
    return listarray;
  }
}

Create a thread

package www.it.com.test;
import java.util.list;
/**
 * @author wangjie
 * @date 2019/11/20 17:03
 * @description Send SMS to 11 people in batch
 * @companyShiwen Software Co., Ltd.
 * /
public class userthread implements runnable {
  private list<user>userlist;
  public userthread (list<user>userlist) {
    this.userlist=userlist;
  }
  public list<user>getuserlist () {
    return userlist;
  }
  public void setuserlist (list<user>userlist) {
    this.userlist=userlist;
  }
  @override
  public void run () {
    userlist.foreach (user->{
      system.out.println ("thread" + thread.currentthread (). getid () + user.tostring ());
    });
    //Call the interface of the third party to send SMS
  }
}
  • Previous Detailed use of @Value in Springboot
  • Next Custom implementation of Android BaseAdapter (generic adapter three)