Java的等待()及通知()与Android的等待()及通知() [英] Java wait() & notify() vs Android wait() & notify()

查看:458
本文介绍了Java的等待()及通知()与Android的等待()及通知()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经想出了如何使用wait()和notify()的Java应用程序来从网上的一些数据,我只好到code迁移到我的Andr​​oid应用程序。因为它原来的code,将在的Java应用程序已经工作永远不会有我的Andr​​oid应用程序内工作,即使企图使多线程(与Runnable接口,然后AsyncTask的)。这个问题似乎是,Android应用程序将在的Object.wait()的调用之后挂起,永远不会再继续下去。

Having figured out how to use the wait() and notify() in Java application to fetch some data from the Internet, I had to migrate that code into my Android application. As it turns out the code that would've worked in Java app would never had worked within my Android app even with attempts to make it multi-threaded (with Runnable and then ASyncTask). The problem seems that the Android app will hang after a call on Object.wait() and will never continue further.

以下是Java和放大器的code; Android的类:

The following is the code of the Java & Android classes:

Java的

    import java.util.Map;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class Getter {
    private String username = "jdk17";
    private String userFullname;
    private Object userObj = new Object();

    public static void main(String[] args) {

        System.out.println("Main");
        String text;
        Getter main = new Getter();
        text = main.getString();
        System.out.println("Main - Text = " + text);
    }

    public String getString() {

        Firebase firebaseRef = new Firebase(
                "https://demoandroid.firebaseio.com/user/username/" + username);

        firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onCancelled(FirebaseError arg0) {
            }

            @Override
            public void onDataChange(DataSnapshot snap) {

                System.out.println("***********onDataChange()***********");
                Object obj = snap.getValue();
                userFullname = (String) ((Map) obj).get("fullname");
                System.out.println("********* The text = " + userFullname);
                synchronized (userObj) {
                    userObj.notify();
                }
            }

        });

        try {
            synchronized (userObj) {
                System.out.println("Calling wait()");
                userObj.wait();
            }
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        System.out.println("getString() returning text = " + userFullname);
        return userFullname;

    }
}

Android版

Android

package com.example.paabooking;

import java.util.Map;

import android.util.Log;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;


public class FirebaseHelper {
    private final String TAG = "FirebaseHelper";
    private String username = "jdk17";
    private String userFullname;
    private Object userObj = new Object();

    public FirebaseHelper() {}

    public String getString()  {
            Log.d(TAG, "getString()");

        Firebase firebaseRef = new Firebase("https://demoandroid.firebaseio.com/user/username/" + username);

        firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onCancelled(FirebaseError arg0) {Log.d(TAG, "cancelled.");}

            @Override
            public void onDataChange(DataSnapshot snap) {

                Log.d(TAG, "***********onDataChange()***********");
                // TODO Auto-generated method stub
                Object obj = snap.getValue();
                userFullname = (String)((Map)obj).get("fullname");
                Log.d(TAG, "********* The text = " + userFullname);
                synchronized(userObj) {
                    userObj.notify();
                }   
            }

        });

        try {
            synchronized (userObj) {
                Log.d(TAG, "Calling wait()");
                userObj.wait();
            }
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }


        Log.d(TAG,"getString() returning text = " + userFullname);
        return userFullname;

    }
}

控制台打印输出的Java

Console printout:Java

Main
Calling wait()
***********onDataChange()***********
********* The text = Tom Barry
getString() returning text = Tom Barry
Main - Text = Tom Barry

控制台打印输出的Andr​​oid

Console printout: Android

getString()
Calling wait()

Java的火力地堡图书馆 - https://www.firebase.com/docs/java- quickstart.html

推荐答案

我不认为这是由于等待之间的差异(真实或假设)/通知的Java和Android。

I don't think that this is due to any differences (real or hypothetical) between wait/notify in Java and Android.

我认为差异是由从火力地堡页本帖链接到您解释道:

I think that the difference is explained by this quote from the Firebase page you linked to:

默认情况下,在Android上,所有的回调都在主线程上执行。在其他JVM的目标,回调是在一个新的,独立的线程执行。您可以通过提供自己的事件目标到默认配置此行为配置库所使用。

在Android的情况下,你的主线程似乎被实例化对象火力地堡,加入侦听器,然后调用等待()。但的wait()阻塞主线程...所以,当然主线程是不是在一个位置接受将其唤醒回调。因此,一切都冻结。

In the Android case, your main thread appears to be instantiating the Firebase object, adding the listener, and then calling wait(). But wait() is blocking the main thread ... so, of course the main thread is not in a position to accept the callback that would wake it up. Hence, everything freezes.

报价的第二个句子似乎表明解决问题的方式。

The 2nd sentence of the quote seems to suggest the way to solve the problem.

我从来没有遇到火力地堡前,更不用说试图使用它。这只是基于我的文档阅读肤浅和你的code。

这篇关于Java的等待()及通知()与Android的等待()及通知()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