Test
public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
The larger the data, the more threads
for (int i = 0; i < 3000000; i++) {
List.add ("Hello" +i);
}
try {
exec (list);
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
public static void exec (List<string> List) throws interruptedexception{
int count = 300;//300 data for one thread processing
int listsize = List.size ();//data collection size
int runsize = (listsize/count) +1; Number of threads turned on
list<string> NewList = null;//Store execution data for each thread
Executorservice executor = Executors.newfixedthreadpool (runsize);//Create a thread pool with the same number as the number of open threads
Create two counters
Countdownlatch begin = New Countdownlatch (1);
Countdownlatch end = new Countdownlatch (runsize);
Looping to create a thread
for (int i = 0; i < runsize; i++) {
Calculate the data that each thread executes
if ((i+1) ==runsize) {
int startIndex = (i*count);
int endindex = List.size ();
Newlist= list.sublist (StartIndex, endindex);
}else{
int startIndex = (i*count);
int endindex = (i+1) *count;
Newlist= list.sublist (StartIndex, endindex);
}
Thread class
Mythread mythead = new Mythread (newlist,begin,end);
The way the thread is executed here is the Executor.execute (Mythead) method of the calling line Cheng Chili.
Executor.execute (Mythead);
}
Begin.countdown ();
End.await ();
Finish closing the thread pool
Executor.shutdown ();
}
Thread classes are not written separately, written in the same file, there is a need to write separately
private static class Mythread implements runnable{
Private list<string> List;
Private Countdownlatch begin;
Private Countdownlatch end;
Create a constructor initialization list, and other parameters used
Public Mythread (list<string> list,countdownlatch Begin,countdownlatch end) {
This.list = list;
This.begin = begin;
This.end = end;
}
@Override
public void Run () {
try {
for (int i = 0; i < list.size (); i++) {
Here again, because in the real project, when the data being processed has a wait timeout and an error causes the thread to remain in the waiting state
This is just a simple process,
System.out.println (List.get (i));
}
//....
Run the thread straight into the wait
Begin.await ();
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
Here is the main, when a thread is done counting to reduce a don't this thread will be hung all the time
, End.countdown (), this method is to directly reduce the counter by one
End.countdown ();
}
}
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