DIV CSS 佈局教程網

多執行緒環形緩衝區的實作思路與程式碼解析
編輯:關於JavaScript     

我平時喜歡從網上聽歌,有些連結的下载速度實在太慢。若用 HttpURLConnection 打開連接,再以 InputStream 取得輸入流,接著用 BufferedInputStream 建構帶緩衝區的輸入流,一旦網速太慢,無論緩衝區設多大,播放起來都斷斷續續,達不到真正的緩衝效果。於是我嘗試自行編寫程式,用緩衝方式讀取遠端檔案。以下貼出的程式碼,是我自製 MP3 解碼器的一部分。

我並不特別贊同使用多執行緒下載,加上有些連結本身速度就很快,因此在頻寬足夠時,我會讓部分下載執行緒退出,最終只保留一個。當然,多執行緒中令人頭痛的死鎖問題,以及 HttpURLConnection 的超時阻塞問題,都會讓程式碼看起來異常複雜。

多執行緒環形緩衝的基本思路

將緩衝區 buf[] 劃分為 16 塊,每塊 32K。下載執行緒負責向緩衝區寫入資料,每次寫一塊;讀取執行緒(BuffRandAcceURL 類)則每次讀取小於 32K 的任意位元組數。同步策略可概括為:寫/寫互斥、等待空閒塊;寫/寫併發填寫 buf[];讀/寫併發使用 buf[]。

經過很長一段時間的實際使用,我認為相當滿意地達成了目標。與其他 MP3 播放器相比,這種方式能較為流暢、穩定地邊下載邊播放。以下把實現多執行緒下載緩衝的方法整理出來,若有不足之處,懇請指正。

一、HttpReader 類:透過 HTTP 協議從指定 URL 讀取資料

package instream;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public final class HttpReader {
    public static final int MAX_RETRY = 10;
    private static long content_length;
    private URL url;
    private HttpURLConnection httpConnection;
    private InputStream in_stream;
    private long cur_pos; // 用於決定 seek 中是否執行定位
    private int connect_timeout;
    private int read_timeout;

    public HttpReader(URL u) { this(u, 5000, 5000); }

    public HttpReader(URL u, int connect_timeout, int read_timeout) {
        this.connect_timeout = connect_timeout;
        this.read_timeout = read_timeout;
        url = u;
        if (content_length == 0) {
            int retry = 0;
            while (retry < HttpReader.MAX_RETRY) try {
                this.seek(0);
                content_length = httpConnection.getContentLength();
                break;
            } catch (Exception e) {
                retry++;
            }
        }
    }

    public static long getContentLength() { return content_length; }

    public int read(byte[] b, int off, int len) throws IOException {
        int r = in_stream.read(b, off, len);
        cur_pos += r;
        return r;
    }

    public void close() {
        if (httpConnection != null) { httpConnection.disconnect(); httpConnection = null; }
        if (in_stream != null) {
            try { in_stream.close(); } catch (IOException e) {}
            in_stream = null;
        }
        url = null;
    }

    /**
     * 拋出異常通知重試。
     * 響應碼 503 可能由暫時性原因引起,例如同一 IP 頻繁連接遭伺服器拒絕。
     */
    public void seek(long start_pos) throws IOException {
        if (start_pos == cur_pos && in_stream != null) return;
        if (httpConnection != null) { httpConnection.disconnect(); httpConnection = null; }
        if (in_stream != null) { in_stream.close(); in_stream = null; }
        httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.setConnectTimeout(connect_timeout);
        httpConnection.setReadTimeout(read_timeout);
        String sProperty = "bytes=" + start_pos + "-";
        httpConnection.setRequestProperty("Range", sProperty);
        int responseCode = httpConnection.getResponseCode();
        if (responseCode < 200 || responseCode >= 300) {
            try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }
            throw new IOException("HTTP responseCode=" + responseCode);
        }
        in_stream = httpConnection.getInputStream();
        cur_pos = start_pos;
    }
}

二、IWriterCallBack 接口:實現讀/寫通訊

package instream;

public interface IWriterCallBack {
    public boolean tryWriting(Writer w) throws InterruptedException;
    public void updateBuffer(int i, int len);
    public void updateWriterCount();
    public void terminateWriters();
}

三、Writer 類:下載執行緒,負責向 buf[] 寫入資料

package instream;

import java.io.IOException;
import java.net.URL;

