簡體   English   中英

Android String if語句

[英]Android String if-statement

我的應用程式開頭有if陳述

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

然后在我的代碼上

ready="yes";

但是如果從不調用if語句,為什么呢? ready =“ yes”; 是從后台線程調用的,為什么?

    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();

如果我錯了糾正我。 如果您說的代碼如下所示:

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

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

並且您要問為什么ready = "yes"不能在if (ready.equals("yes"))之前執行,那是因為不能保證多個線程按特定順序執行 如果要等到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

祝好運!

編輯:確保將每個以上的代碼片段包裝在一個synchronized (waitOnThis)塊中,否則您將得到IllegalMonitorStateException

ApplicationOnCreate只會在開始時被調用一次。

如果您想通知下載已完成,請考慮使用回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM