没有加join是无能执行多线程的
代码如下
@Test
public void multInsert()throws Exception{
final Connection conn =HbaseUtils.conn();
final String tableName = “ns1:t2”;
Thread t1 =new Thread(“thread1”){
public void run() {
String inset = inset(0, 300, tableName,conn);
System.out.println(inset);
}
};
Thread t2 =new Thread(“thread2”){
public void run() {
inset(300,600,tableName,conn);
}
};
Thread t3 = new Thread(“thread3”) {
public void run() {
inset(600, 1000, tableName, conn);
}
};
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
}
//百万插入
private String inset(int start,int end,String tableName,Connection conn ) {
try {
TableName tname = TableName.valueOf(tableName);
HTable table = (HTable) conn.getTable(tname);
DecimalFormat df = new DecimalFormat("0000");
table.setAutoFlush(false);
for (int j = start; j < end; j++) {
byte[] rowkey = Bytes.toBytes("row" + df.format(j));
Put put = new Put(rowkey);
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("id"), Bytes.toBytes(end - 1));
put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("name" + j));
table.put(put);
if (j % 2000 == 0) {
table.flushCommits();
}
}
table.flushCommits();
}catch(Exception e){
log.error("插入异常:{}",e);
}
return "完成:"+(end-start);
}