六: 高併發下如何安全修改同一行數據

多個用戶下訂單, 生成全局自增的訂單ID 定義全局變量 a =0, 啓動50 個線程 生成50 個訂單id 時 a+1, 會有重複的訂單id 出現。 線程不安全 解決: 一:基於 jvm 解決方式。 1.將全局變量, 自增時得代碼塊 加Synchorized 關鍵字 2. lock lock= new ReentranLock() try{ lock.lock() 將全局變量, 自增時得代碼塊 }
相關文章
相關標籤/搜索