DIV CSS 佈局教程網

用 Java 實作多執行緒環形緩衝,讓網路播放不再斷斷續續
編輯:關於JavaScript     

我平常喜歡在網路上聽音樂,有些連結的下載速度慢得令人心浮氣躁。直覺做法是透過 HttpURLConnection 開連線,用 InputStream 取資料流,再包一層 BufferedInputStream。實測之後發現沒用——只要網路速度不夠,緩衝區設多大都一樣,播放還是斷斷續續,根本達不到真正的緩衝效果。

問題出在「單執行緒」:同一條線上要下載、同時又要供讀取,兩件事互相搶時間。於是我改寫成多執行緒的緩衝播放。

設計思路

核心是一個環形緩衝區。把 buf[] 切成 16 塊,每塊 32KB;下載執行緒負責往緩衝區寫,一次寫一塊;讀取執行緒(BuffRandAcceURL)每次讀任意小於 32KB 的長度。

同步規則可以濃縮成三句話:寫與寫之間互斥,需等待空閒塊;多個寫執行緒併發填滿 buf[];讀與寫併發使用 buf[],互不阻塞。

我不贊成無上限的多執行緒下載,所以設計了自動收斂機制:當下載速度已經足夠,就讓部分下載執行緒自行退出,直到只剩一條。當然,一旦引入多執行緒,死鎖與 HttpURLConnection 的超時阻塞就變成必須正面處理的難題,程式碼複雜度也跟著上升。

一、HttpReader:以 HTTP 協定從指定 URL 讀資料

它封裝了連線建立、逾時設定,以及重點——以 Range 標頭做定位(seek)。當請求的位置與目前位置相同且連線仍活著時,直接沿用既有連線,不重開。

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;
    }

    /** 讀滿 len 個位元組,不足則持續補足 */
    public int getData(byte[] b, int off, int len) throws IOException {
        int r, rema = len;
        while (rema > 0) {
            if ((r = in_stream.read(b, off, rema)) == -1) {
                return -1;
            }
            rema -= r;
            off += r;
            cur_pos += r;
        }
        return len;
    }

    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:讀寫之間的通訊介面

把「寫執行緒需要問緩衝管理器的事」抽象成一個介面,讓 Writer 不必知道緩衝區的實作細節。

package instream;

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

三、Writer:下載執行緒

每個 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 都實作這個介面,前者讀遠端,後者讀本機磁碟。

package instream;

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

五、BuffRandAcceURL:核心的緩衝管理器

