11package example ;
22
3- import static org .junit .Assert .*;
4-
53import org .junit .Before ;
64import org .junit .Test ;
75
108import java .io .FileInputStream ;
119import java .io .InputStreamReader ;
1210import java .util .ArrayList ;
11+ import java .util .Collections ;
12+ import java .util .Comparator ;
1313import java .util .List ;
1414
15+ import static org .junit .Assert .assertEquals ;
16+
1517public class OneTest {
1618
17- @ Before
18- public void beforeFoo (){
19- File file = new File ("out.txt" );
20- if (file .exists ()){
21- file .delete ();
19+ // private String name ="11";
20+
21+ @ Before
22+ public void beforeFoo () {
23+ File file = new File ("out.txt" );
24+ if (file .exists ()) {
25+ boolean a = file .delete ();
26+ if (a ){
27+ System .out .println ("aaa" );
28+ }
29+ }
30+ }
31+
32+ private List <String > readOutFile () {
33+ File file = new File ("out.txt" );
34+ FileInputStream is ;
35+ List <String > list = new ArrayList <String >();
36+ try {
37+ if (file .length () != 0 ) {
38+ is = new FileInputStream (file );
39+ InputStreamReader streamReader = new InputStreamReader (is );
40+ BufferedReader reader = new BufferedReader (streamReader );
41+ String line ;
42+ while ((line = reader .readLine ()) != null ) {
43+ list .add (line );
44+ }
45+ reader .close ();
46+ is .close ();
47+ }
48+
49+ } catch (Exception e ) {
50+ e .printStackTrace ();
51+ }
52+ return list ;
53+
54+ }
55+
56+ @ Test
57+ public void testFoo () {
58+ One one = new One ();
59+ one .foo ();
60+ List <String > list = readOutFile ();
61+ assertEquals ("freets" , list .get (2 ));
62+
63+ int count = 3 ;
64+ sortByThread (list , count );
65+
66+ // boolean isSuccese = writeToOutFile("output.txt", list);
67+
2268 }
23- }
24-
25- private List <String > readOutFile () {
26- File file = new File ("out.txt" );
27- FileInputStream is ;
28- List <String > list = new ArrayList <String >();
29- try {
30- if (file .length () != 0 ) {
31- is = new FileInputStream (file );
32- InputStreamReader streamReader = new InputStreamReader (is );
33- BufferedReader reader = new BufferedReader (streamReader );
34- String line ;
35- while ((line = reader .readLine ()) != null ) {
36- list .add (line );
69+
70+ private void sortByThread (List <String > list , int threadNum ) {
71+ if (null == list ) {
72+ return ;
3773 }
38- reader .close ();
39- is .close ();
40- }
4174
42- } catch (Exception e ) {
43- e .printStackTrace ();
75+
76+ if (list .size () <= threadNum ) {
77+ sortList (list );
78+ }
79+
80+ sortList (list );
81+
82+ //分配每个线程处理的队列长度
83+ // int length = list.size() / threadNum;
84+ // boolean zhenchu = true;
85+ // if (list.size() % threadNum != 0) {
86+ // length += 1;
87+ // zhenchu = false;
88+ // }
89+
90+
91+ int times = 0 ;
92+ for (int num = 0 ; num < threadNum ; num ++) {
93+ times = list .size ()/threadNum ;
94+ }
95+
96+ if ( times == 0 ){
97+ System .out .println ("2345" );
98+ }
99+ sortList (list );
100+
44101 }
45- return list ;
46102
47- }
103+ private void sortList ( List < String > list ) {
48104
49- @ Test
50- public void testFoo () {
51- One one = new One ();
52- one .foo ();
53- List <String > list = readOutFile ();
54- assertEquals ("freets" , list .get (2 ));
55- }
105+ Collections .sort (list , new Comparator <String >() {
106+ public int compare (String o1 , String o2 ) {
107+ return o1 .compareTo (o2 );
108+ }
109+ });
110+ }
56111
57112}
0 commit comments