import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.Callable;
public class GetCdrMultithreading {
class TaskWithResult implements Callable<String[]> {
private int id;
String[] values;
public TaskWithResult(int id, String[] values) {
this.id = id;
this.values = values;
}
@Override
public String[] call() throws Exception {
String[] res = new String[] { "1", "2" };
GetCdrMultithreading getCdrMultithreading=new GetCdrMultithreading();
getCdrMultithreading.writeCsv(values, ".csv","e:/rId/","msisdn_");
return res;
}
}
public static class MyThread implements Runnable {
int count = 1, number;
String[] valuea;
public MyThread(int num, String[] value) {
number = num;
valuea = value;
System.out.println("创建线程 " + number);
}
public void run() {
// 实现了接口的run()方法
System.out.println("线程 " + number + ":计数 " + count);
String[] values = { "123", "234", "456", "567", "678" };
GetCdrMultithreading getCdrMultithreading=new GetCdrMultithreading();
getCdrMultithreading.writeCsv(valuea,".csv","e:/rId/",values[num - 1] + "_" + num + "");
return;
}
}
public void start() throws Exception {
GetCdrMultithreading aw = new GetCdrMultithreading();
String[] value1 = { "asdf", "aadf", "adf" };
String[] valu = new String[6];
String[] a = new TaskWithResult(100000000, value1).call();
aw.write();
}
public void write() {
String[] value1 = { "asdf", "aadf", "adf" };
String[] value2 = { "bsdf", "badf", "bdf" };
String[] value3 = { "csdf", "cadf", "cdf" };
String[] value4 = { "dsdf", "dadf", "ddf" };
String[] value5 = { "esdf", "eadf", "edf" };
for (int i = 0; i < 6666; i++) {
System.out.println(i);
if (i == 0) {
String[] values = value1;
new Thread(new MyThread(i + 1, values)).start();
}
if (i == 1) {
String[] values = value2;
new Thread(new MyThread(i + 1, values)).start();
}
if (i == 2) {
String[] values = value3;
new Thread(new MyThread(i + 1, values)).start();
}
if (i == 3) {
String[] values = value4;
new Thread(new MyThread(i + 1, values)).start();
}
if (i == 4) {
String[] values = value5;
new Thread(new MyThread(i + 1, values)).start();
}
}
}
public String[] writeCsv(String[] values,String suffix,String path,String name) {
String[] result = new String[2];
result[0] = name;
String fileName = name;
FileWriter fileWriter = null;
File csv = new File(path+"/"); // CSV文件
if (!csv.exists()) {
csv.mkdirs();
}
try {
fileWriter = new FileWriter(path+"/"+fileName+suffix);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer bw = new StringBuffer();
// 新增一行数据
// bw.newLine();
for (int i = 0; i < values.length; i++) {
bw.append(values[i]);
bw.append("\r\n");
}
try {
fileWriter.write(bw.toString());
fileWriter.flush();
fileWriter.close();
String fileNameT=name+"_"+".chk";
fileWriter = new FileWriter(path+"/"+fileNameT);
fileWriter.write("file_name:"+fileName+"\r\n");
fileWriter.write("row_num:"+values.length+"\r\n");
fileWriter.write("file_time:");
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return result;
}
}