提交 cc97708a authored 作者: 黄夏豪's avatar 黄夏豪

增加了雪花算法测试类的注释

上级 676b0099
...@@ -3,6 +3,7 @@ package snowflake; ...@@ -3,6 +3,7 @@ package snowflake;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
* 描述:雪花算法测试线程
* @author HuangXiahao * @author HuangXiahao
* @version V1.0 * @version V1.0
* @class SnowFlakThread * @class SnowFlakThread
...@@ -11,8 +12,10 @@ import java.util.concurrent.atomic.AtomicLong; ...@@ -11,8 +12,10 @@ import java.util.concurrent.atomic.AtomicLong;
**/ **/
public class SnowFlakThread extends Thread{ public class SnowFlakThread extends Thread{
//雪花算法类
private SnowFlake snowFlake; private SnowFlake snowFlake;
//需要生成ID的数量
private long n; private long n;
public SnowFlakThread( SnowFlake snowFlake, long n) { public SnowFlakThread( SnowFlake snowFlake, long n) {
......
...@@ -66,7 +66,7 @@ public class SnowFlake { ...@@ -66,7 +66,7 @@ public class SnowFlake {
* @Author : HuangXiahao * @Author : HuangXiahao
* @Date : 2020/5/22 15:31 * @Date : 2020/5/22 15:31
*/ */
public synchronized long nextId(){ public long nextId(){
//获取当前时间戳 //获取当前时间戳
long currentTime = getNewTime(); long currentTime = getNewTime();
if (lastTimestamp==currentTime){ if (lastTimestamp==currentTime){
......
...@@ -9,6 +9,7 @@ import java.util.concurrent.Future; ...@@ -9,6 +9,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
* 描述:雪花算法测试类
* @author HuangXiahao * @author HuangXiahao
* @version V1.0 * @version V1.0
* @class TestSnowFake * @class TestSnowFake
...@@ -16,20 +17,6 @@ import java.util.concurrent.atomic.AtomicLong; ...@@ -16,20 +17,6 @@ import java.util.concurrent.atomic.AtomicLong;
* @data 2020/5/31 * @data 2020/5/31
**/ **/
public class TestSnowFlake { public class TestSnowFlake {
static class TimeAndQPS{
private double QPS;
private long time;
public TimeAndQPS(double QPS, long time) {
this.QPS = QPS;
this.time = time;
}
@Override
public String toString() {
return "每秒QPS:"+this.QPS+" 所用时间:" + this.time;
}
}
public static void main(String[] args) { public static void main(String[] args) {
//测试个数 //测试个数
...@@ -44,6 +31,14 @@ public class TestSnowFlake { ...@@ -44,6 +31,14 @@ public class TestSnowFlake {
} }
} }
/**
* @param num 测试数量
* @param threadNum 线程数量
* @param sequenceLength 序列号位数
* @Return : snowflake.TestSnowFlake.TimeAndQPS
* @Author : HuangXiahao
* @Date : 2020/6/8 11:24
*/
public static TimeAndQPS startWithThread(long num, int threadNum,long sequenceLength) { public static TimeAndQPS startWithThread(long num, int threadNum,long sequenceLength) {
ExecutorService executorService = Executors.newFixedThreadPool(threadNum); ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
SnowFlake snowFake = new SnowFlake(sequenceLength); SnowFlake snowFake = new SnowFlake(sequenceLength);
...@@ -51,11 +46,30 @@ public class TestSnowFlake { ...@@ -51,11 +46,30 @@ public class TestSnowFlake {
for (int i = 0; i < threadNum; i++) { for (int i = 0; i < threadNum; i++) {
executorService.submit(new SnowFlakThread(snowFake, num / threadNum)); executorService.submit(new SnowFlakThread(snowFake, num / threadNum));
} }
//暂主线程直到num个雪花ID生成完毕 //暂主线程直到num个雪花ID生成完毕
while (snowFake.getAtomicLong() != num) { while (snowFake.getAtomicLong() != num) {
} }
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
executorService.shutdown(); executorService.shutdown();
return new TimeAndQPS((num / (end - start) * 1.0) * 1000.0,end-start); return new TimeAndQPS((num / (end - start) * 1.0) * 1000.0,end-start);
} }
/*
* 用于存放测试数据
* */
static class TimeAndQPS{
private double QPS;
private long time;
public TimeAndQPS(double QPS, long time) {
this.QPS = QPS;
this.time = time;
}
@Override
public String toString() {
return "每秒QPS:"+this.QPS+" 所用时间:" + this.time;
}
}
} }
package snowflake;
/**
* @author HuangXiahao
* @version V1.0
* @class test
* @packageName snowflake
* @data 2020/6/5
**/
public class test {
public static int count = 0;
public static void main(String[] args) {
int target = 300000;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < target; i++) {
count++;
System.out.println("count:"+count);
}
}
});
thread.start();
int validNumber = count;
while (validNumber!=target){
System.out.println("");
validNumber = count;
}
System.out.println("OK");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论