我试图将关系添加到我使用 Java 的 ExecutorService 并行创建的 Neo4j 图形中。我有两个问题。首先,当我的所有可运行文件都被提交时,我的程序会向前跳转并关闭事务。其次,如果我保持 Transaction 处于打开状态(通过事先无限,因此它不会关闭,因此问题并没有真正解决),当可运行对象被执行时,它们无法添加关系并且似乎被卡住了那些代码行。
private void createDB()
{
graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(Neo4j_DBPath);
registerShutdownHook( graphDB );
Transaction tx = graphDB.beginTx();
try
{
parser.read(File, graphDB);
parser.similirize();
while (Global.running) {
}
tx.success();
System.out.println("donedone");
}
finally
{
System.out.println("closing");
tx.finish();
}
}
read 解析一些文件并创建我的数据库。Similirize 进行 mxn 比较并将这些关系添加到图表中。我希望并行执行此操作。这就是我现在要做的事情:
public void similirize() {
System.out.println("starting ||");
final Node NoSim = graphDB.createNode();
NoSim.setProperty("type", "No Similarites");
int threads = Runtime.getRuntime().availableProcessors();
System.out.println(threads);
final ExecutorService service = Executors.newFixedThreadPool(threads);
for (final Reaction r: FailedRxns){
System.out.println("submitting runnable");
service.submit(new Runnable() {
@Override
public void run() {
System.out.println("running runnable");
try {
System.out.println("try");
Node SIM = Parser.this.mostSimilar(r);
while (!Thread.interrupted()) {
System.out.println("while");
if (SIM != null) {
System.out.println("if");
r.getUnderlyingNode().createRelationshipTo(SIM, RelTypes.SIMILAR_TO);
System.out.println("btwn the lines");
r.getUnderlyingNode().createRelationshipTo(SIM.getRelationships(Direction.OUTGOING).iterator().next().getOtherNode(SIM), RelTypes.INFERRED_IN);
System.out.println("made connection!");
} else {
System.out.println("else");
r.getUnderlyingNode().createRelationshipTo(NoSim, RelTypes.IN);
System.out.println("Couldn't make connection :(");
}
}
} finally {
service.shutdown();
}
}
});
}
}
我的输出看起来像:开始|| 提交runnable 提交runnable....(持续很长时间) running runnable try while if running runnable try while if running runnable try while if running runnable try while if
然后它永远被困在这里。
感谢你的帮助!