[英]issues with concurrent update in titan graph database
public class a {
private static TitanGraph titanGraph = null;
static GraphTraversalSource traversalSource = null;
public static void main(String a[])
{
titanGraph = TitanFunctions.getTitanGraph();
traversalSource = titanGraph.traversal();
// Task to be executed by each thread
Runnable r = new Runnable() {
public void run() {
long ab = traversalSource.V().has("RequestJob", "RequestId", 203)
.has("JobLockStatus","F")
.property("JobLockStatus", "B").count().next();
titanGraph.tx().commit();
System.out.println("value: "+ab+" : " +Thread.currentThread().getName());
}
};
Thread t1 = new Thread(r, "T1");
Thread t2 = new Thread(r, "T2");
t1.start();
t2.start();
}}
在上面的程序中,兩個並發線程試圖將相同的RequestId=203
的值從“ F”更新為“ B” 。
似乎兩個線程都將狀態值獲取為“ F”並將其更新為B。 但是我希望只有一個線程應該將值從“ F”更改為“ B”。 更新值之后,我還使用了commit()保存更改。
如果有任何線程將狀態從“(F)ree”更改為“(B)usy”,則其他線程應看到更改后的值。( “ B” )。
請幫助我解決這個問題。
您可以使用互斥量或synchronized()
塊來確保在任何給定時間只有一個線程可以執行該操作。 類似以下內容可能會起作用:
titanGraph = TitanFunctions.getTitanGraph();
traversalSource = titanGraph.traversal();
Runnable r = new Runnable() {
public void run() {
synchronized(titanGraph){
long ab = traversalSource.V().has("RequestJob", "RequestId", 203)
.has("JobLockStatus","F")
.property("JobLockStatus", "B").count().next();
titanGraph.tx().commit();
}
}
};
Thread t1 = new Thread(r, "T1");
Thread t2 = new Thread(r, "T2");
t1.start();
t2.start();
因此,上面的synchronized(titanGraph)
塊基本上表明,對於該塊內的任何內容,它只能由在括號內鎖定對象的線程執行。 在這種情況下, titanGraph
。 如果線程沒有鎖,則它等待鎖可用。
這是有關使用synchronized
的非常好的教程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.