單服務器防止生成重複訂單的方法

涉及到取餐號的問題,每一個商家都有相對應的取餐號,這時候很難用 blockqueue來實現,想了不少辦法,首先作測試,生成一堆訂單。,而後批量改變訂單狀態。java

class ThreadTest implements Runnable{

private String testString;
public ThreadTest(String test){
testString = test;
}

/**
* 重寫(Override)run()方法 JVM會自動調用該方法
*/
public void run() {
String token = "token=18f78d796311bd38&";
String uri="http://localhost:8080/noodle/payOrderStatus.json?"+token+"orderid="+testString+"&orderstatus=1";
try{
HttpClient httpClient=new DefaultHttpClient();

HttpGet httpGet = new HttpGet(uri);
HttpResponse resp=httpClient.execute(httpGet);

if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
BufferedReader reader=new BufferedReader(
new InputStreamReader(resp.getEntity().getContent())
);
StringBuffer result=new StringBuffer();
String inputLine=null;
while((inputLine=reader.readLine())!=null){
result.append(inputLine);
}
System.out.println(result.toString());
ResultModel<List<OrderDetail>> test = JSON.parseObject(result.toString(),new TypeReference<ResultModel<List<OrderDetail>>>(){});
System.out.println(test.getCode());
OrderDetail orderDetail = test.getData().get(0);
System.out.println(orderDetail.getDishtype());
}

}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

使用線程池來模擬
不用sychronized就會是有重複的數據產生,若是加上則不會產生重複數據,達到預期效果。json

相關文章
相關標籤/搜索