【实现】
Java代码
1 package com.mypack.thread;
2
3 public class Student {
4
5 private String name;
6 private String sex;
7 private int age;
8
9 public String getName() {
10 return name;
11 }
12
13 public void setName(String name) {
14 this.name = name;
15 }
16
17 public String getSex() {
18 return sex;
19 }
20
21 public void setSex(String sex) {
22 this.sex = sex;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32
33 }
Java代码
34 package com.mypack.thread;
35
36 importjava.util.ArrayList;
37 import java.util.List;
38
39 /**
40 * <ul>
41 * 学生线程
42 * </ul>
43 *
44 * @author liudong
45 *
46 */
47 public class StudentThreadextends Thread {
48
49 private List<Student> students = newArrayList<Student>();
50
51 public StudentThread(List<Student>students) {
52 this.students = students;
53
54 }
55
56 public void run() {
57
58 for (Student student : students) {
59 System.out.println("当前线程:" +Thread.currentThread().getName());
60 System.out.println("name =" + student.getName() + " age = "
61 + student.getAge() + "sex = " + student.getSex());
62 // 导入学生信息到数据库中
63 }
64 }
65
66 }
Java代码
67 package com.mypack.thread;
68
69 importjava.util.ArrayList;
70 import java.util.List;
71
72 /**
73 *
74 * <ul>
75 * 测试类
76 * </ul>
77 *
78 * @author liudong
79 *
80 */
81 public class Test {
82
83 public static void main(String[] args){
84
85 Test test = new Test();
86 // 查询所有的学生信息
87 List<Student> students =test.getAllStudents();
88 // 定义一个临时集合,用于存放学生信息
89 List<Student> tempStudents = newArrayList<Student>();
90
91 for (int i = 0; i < students.size();i++) {
92 tempStudents.add(students.get(i));
93 if (i != 0 && i % 50000 ==0) {
94 test.createThread4ExportStudentInfo(tempStudents,i);
95 }
96 }
97
98 test.createThread4ExportStudentInfo(tempStudents,students.size());
99
100 }
101
102 /**
103 * <li>创建线程,导入学生信息</li>
104 *
105 * @param tempStudents
106 * 每个线程,需要导入的学生信息的数量
107 * @param i
108 */
109 public voidcreateThread4ExportStudentInfo(List<Student> tempStudents, int i) {
110
111 List<Student> students = newArrayList<Student>();
112
113 for (Student student : tempStudents) {
114 students.add(student);
115 }
116
117 StudentThread studentThread = newStudentThread(students);
118 // 设置线程名称
119 studentThread.setName("Thread-"+ i);
120 // 启动线程
121 studentThread.start();
122
123 // 重新生成一个新的临时学生集合。
124 tempStudents = newArrayList<Student>();
125 }
126
127 /**
128 * <li>查询所有的学生信息</li>
129 *
130 * @return
131 */
132 public List<Student> getAllStudents(){
133
134 List<Student> students = newArrayList<Student>();
135
136 for (int i = 1; i <= 100000; i++){
137 Student student = newStudent();
138
139 student.setName("liudong"+ i);
140 student.setAge(i);
141 student.setSex(i + "");
142
143 students.add(student);
144 }
145
146 return students;
147 }
148 }