我正在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);
}
首先,实现对我要执行的操作是否正确,只是创建一个新用户并在创建后返回文档对象。
为什么会引发错误?
有任何想法吗?
Here's问题有关类似问题。可能有用
总而言之:
Quote:“这里是等待和通知的原意:”
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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句