我从CreateOne
类调用方法Non-Activity
,CreateOne
方法需要在Object
执行后返回Task
。所以我在调用Task之后添加了以下行。
task.wait();
但它正在返回错误,如
object not locked by thread before wait()
这是我的完整代码:
public class MyStitchHelper
{
public Object CreateOne(Map<String, Object> map) throws InterruptedException
{
Object returnObject = null;
Document mapDoc = new Document(map);
ArrayList<Document> list = new ArrayList<>();
list.add(mapDoc);
Document itemsDocument = new Document("items", list);
PipelineStage itemsStage = new PipelineStage("literal", "", itemsDocument);
Document AuthDocument = new Document();
AuthDocument.append("database","my_db");
AuthDocument.append("collection", _collectionName);
PipelineStage AuthStage = new PipelineStage("insert", "mongodb-atlas", AuthDocument);
Task<List<Object>> task = _client.executePipeline(itemsStage, AuthStage);
task.wait();
if(task.isSuccessful())
{
Log.e("UserDAL", task.getResult().toString());
returnObject = task.getResult();
}
else
{
Log.e("UserDAL", "Error Adding Collectionsss");
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("UserDAL", "" + e.getMessage());
}
});
}
return returnObject;
}
}
通话方式:
public void AddUser()
{
MyStitchHelper DBHelper = new MyStitchHelper();
Map<String, Object> map = new HashMap<>();
map.put(User.Columns.EMAIL,user.get_userName());
map.put(User.Columns.PASSWORD, user.get_password());
map.put(User.BaseColumns.CREATED_DATE, Calendar.getInstance().getTime());
Document doc = (Document) DBHelper.CreateOne(map);
}
首先,实现是否正确我想要做的只是创建一个新用户并在创建后返回Document对象。
错误抛出的原因是什么?
任何想法?
答案 0 :(得分:0)
这是关于类似问题的Question。它可能是有用的
总之,回答:
引用:“这是等待和通知的意思:”
private Queue<Product> q = ...;
private Object lock = new Object();
void produceSomething(...) {
Product p = reallyProduceSomething();
synchronized(lock) {
q.add(p);
lock.notify();
}
}
void consumeSomething(...) {
Product p = null;
synchronized(lock) {
while (q.peek() == null) {
lock.wait();
}
p = q.remove();
}
reallyConsume(p);
}