亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)多進程并發(fā)控制的兩種方案

 更新時間:2025年04月22日 08:30:41   作者:半山居士  
當一個App中存在多個進程時例如存在?主進程,輔進程兩個進程,兩個進程都會去向A文件中寫入數(shù)據(jù),但是我們業(yè)務(wù)中希望每次僅允許有一個進程向A文件寫入內(nèi)容,所以本文給大家介紹了Android實現(xiàn)多進程并發(fā)控制的兩種方案,需要的朋友可以參考下

一、問題背景

當一個App中存在多個進程時例如存在 主進程,輔進程兩個進程,兩個進程都會去向A文件中寫入數(shù)據(jù)。但是我們業(yè)務(wù)中希望每次僅允許有一個進程向A文件寫入內(nèi)容。即當主進程寫入時,輔進程要等待主進程寫完之后才可以寫入,防止出現(xiàn)并發(fā)修改導致數(shù)據(jù)異常的問題。

在實際的場景上,例如在我們的項目中未使用MMKV之前,KV存儲是自行實現(xiàn)的多進程并發(fā)的SP。

二、實現(xiàn)方案

1、方案1:僅一個進程負責寫

將所有的寫入操作調(diào)整到同一個進程中,這樣就相當于規(guī)避了多進程并發(fā)問題。

我們可以通過提供一個ContantProvider或者Service來是實現(xiàn)這個功能。

以下是使用ContentProvider的方式:

FileProvider

public class FileProvider extends ContentProvider {
    private static final String AUTHORITY = "com.example.fileprovider";
    private static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/file");

    // 文件鎖,確保單進程寫入
    private static final Object fileLock = new Object();

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, 
        String selection, String[] selectionArgs, String sortOrder) {
        return null; // 不提供查詢功能
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null; // 不提供插入功能
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0; // 不提供刪除功能
    }

    @Override
    public int update(Uri uri, ContentValues values, 
        String selection, String[] selectionArgs) {
        return 0; // 不提供更新功能
    }

    // 自定義方法:寫入文件
    @Override
    public Bundle call(String method, String arg, Bundle extras) {
        if ("writeToFile".equals(method)) {
            String content = extras.getString("content");
            synchronized (fileLock) { 
                writeToFile(content);
            }
            Bundle result = new Bundle();
            result.putBoolean("success", true);
            return result;
        }
        return super.call(method, arg, extras);
    }

    // 實際寫入文件的邏輯
    private void writeToFile(String content) {
        File file = new File(getContext().getFilesDir(), "A.txt");
        try (FileOutputStream fos = new FileOutputStream(file, true)) {
            fos.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注冊

<provider
    android:name=".FileProvider"
    android:authorities="com.example.fileprovider"
    android:exported="true" />

寫入邏輯

    private void writeToFileViaProvider(String content) {
        Uri uri = Uri.parse("content://com.example.fileprovider/file");
        ContentResolver resolver = getContentResolver();
        
        Bundle extras = new Bundle();
        extras.putString("content", content);
        
        try {
            Bundle result = resolver.call(uri, "writeToFile", null, extras);
            if (result != null && result.getBoolean("success")) {
                Log.d("FileProvider", "Write successful");
            }
        } catch (Exception e) {
            Log.e("FileProvider", "Failed to write file", e);
        }
    }

使用Service + Binder的方式,代碼比較簡單,這里就不寫了。

2、方案2:通過文件鎖的方式

文件鎖主要是利用FileChannel、FileLock來控制多進程并發(fā)。

關(guān)于 Channel

Channel 經(jīng)常翻譯為通道,類似 IO 中的流,用于讀取和寫入。不用像BIO那樣,讀數(shù)據(jù)和寫數(shù)據(jù)需要不同的數(shù)據(jù)通道。

public interface Channel extends Closeable {

    /**
     * Tells whether or not this channel is open.
     *
     * @return <tt>true</tt> if, and only if, this channel is open
     */
    public boolean isOpen();

    /**
     * Closes this channel.
     */
    public void close() throws IOException;

}

我們常用的Channel有:

  • FileChannel:文件通道,用于文件的讀和寫。
  • DatagramChannel:用于UDP連接的接收和發(fā)送。
  • SocketChannel:把它理解為TCP連接通道,簡單理解就是TCP客戶端。
  • ServerSocketChannel:TCP對應(yīng)的服務(wù)端,用于監(jiān)聽某個端口進來的請求。

FileChannel

FileChannel 是 Java NIO (New I/O) 中的一個類,用于對文件進行高效的讀寫操作。它提供了比傳統(tǒng) FileInputStream 和 FileOutputStream 更靈活和高效的文件操作方式。

所有的NIO操作始于通道,通道是數(shù)據(jù)來源或數(shù)據(jù)寫入的目的地。其與 Buffer 打交道,讀操作的時候?qū)?Channel 中的數(shù)據(jù)填充到 Buffer 中,而寫操作時將 Buffer 中的數(shù)據(jù)寫入到 Channel 中。

FileChannel的獲取方式:

通過FileInputStream/FileOutputStream

// 通過 FileInputStream/FileOutputStream (只讀或只寫)
FileInputStream fis = new FileInputStream("file.txt");
FileChannel readChannel = fis.getChannel();

FileOutputStream fos = new FileOutputStream("file.txt");
FileChannel writeChannel = fos.getChannel();

通過RandomAccessFile

// 通過 RandomAccessFile
RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
FileChannel channel = raf.getChannel();

通過FileChannel.open()

FileChannel channel = FileChannel.open(Paths.get("file.txt"), StandardOpenOption.READ);

在我們示例代碼中選擇了使用 FileOutputStream 來獲取FileChannel。

FileLock

FileLock 表示文件或文件區(qū)域的鎖,用于控制多個進程或線程對同一文件的并發(fā)訪問。

鎖的類型

  • 共享鎖 (Shared Lock) :多個進程可同時持有,用于讀操作
  • 排他鎖 (Exclusive Lock) :一次只能由一個進程持有,用于寫操作

通過文件鎖的方式控制多進程并發(fā)的 示例代碼:

public class FileWriter {

    private static final String FILE_PATH = "/path/to/your/file.txt";

    public void writeToFile(String content) {
        File file = new File(FILE_PATH);
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            FileChannel channel = fos.getChannel()) 
            // 獲取獨占鎖
            FileLock lock = channel.lock();
            try {
                // 寫入文件
                fos.write(content.getBytes());
            } finally {
                // 釋放鎖
                lock.release();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

三、總結(jié)

以上簡單介紹了一下兩種控制多進程并發(fā)的方案。

其中使用ContentProvider或者Service的方式將所有的操作控制在同一個進程中的方案邏輯清晰,但是代碼量比較多。尤其是使用Service的方式,雖然上面我們每個給出示例代碼,但是可以想象沒新增一個進程都需要寫相關(guān)的代碼,寫起來就比較啰嗦了。

而ContentProvicer的方式,系統(tǒng)中也有很多相關(guān)的實現(xiàn)方案,例如更新媒體文件,更新聯(lián)系人數(shù)據(jù)等。

使用文件鎖的方式對于僅熟悉Android不熟悉Java的同學不容易想到,所以本篇也同時簡單介紹了一下FileChannel以及FileLock。

到此這篇關(guān)于Android實現(xiàn)多進程并發(fā)控制的兩種方案的文章就介紹到這了,更多相關(guān)Android多進程并發(fā)控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論