Use of Java Multi-threading

Source: Internet
Author: User

Recently in the project using a multi-threading technology, feel the data a lot of batch processing effect is good, so record down here, give yourself a day, also share to everyone!


1. First to get your data set DataList (shown here in DataList)

1.1 People think that if you get very little data, there's no need to use multithreading.


2. Use int threadnum = Runtime.getruntime (). Availableprocessors (); Get the number of your CPU cores

2.1 for the number of cores to do their own instructions, when they were doing, looked at some of the use of CPU core number of articles

Some experts do some performance testing, said when the number of cores called more time, the processing performance improvement is very good, but more time, has not so obvious effect (like remember that the Master borrowed from an IBM article to say), this can be another search query, the individual is to 4 as the standard to do, You can find your own decision.


3. Calculate the amount of data each thread needs to process according to Datalistsize and Threadnum

3.1 Related codes

int threadlistsize = datalistsize;
if (listsize<threadnum)
{
Threadnum=1;
}
else if (threadnum>4)
{
threadnum=4;
Threadlistsize = Listsize/threadnum;
}
Else
{
Threadlistsize = Listsize/threadnum;
}
Final int[] DataRange = new Int[threadnum];
for (int i = 0; i < threadNum-1; i++) {
Datarange[i] = Threadlistsize * (i + 1);
}

Some cannot be broken at once, so the last DataRange must contain all the remaining
Datarange[threadnum-1] = listsize;

3.2 Some notes

People may ask me what this is about, and I'll explain it slowly. Because the data collection dataList only one, but you need to do multithreading, then what? Divide this data collection DataList into several data sets based on the number of CPU cores (threadnum)?

No, performance is too poor, directly according to the index of the list to do, such as List.get (0), the index directly into the corresponding parts, and then DataList directly call


4. Start multithreading

4.1 Related codes


4.1.1 First part

Executorservice executor = Executors.newfixedthreadpool (threadnum);
Future<jsonarray>[] results = new Future[threadnum];
for (int i = 0; i < threadnum; i++) {
int startindex=0;
if (i!=0)
{
STARTINDEX=DATARANGE[I-1];
}
Results[i]=executor.submit (New Countertask (StartIndex, datarange[i]));
}

After each thread has finished processing, the returned results are returned to future<jsonarray>[]

Jsonarray resultarray=new Jsonarray ();
for (int i = 0; i < threadnum; i++)
{
Resultarray.addall (Results[i].get ());
}

Don't forget that.
Executor.shutdown ();


4.1.2 Part II (this can be handled directly as an internal class, without having to build a class)

Class Countertask implements callable<jsonarray>{

private int startIndex;
private int endIndex;

Public countertask (int startindex,int endIndex) {
This.startindex=startindex;
This.endindex=endindex;
}
@Override
Public Jsonarray Call () throws Exception {
TODO auto-generated Method Stub
Return Mailcall (Startindex,endindex);
}
}

4.1.3 The third part of the code

protected Jsonarray mailcall (int startindex,int endIndex) {

Jsonarray resultarray=new Jsonarray ();


Jsonobject Result=null;
for (int i=startindex;i<endindex;i++)
{
Object Object=datalist.get (i)

Do your own thing according to your logic.

。。。。。

}

return resultarray;


5. Although it is over, there are still some notes to be made


5.1 In Mailcall this method, because it is multi-threading, so this can not be used in non-thread-safe sharing, such as

SimpleDateFormat, about how this is going to work out,

(1) Many people are using Apache Commons-lang package of Dateutils and Dateformatutils class, the methods of these two classes are thread-safe.

(2) In this method, a new SimpleDateFormat object (feel bad, if more than the performance, 1000 data do not new 1000 SimpleDateFormat objects .... )

(3) There is nothing else to say, you can search by yourself


6, first write don't know scatter feeling, slowly strengthen ^_^





This article is from "I am just a mortal" blog, please make sure to keep this source http://ybqin.blog.51cto.com/5922943/1621116

Use of Java Multi-threading

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.