public final class Writer implements Runnable {
    private static boolean isalive = true;
    private byte[] buf;
    private IWriterCallBack icb;
    protected int index;        // buf[] 內「塊」索引
    protected long start_pos;   // index 對應的檔案位置(相對檔首的偏移量)
    protected int await_count;  // 用於判斷:速度足夠就退出一個「寫」執行緒
    private HttpReader hr;

    public Writer(IWriterCallBack call_back, URL u, byte[] b, int i) {
        hr = new HttpReader(u);
        if (HttpReader.getContentLength() == 0) return; // 連線都失敗則返回
        icb = call_back;
        buf = b;
        Thread t = new Thread(this, "dt_" + i);
        t.setPriority(Thread.NORM_PRIORITY + 1);
        t.start();
    }

    public void run() {
        int write_bytes = 0, write_pos = 0, rema = 0, retry = 0;
        boolean cont = true;
        while (cont) {
            try {
                // 1. 等待空閒塊
                if (retry == 0) {
                    if (icb.tryWriting(this) == false) break;
                    write_bytes = 0;
                    rema = BuffRandAcceURL.UNIT_LENGTH;
                    write_pos = index << BuffRandAcceURL.UNIT_LENGTH_BITS;
                }
                // 2. 定位
                hr.seek(start_pos);
                // 3. 下載「一塊」
                int w;
                while (rema > 0 && isalive) {
                    w = (rema < 2048) ? rema : 2048;
                    if ((w = hr.read(buf, write_pos, w)) == -1) { cont = false; break; }
                    rema -= w;
                    write_pos += w;
                    start_pos += w;
                    write_bytes += w;
                }
                // 4. 通知「讀」執行緒
                retry = 0;
                icb.updateBuffer(index, write_bytes);
            } catch (InterruptedException e) {
                isalive = false; icb.terminateWriters(); break;
            } catch (IOException e) {
                if (++retry == HttpReader.MAX_RETRY) {
                    isalive = false; icb.terminateWriters(); break;
                }
            }
        }
        icb.updateWriterCount();
        try { hr.close(); } catch (Exception e) {}
        hr = null; buf = null; icb = null;
    }
}

四、IRandomAccess 接口

隨機讀取檔案的接口,由 BuffRandAcceURL 類與 BuffRandAcceFile 類實作。其中 BuffRandAcceFile 類負責讀取本機磁碟檔案,此處不展開其原始碼。

package instream;

public interface IRandomAccess {
    public int read() throws Exception;
    public int read(byte b[]) throws Exception;
    public int read(byte b[], int off, int len) throws Exception;
    public int dump(int src_off, byte b[], int dst_off, int len) throws Exception;
    public void seek(long pos) throws Exception;
    public long length();
    public long getFilePointer();
    public void close();
}

五、BuffRandAcceURL 類:建立下載執行緒,並從 buf[] 讀取資料

關鍵在於如何簡單有效地防止死鎖。以下只是一次嘗試,歡迎指正。

package instream;

import java.net.URL;
import java.net.URLDecoder;
import decode.Header;
import tag.MP3Tag;
import tag.TagThread;

public final class BuffRandAcceURL implements IRandomAccess, IWriterCallBack {
    public static final int UNIT_LENGTH_BITS = 15;      // 32K
    public static final int UNIT_LENGTH = 1 << UNIT_LENGTH_BITS;
    public static final int BUF_LENGTH = UNIT_LENGTH << 4; // 16 塊
    public static final int UNIT_COUNT = BUF_LENGTH >> UNIT_LENGTH_BITS;
    public static final int BUF_LENGTH_MASK = (BUF_LENGTH - 1);

    private static final int MAX_WRITER = 8;
    private static long file_pointer;
    private static int read_pos;
    private static int fill_bytes;
    private static byte[] buf;          // 同時作為讀寫同步鎖:buf.wait() / buf.notify()
    private static int[] buf_bytes;
    private static int buf_index;
    private static int alloc_pos;
    private static URL url = null;
    private static boolean isalive = true;
    private static int writer_count;
    private static int await_count;
    private long file_length;
    private long frame_bytes;

    public BuffRandAcceURL(String sURL) throws Exception { this(sURL, MAX_WRITER); }

