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
}
}
Related articles
- Java multi-threaded keywords final and static
- Java simulation multi-threaded ticket grabbing code example
- Java multi-threaded concurrent programming and locking principle analysis
- How to solve the problem of java socket connection pool caused by too many threads
- Java multi-threaded simulation movie ticketing process
- Java multi-threaded synchronous execution in the specified order
- Java multithreaded deadlocks and how to avoid them
- Super detailed introduction to multithreading in java
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable