简体   繁体   English

Android String if语句

[英]Android String if-statement

I have a if-statement in the start of my app 我的应用程式开头有if陈述

    if (ready.equals("yes")){
...
}

and later on my code I have 然后在我的代码上

ready="yes";

but the if statement is never called, why? 但是如果从不调用if语句,为什么呢? The ready="yes"; ready =“ yes”; is called from a background thread, is that why? 是从后台线程调用的,为什么?

    public void DownloadFromUrl(final String fileName) {  //this is the downloader method
            new Thread(new Runnable() {
                public void run() {
        try {

            URL url = new URL("https://xxxxxxx");
            File file = new File(PATH + fileName);

            long startTime = System.currentTimeMillis();
            Log.d("ImageManager", "download begining");
            Log.d("ImageManager", "download url:" + url);
            Log.d("ImageManager", "downloaded file name:" + fileName);
                    /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

                    /*
                     * Define InputStreams to read from the URLConnection.
                     */
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

                    /*
                     * Read bytes to the Buffer until there is nothing more to read(-1).
                     */
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

                    /* Convert the Bytes read to a String. */
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            Log.e("Ready or not", ready);
            ready="yes";
            Log.d("ImageManager", "download ready in"
                    + ((System.currentTimeMillis() - startTime) / 1000)
                    + " sec");
            Log.e("Ready or not", ready);



        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }

    }
    }).start();

Correct me if I'm wrong. 如果我错了纠正我。 If you're saying that your code looks like this: 如果您说的代码如下所示:

new Thread(new Runnable() { public void run()
                            {
                                // thread code
                                if (ready.equals("yes")) {
                                    // handler code
                                }
                                // more thread code
                            }).start();

// later on...
ready = "yes";

And you're asking why ready = "yes" doesn't execute before if (ready.equals("yes")) , then that's because multiple threads aren't guaranteed to execute in a certain order . 并且您要问为什么ready = "yes"不能在if (ready.equals("yes"))之前执行,那是因为不能保证多个线程按特定顺序执行 If you want to wait until ready.equals("yes") before you execute the if statement, then you have to use the Object.wait() and Object.notifyAll() methods: 如果要等到ready.equals("yes")执行if语句之前,则必须使用Object.wait()Object.notifyAll()方法:

// this is a field
private Object waitOnThis = new Object();

new Thread(new Runnable() { public void run()
                            {
                                // thread code
                                waitOnThis.wait(); // blocks until notify / notifyAll is called on waitOnThis
                                // by this point ready.equals("yes")
                                if (ready.equals("yes")) {
                                    // handler code
                                }
                                // more thread code
                            }).start();

// later on...
ready = "yes";
waitOnThis.notifyAll(); // unblocks threads waiting on waitOnThis

Good luck! 祝好运!

EDIT: Be sure to wrap each of the code fragments above in a synchronized (waitOnThis) block, or else you'll get an IllegalMonitorStateException . 编辑:确保将每个以上的代码片段包装在一个synchronized (waitOnThis)块中,否则您将得到IllegalMonitorStateException

Application 's OnCreate will only be called once at the beginning when the app is created. ApplicationOnCreate只会在开始时被调用一次。

Consider using callback if you want to notify that the download has finished. 如果您想通知下载已完成,请考虑使用回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM