Android實現(xiàn)多進(jìn)程并發(fā)控制的兩種方案
一、問題背景
當(dāng)一個App中存在多個進(jìn)程時例如存在 主進(jìn)程,輔進(jìn)程兩個進(jìn)程,兩個進(jìn)程都會去向A文件中寫入數(shù)據(jù)。但是我們業(yè)務(wù)中希望每次僅允許有一個進(jìn)程向A文件寫入內(nèi)容。即當(dāng)主進(jìn)程寫入時,輔進(jìn)程要等待主進(jìn)程寫完之后才可以寫入,防止出現(xiàn)并發(fā)修改導(dǎo)致數(shù)據(jù)異常的問題。
在實際的場景上,例如在我們的項目中未使用MMKV之前,KV存儲是自行實現(xiàn)的多進(jìn)程并發(fā)的SP。
二、實現(xiàn)方案
1、方案1:僅一個進(jìn)程負(fù)責(zé)寫
將所有的寫入操作調(diào)整到同一個進(jìn)程中,這樣就相當(dāng)于規(guī)避了多進(jìn)程并發(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");
// 文件鎖,確保單進(jìn)程寫入
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來控制多進(jìn)程并發(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)聽某個端口進(jìn)來的請求。
FileChannel
FileChannel 是 Java NIO (New I/O) 中的一個類,用于對文件進(jìn)行高效的讀寫操作。它提供了比傳統(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ū)域的鎖,用于控制多個進(jìn)程或線程對同一文件的并發(fā)訪問。
鎖的類型
- 共享鎖 (Shared Lock) :多個進(jìn)程可同時持有,用于讀操作
- 排他鎖 (Exclusive Lock) :一次只能由一個進(jìn)程持有,用于寫操作
通過文件鎖的方式控制多進(jìn)程并發(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é)
以上簡單介紹了一下兩種控制多進(jìn)程并發(fā)的方案。
其中使用ContentProvider或者Service的方式將所有的操作控制在同一個進(jìn)程中的方案邏輯清晰,但是代碼量比較多。尤其是使用Service的方式,雖然上面我們每個給出示例代碼,但是可以想象沒新增一個進(jìn)程都需要寫相關(guān)的代碼,寫起來就比較啰嗦了。
而ContentProvicer的方式,系統(tǒng)中也有很多相關(guān)的實現(xiàn)方案,例如更新媒體文件,更新聯(lián)系人數(shù)據(jù)等。
使用文件鎖的方式對于僅熟悉Android不熟悉Java的同學(xué)不容易想到,所以本篇也同時簡單介紹了一下FileChannel以及FileLock。
到此這篇關(guān)于Android實現(xiàn)多進(jìn)程并發(fā)控制的兩種方案的文章就介紹到這了,更多相關(guān)Android多進(jìn)程并發(fā)控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Viewpager實現(xiàn)輪播廣告圖
這篇文章主要為大家詳細(xì)介紹了Android Viewpager實現(xiàn)輪播廣告圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
android開發(fā)之蜂鳴提示音和震動提示的實現(xiàn)原理與參考代碼
蜂鳴提示音和震動提示此功能在手機(jī)使用中很實用,最近在讀zxing項目,學(xué)到了不少東西;我們一起來看看他是怎么做的,感興趣的朋友可以了解下哦2013-01-01
Android實現(xiàn)文字動態(tài)高亮讀取進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)文字動態(tài)高亮讀取進(jìn)度效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android中執(zhí)行java命令的方法及java代碼執(zhí)行并解析shell命令
這篇文章給大家介紹Android中執(zhí)行java命令的方法及java代碼執(zhí)行并解析shell命令,需要的朋友一起學(xué)習(xí)2015-11-11
Android webview與js的數(shù)據(jù)交互
有了WebView這個組件,Android應(yīng)用開發(fā)技術(shù)也就轉(zhuǎn)嫁到html與java數(shù)據(jù)交互上來。說白了就是js與WebView的數(shù)據(jù)交互,這就是本文所要討論的2017-04-04

