av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

瀏覽:3日期:2022-08-17 16:41:47
零、插播2020CSDN博客之星投票新聞

近日(1月11日-1月24日),2020CSDN博客之星評選正在火熱進(jìn)行中,作為碼齡1年的小白有幸入選Top 200,首先很感謝CSDN官方把我選上,本來以為只是來湊熱鬧,看大佬們PK 。

綜合過去9天大佬們戰(zhàn)況,前10名大佬基本坐得很穩(wěn),后期出現(xiàn)黑馬發(fā)力,勢不可擋,都在沖刺Top 20,有了微妙的變化,不得不令人佩服點贊!真正的實力可以看出,文章數(shù)量不重要,更重要的是質(zhì)量!一切用數(shù)據(jù)說話,如圖:

截至 2021-01-20 11:50:02

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

看了大佬的驚人數(shù)據(jù),與我差距甚大,不禁感慨,接下來看看我自己!

首先,很感謝每一位幫忙投票的粉絲和兄弟姐妹們,感謝您的關(guān)注和支持,經(jīng)過大家上一周的共同努力,我已進(jìn)入2020博客之星投票排行榜Top 100。

投票還有一周時間,進(jìn)入更激烈更有懸念的階段,希望讀者們下來一周能投出您手中寶貴的票權(quán),讓我更進(jìn)一步!

投票地址:https://bss.csdn.net/m/topic/blog_star2020/detail?username=charzous

或者掃碼投票:

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

重點:每一個投票都會被記錄,投了之后找Charzous幫忙也容易了(瘋狂暗示投票拉票)!

比如,幫忙下載資源,或者博客一鍵三連,再次對每位幫我投票的粉絲表示感謝! 😊新的一年,讓我們一起變得更強(qiáng)!

即日起到24號,每天都可以投票哦,票數(shù)越多,貢獻(xiàn)排行榜就越靠前,我就記住你的名字啦!

24號是否能和大佬們在頂峰相見,就靠大家了哈!

一、承上啟下

前一篇學(xué)習(xí)了Java并發(fā)程序設(shè)計原理之后,為了對這個部分有了更深層的理解,并運用于實際場景中,所以我找了比較實際的案例進(jìn)行實踐——文件搜索,簡單來說,這也是電腦文件系統(tǒng)中的一個常見功能,用戶可以通過用戶名搜索文件系統(tǒng)中符合條件的文件。

文件搜索的程序需要用到Java并發(fā)API中的Thread類和Runnable接口,其中一些重要的內(nèi)容先簡單了解一下。

二、Java中的多線程

線程類Thread,有兩種方式創(chuàng)建執(zhí)行線程。

1、擴(kuò)展Thread類并重載run()方法

Thread類包含了豐富的方法,在實現(xiàn)線程時候必須重載run方法,擴(kuò)展Thread類和調(diào)用start方法創(chuàng)建新的線程。其他常用方法:

getId():獲取Thread對象的標(biāo)識符,線程整個生命周期中唯一不變的一個正整數(shù)。getName()/setName():String類型,獲取或設(shè)置Thread對象名。getPriority()/setPriority():獲取或設(shè)置線程的優(yōu)先級。值范圍:Thread.MIN_PRIORITY~Thread.MAX_PRIORITY(1~10),創(chuàng)建時默認(rèn)Thread.NORM_PRIORITY(5)。getState():線程對象的狀態(tài)。包括:NEW(新創(chuàng)建)、RUNNABLE(運行中)、BLOCKED(等待鎖定)、WAITING(等待)、TIME_WAITING(有時間限制等待)、THREAD(完成)。線程在一段時間中只能處于一種狀態(tài),而且是在JVM中的狀態(tài),不能映射到操作系統(tǒng)的線程狀態(tài)。interrupt():請求結(jié)束執(zhí)行Thread對象。interrupted():檢查中斷狀態(tài),清除中斷標(biāo)志的值。isInterrupted():檢查中斷狀態(tài),不清除中斷標(biāo)志的值。sleep():線程執(zhí)行睡眠時間,單位毫秒。join():暫停調(diào)用線程的執(zhí)行,直到調(diào)用該方法的線程執(zhí)行結(jié)束為止。currentThread():靜態(tài)方法,返回實際執(zhí)行當(dāng)前任務(wù)的Thread對象。2、實現(xiàn)Runnable接口

可以通過線程來執(zhí)行Runnable對象,更靈活更改并發(fā)程序,還可以通過不同線程使用同一個Runnable對象。