它同時實作 IRandomAccess 與 IWriterCallBack,負責建立下載執行緒、從 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;          // 32KB
    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;
    // 同時作為讀寫同步鎖:buf.wait() / buf.notify()
    private static byte[] buf;
    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);

        // 以非同步方式解析 ID3
        new TagThread(url);

        try {
            String s = URLDecoder.decode(sURL, "GBK");
            System.out.println("start>> " + s.substring(s.lastIndexOf("/") + 1));
            s = null;
        } catch (Exception e) {
            System.out.println("start>> " + sURL);
        }

        // 建立「寫」執行緒
        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();

        // 跳過 ID3 v2 標籤
        MP3Tag mP3Tag = new MP3Tag();
        int v2_size = mP3Tag.checkID3V2(buf, 0);
        if (v2_size > 0) {
            frame_bytes -= v2_size;
            fill_bytes -= v2_size;
            file_pointer = v2_size;
            read_pos = v2_size;
            read_pos &= BUF_LENGTH_MASK;

            int units = v2_size >> UNIT_LENGTH_BITS;
            for (int i = 0; i < units; i++) {
                buf_bytes[i] = 0;
                this.notifyWriter();
            }
            buf_bytes[units] -= v2_size;
            this.notifyWriter();
        }
        mP3Tag = null;
    }

    /** 等待緩衝區填到足夠水位 */
    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;
            System.out.printf("\r[緩衝%1$6.2f%%] ", (float) fill_bytes / cache_size * 100);
            synchronized (buf) {
                buf.wait();
            }
        }
        System.out.printf("\r");
    }

    /** 確認目前塊與下一塊加起來是否夠讀 len 個位元組 */
    private int try_reading(int i, int len) throws Exception {
        int n = (i == UNIT_COUNT - 1) ? 0 : (i + 1);
        int r = (buf_bytes[i] == 0) ? 0 : (buf_bytes[i] + buf_bytes[n]);
        while (r < len) {
            if (writer_count == 0 || isalive == false) return r;
            try_cache();
            r = (buf_bytes[i] == 0) ? 0 : (buf_bytes[i] + buf_bytes[n]);
        }
        return len;
    }

    /** 各「寫」執行緒互斥地等待空閒塊 */
    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;

        // 1. 等到至少有 1 個位元組可讀
        while (buf_bytes[i] < 1) {
            try_cache();
            if (writer_count == 0) return -1;
        }
        if (isalive == false) return -1;

        // 2. 讀取
        iret = buf[read_pos] & 0xff;
        fill_bytes--;
        file_pointer++;
        read_pos++;
        read_pos &= BUF_LENGTH_MASK;

        if (--buf_bytes[i] == 0) notifyWriter();

        // 3. 通知
        return iret;
    }

    public int read(byte b[]) throws Exception {
        return read(b, 0, b.length);
    }

    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;

        // 1. 等到有足夠內容可讀
        if (try_reading(i, len) < len || isalive == false) return -1;

        // 2. 讀取(注意環形緩衝可能跨尾端)
        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;
    }

    /** 從 src_off 處複製資料,但不移動檔案「指標」 */
    public int dump(int src_off, byte b[], int dst_off, int len) throws Exception {
        int rpos = read_pos + src_off;
        if (try_reading(rpos >> UNIT_LENGTH_BITS, len) < len || isalive == false)
            return -1;

        int tail_len = BUF_LENGTH - rpos;
        if (tail_len < len) {
            System.arraycopy(buf, rpos, b, dst_off, tail_len);
            System.arraycopy(buf, 0, b, dst_off + tail_len, len - tail_len);
        } else {
            System.arraycopy(buf, rpos, b, dst_off, len);
        }
        return len;   // 不發送通知
    }

    public long length()         { return file_length; }
    public long getFilePointer() { return file_pointer; }
    public void close()          { }
    public void seek(long pos) throws Exception { }
}

幾個關鍵設計說明

為什麼用 32KB 對齊。所有位移運算都用位元 shifting 完成(index << 15 換算位元組位置、pos >> 15 換算塊索引、pos & MASK 做環形繞回),比除法與取模運算快,程式碼也更短。這是選 2 的整數次方作為單位大小的真正原因。

用 buf 本身當鎖。沒有另外宣告 lock 物件,直接把 byte[] 當成同步守門員。所有 wait/notify 都針對它,減少了物件數量,也讓「誰在等什麼」這件事更容易追。

為什麼讀取要同時看兩塊。讀取長度可以任意指定,因此允許一次讀取橫跨兩塊緩衝區。try_reading 把「目前塊剩餘 + 下一塊」加總來判斷是否夠,read 中再用 tail_len 決定是否要走兩段 arraycopy。

執行緒自動收斂的條件。tryWriting 中的 await_count 是關鍵:等待次數多、且排名在後的執行緒會自行退出,讓總執行緒數隨網路狀況收斂。這樣在網速好時不會浪費連線,網速差時又能保持足夠的平行度。

實測結果與提醒

經過一段不短的實際使用,這個寫法確實達到了目標。與其他播放器相比,它能較為流暢穩定的下載並播放,很少出現卡頓。

有幾點值得注意。HttpReader 中的 content_length 是 static,代表同一個 JVM 內只能服務單一檔案;若要同時播多首曲子,必須把它改成每個 HttpReader 各自持有的成員變數。另外,多執行緒協調本質上複雜,這份程式碼的重試與逾時處理仍偏保守,正式使用前建議補上完整的單元測試,特別是「伺服器中途斷線」與「緩衝區耗盡」這兩個情境。

以上實作仍有不足之處,歡迎指教。

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