In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about JAVA simulation multithreading how to send text messages to multiple users. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Code writing logic, assuming that you need to send text messages to 11 users, and each thread sends text messages to two users to achieve multi-thread concurrent processing.
Create an entity user
Package www.it.com.test;/** * @ author wangjie * @ date on 2019-11-20 17:10 * @ description * @ company Shiwen Software Company Limited * / 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 +'\'+ ", id=" + id +'}';}}
Business code
Package www.it.com.test;import java.util.ArrayList;import java.util.List;/** * @ author wangjie * @ date on 2019-11-20 17:09 * @ description * @ company Shiwen Software Co., Ltd. * / public class BranchThread {public static void main (String [] args) {/ / prepare data source List userList = getInitUser (); / / define how many users Integer count = 2 per thread / / determine how many threads are needed based on the number of users List lists = splitList (userList, count); / / transfer users to each thread to execute for (List list:lists) {UserThread userThread=new UserThread (list); Thread thread=new Thread (userThread); / / start thread thread.start ();}} / * initial message data * @ return * / public static List getInitUser () {ArrayList 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; } /** * 计算出需要多少个线程 * @param list * @param pageSize * @param * @return */ public static List splitList(List list, int pageSize) { int listSize = list.size(); int page = (listSize + (pageSize - 1)) / pageSize; List listArray = new ArrayList(); for (int i = 0; i < page; i++) { List subList = new ArrayList(); 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; }} 创建线程 package www.it.com.test;import java.util.List;/** * @author wangjie * @date 2019/11/20 17:03 * @description 给11个人分批发送短信 * @company 石文软件有限公司 */public class UserThread implements Runnable{ private List userList; public UserThread(List userList) { this.userList = userList; } public List getUserList() { return userList; } public void setUserList(List userList) { this.userList = userList; } @Override public void run() { userList.forEach(user ->{System.out.println ("thread" + Thread.currentThread (). GetId () + user.toString ());}); / / call the interface for sending text messages from a third party}}
The above is the JAVA simulation of how to send text messages to multiple users. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.