2017-11-19 2 views
0

CreateOneをクラスから呼び出すと、CreateOneメソッドはTaskの実行後にObjectを返す必要があります。だから私はタスクを呼び出した後、以下の行を追加しました。非アクティビティのAndroidアプリで待機する前にオブジェクトがスレッドによってロックされていない

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 Objectを返すだけです。

なぜエラーがスローされますか?

すべてのアイデア?

答えて

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); 
} 
+0

おかげしかし、この例では、見て、両方の方法は、内部にあります同じクラスとロックオブジェクトが使用されていますが、私の場合は両方とも異なるクラスのものです。 –

関連する問題