相對來說,使用Runnable接口創(chuàng)建線程的方法更加推薦,它只定義了run方法,是每個線程的主方法。當(dāng)執(zhí)行start方法啟動新線程時,就會調(diào)用run方法。

三、串行文件搜索

這里分為兩種版本,串行(單線程)和并發(fā)(多線程),后續(xù)可以進(jìn)行比較。

1、創(chuàng)建公共類Result保存搜索結(jié)果

/** * Result.java * @author Charzous * @date 2021/1/20 11:00 * */ package SearchFiles; public class Result { boolean found; String path; public void setFound(boolean found){ this.found=found; } public boolean isFound(){ return this.found; } public void setPath(String path){ this.path=path; } public String getPath(){ return this.path; }}2、查找算法

算法思路簡單,通過初始路徑,獲取文件和目錄內(nèi)容,并與目標(biāo)文件名進(jìn)行比較,相同則記錄Result,算法完成;不同則遞歸遍歷文件,直到算法完成。

/** * * SerialSearch.java * @author Charzous * @date 2021/1/20 11:15 * */ package SearchFiles; import java.io.File; public class SerialFileSearch { public static void searchFiles(File file,String fileName,Result result){ File[] contents; contents=file.listFiles(); if ((contents==null)||(contents.length==0)) return; for (File content:contents){ if (content.isDirectory()) searchFiles(content,fileName,result); else{ if (content.getName().equals(fileName)){ result.setPath(content.getAbsolutePath()); result.setFound(true); System.out.println('Serial Search Path: '+result.getPath()); return; } } if (result.isFound()) return; } } public static void main(String[] args) { Result result=new Result(); File file=new File('D:'); long startTime=System.currentTimeMillis(); String fileName='maskOrder.txt'; SerialFileSearch.searchFiles(file,fileName,result); if (!result.isFound()) System.out.println('未找到該文件:'+fileName); else System.out.println('找到該文件:'+fileName+'!'); System.out.println('查詢時間:'+(System.currentTimeMillis()-startTime)+'ms'); }}四、并行文件搜索(多線程)1、創(chuàng)建ParallelGroupFileTask類

它實現(xiàn)所有用于查找文件的線程,實現(xiàn)Runnable接口,重載run方法,其中包括了處理目錄的processDirectory方法,處理文件的processFile方法。