    public BuffRandAcceURL(String sURL, int download_threads) throws Exception {
        buf = new byte[BUF_LENGTH];
        buf_bytes = new int[UNIT_COUNT];
        url = new URL(sURL);
        new TagThread(url); // 異步解析 ID3
        // 建立「寫」執行緒
        for (int i = 0; i < download_threads; i++) new Writer(this, url, buf, i + 1);
        frame_bytes = file_length = HttpReader.getContentLength();
        if (file_length == 0) {
            Header.strLastErr = "連接 URL 出錯,重試 " + HttpReader.MAX_RETRY + " 次後放棄。";
            throw new Exception("retry " + HttpReader.MAX_RETRY);
        }
        writer_count = download_threads;
        try_cache(); // 緩衝
    }

    private void try_cache() throws InterruptedException {
        int cache_size = BUF_LENGTH;
        if (cache_size > (int) file_length - alloc_pos) cache_size = (int) file_length - alloc_pos;
        cache_size -= UNIT_LENGTH;
        while (fill_bytes < cache_size) {
            if (writer_count == 0 || isalive == false) return;
            if (BUF_LENGTH > (int) file_length - alloc_pos)
                cache_size = (int) file_length - alloc_pos - UNIT_LENGTH;
            synchronized (buf) { buf.wait(); }
        }
    }

    // 各「寫」執行緒互斥等待空閒塊
    public synchronized boolean tryWriting(Writer w) throws InterruptedException {
        await_count++;
        while (buf_bytes[buf_index] != 0 && isalive) { this.wait(); }
        if (writer_count > 1 && w.await_count >= await_count && w.await_count >= writer_count) return false;
        if (alloc_pos >= file_length) return false;
        w.await_count = await_count;
        await_count--;
        w.start_pos = alloc_pos;
        w.index = buf_index;
        alloc_pos += UNIT_LENGTH;
        buf_index = (buf_index == UNIT_COUNT - 1) ? 0 : buf_index + 1;
        return isalive;
    }

    public void updateBuffer(int i, int len) {
        synchronized (buf) { buf_bytes[i] = len; fill_bytes += len; buf.notify(); }
    }

    public void updateWriterCount() {
        synchronized (buf) { writer_count--; buf.notify(); }
    }

    public synchronized void notifyWriter() { this.notifyAll(); }

    public void terminateWriters() {
        synchronized (buf) {
            if (isalive) {
                isalive = false;
                Header.strLastErr = "讀取檔案超時。重試 " + HttpReader.MAX_RETRY + " 次後放棄,請稍後再試。";
            }
            buf.notify();
        }
        notifyWriter();
    }

    public int read() throws Exception {
        int iret = -1;
        int i = read_pos >> UNIT_LENGTH_BITS;
        while (buf_bytes[i] < 1) { // 等待有 1 位元組可讀
            try_cache();
            if (writer_count == 0) return -1;
        }
        if (isalive == false) return -1;
        iret = buf[read_pos] & 0xff;
        fill_bytes--; file_pointer++; read_pos++;
        read_pos &= BUF_LENGTH_MASK;
        if (--buf_bytes[i] == 0) notifyWriter();
        return iret;
    }

    public int read(byte[] b, int off, int len) throws Exception {
        if (len > UNIT_LENGTH) len = UNIT_LENGTH;
        int i = read_pos >> UNIT_LENGTH_BITS;
        if (try_reading(i, len) < len || isalive == false) return -1;
        int tail_len = BUF_LENGTH - read_pos;
        if (tail_len < len) {
            System.arraycopy(buf, read_pos, b, off, tail_len);
            System.arraycopy(buf, 0, b, off + tail_len, len - tail_len);
        } else {
            System.arraycopy(buf, read_pos, b, off, len);
        }
        fill_bytes -= len; file_pointer += len; read_pos += len;
        read_pos &= BUF_LENGTH_MASK;
        buf_bytes[i] -= len;
        if (buf_bytes[i] < 0) {
            int ni = read_pos >> UNIT_LENGTH_BITS;
            buf_bytes[ni] += buf_bytes[i]; buf_bytes[i] = 0; notifyWriter();
        } else if (buf_bytes[i] == 0) {
            notifyWriter();
        }
        return len;
    }

    public long length() { return file_length; }
    public long getFilePointer() { return file_pointer; }
    public void close() {}
}

整體而言,這套機制透過「分塊緩衝 + 多執行緒寫入 + 單執行緒讀取」的方式,在頻寬充足時自動精簡下載執行緒,並以 wait/notify 兼顧同步與效能,最终实现邊下边播的流暢體驗。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved