我有以下方法来创建transactioID,我想知道它是否在多线程执行时创建相同的id 使用该方法我会遇到什么样的问题?
private String getTransactionId(){
StringBuffer buf = new StringBuffer("");
buf.append(this.getPrefix());
buf.append(this.getApplicationId());
buf.append(this.getThreadId());
buf.append(System.currentTimeMillis());
try{
Thread.sleep(1);
}
catch(Exception e){
}
while(buf.length()<19){
buf.append("0");
}
return buf.toString();
}
最佳答案
如果我们可以假设 this.getPrefix()、getApplicationID() 和 getThreadID() 方法本身是线程安全的,那么此方法就是线程安全的。它没有访问该对象的任何成员。
StringBuffer 对象只能通过调用线程堆栈上的 buf
变量访问,因此只能通过创建它的线程访问。
关于java - 交易ID创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25643358/