/** * ParallelGroupFileTask.java * @author Charzous * @date 2021/1/20 11:31 * */package SearchFiles; import java.io.File;import java.util.concurrent.ConcurrentLinkedQueue; class ParallelGroupFileTask implements Runnable { private final String fileName; private final ConcurrentLinkedQueue<File> directories; private final Result parallelResult; private boolean found; public ParallelGroupFileTask(String fileName, ConcurrentLinkedQueue<File> directories, Result parallelResult) { this.fileName = fileName; this.directories = directories; this.parallelResult = parallelResult; } @Override public void run() { while (directories.size() > 0) { File file = directories.poll(); try { processDirectory(file,fileName,parallelResult);//遞歸 if (found) { System.out.println(Thread.currentThread().getName() + ' has found the file'); System.out.println('parallel search:Path :' + parallelResult.getPath()); return; } } catch (Exception e) { System.out.println(Thread.currentThread().getName() + ' hae been interrupted'); } } } public void processDirectory(File file, String fileName, Result parallelResult) throws InterruptedException { File[] contents; contents = file.listFiles(); if ((contents == null) || (contents.length == 0)) return; for (File content : contents) { if (content.isDirectory()) { processDirectory(content, fileName, parallelResult); if (Thread.currentThread().isInterrupted()) throw new InterruptedException(); if (found) return; } else { processFile(content, fileName, parallelResult);//遞歸 if (Thread.currentThread().isInterrupted()) throw new InterruptedException(); if (found) return; } } } public void processFile(File content, String fileName, Result parallelResult) { if (content.getName().equals(fileName)) { parallelResult.setPath(content.getAbsolutePath()); this.found = true; } } public boolean getFound() { return found; } }2、多線程算法

創(chuàng)建ParallelGroupFileSearch類,其中包括了存放基本路徑的線程安全的列表ConcurrentLinkedQueue,然后創(chuàng)建新線程,數(shù)量有JVM中可用的線程數(shù)量,通過Runtime的availableProcessors方法獲得。

其中,若某個線程找到目標(biāo)文件,會使用interrupt方法取消其他線程的執(zhí)行。具體實現(xiàn)代碼如下:

/** * ParallelGroupFileSearch.java * @author Charzous * @date 2021/1/20 11:40 * */package SearchFiles; import java.io.File;import java.util.concurrent.ConcurrentLinkedQueue; public class ParallelGroupFileSearch { public static void searchFiles(File file, String fileName, Result parallelResult) { ConcurrentLinkedQueue<File> directories = new ConcurrentLinkedQueue<>(); File[] contents = file.listFiles(); for (File content : contents) { if (content.isDirectory()) directories.add(content); } int numThreads = Runtime.getRuntime().availableProcessors(); Thread[] threads = new Thread[numThreads]; ParallelGroupFileTask[] tasks = new ParallelGroupFileTask[numThreads]; for (int i = 0; i < numThreads; i++) { tasks[i] = new ParallelGroupFileTask(fileName, directories, parallelResult); threads[i] = new Thread(tasks[i]); threads[i].start(); } boolean finish = false; int numFinished = 0; while (!finish) { numFinished = 0; for (int i = 0; i < threads.length; i++) { if (threads[i].getState() == Thread.State.TERMINATED) { numFinished++; if (tasks[i].getFound()) finish = true; } } if (numFinished == threads.length) finish = true; } if (numFinished != threads.length) { for (Thread thread : threads) thread.interrupt(); } } public static void main(String[] args) { Result result=new Result(); File file=new File('D:'); String fileName='maskOrder.txt'; long startTime=System.currentTimeMillis(); ParallelGroupFileSearch.searchFiles(file,fileName,result); System.out.println('查詢時間:'+(System.currentTimeMillis()-startTime)+'ms'); } }五、結(jié)果1、串行(單線程)

串行版本多次測試結(jié)果用時在1900ms左右!

10次測試數(shù)據(jù):

查詢時間:1978ms 2036 1860 1926 1861 2100 1889 2030 1905 1990

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

2、并發(fā)(多線程)

并發(fā)版本多線程測試用時在1400ms左右!

10次測試數(shù)據(jù):

查詢時間:1441ms 1368 1258 1546 1444 1430 1490 1432 1338 1435

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索功能

從簡單的測試結(jié)果可以看出,并發(fā)搜索的算法速度提升明顯。

這一篇通過實際的案例進(jìn)行實踐——文件搜索,簡單來說,這也是電腦文件系統(tǒng)中的一個常見功能,用戶可以通過用戶名搜索文件系統(tǒng)中符合條件的文件。Runnable接口和Thread類的基本使用也有了更深的認(rèn)識。在文件搜索這個案例中,學(xué)習(xí)了Java并發(fā)原理的實際應(yīng)用,首先設(shè)計一種串行的版本,然后再實現(xiàn)并發(fā)的版本,這也是一個改進(jìn)的過程。

到此這篇關(guān)于Java并發(fā)(Runnable+Thread)實現(xiàn)硬盤文件搜索的文章就介紹到這了,更多相關(guān)Java并發(fā)硬盤文件搜索內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 久久免费资源 | 久久久婷| 91视频免费在观看 | 亚洲精品乱码久久久久久按摩 | 羞羞视频在线观免费观看 | 一区二区三区在线电影 | 精品国产乱码久久久久久蜜柚 | 精精国产xxxx视频在线播放 | 中国大陆高清aⅴ毛片 | 国产精久久久久久久妇剪断 | 99久久成人 | 一区二区三区在线 | 久久久一区二区 | 欧美无乱码久久久免费午夜一区 | 免费艹逼视频 | 精品视频网 | 亚洲精品二三区 | 韩日一区二区三区 | 西西裸体做爰视频 | 国产亚洲精品久久久久久豆腐 | 中文字幕欧美一区 | 国产视频第一页 | 香蕉超碰 | 日本成年免费网站 | 欧美久久国产精品 | 国产精品亚洲成在人线 | 日韩成人在线视频 | 九九精品久久久 | 国产久视频 | 91观看| 亚洲国产成人精品久久 | 自拍偷拍亚洲欧美 | 国产精品欧美一区二区 | 精品欧美乱码久久久久久 | 玖玖视频| 99reav| 国产精品视频在线观看 | 国产精品不卡 | 成人av电影免费在线观看 | 中文字幕亚洲欧美日韩在线不卡 | 一区二区三区四区在线 |