欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談一下Java多線程斷點(diǎn)復(fù)制

 更新時(shí)間:2023年04月11日 10:12:20   作者:CrazyDragon_King  
這篇文章主要介紹了淺談一下Java多線程斷點(diǎn)復(fù)制,當(dāng)程序執(zhí)行中斷時(shí)(出現(xiàn)錯(cuò)誤、斷電關(guān)機(jī)),仍可以從上次復(fù)制過程中重新開始(不必從頭開始復(fù)制),需要的朋友可以參考下

上次寫了一個(gè)利用 RandomAccessFile 和 多線程實(shí)現(xiàn)的多線程復(fù)制,但是沒有增加斷點(diǎn)復(fù)制的功能。這里的斷點(diǎn)復(fù)制是指:當(dāng)程序執(zhí)行中斷時(shí)(出現(xiàn)錯(cuò)誤、斷電關(guān)機(jī)),仍可以從上次復(fù)制過程中重新開始(不必從頭開始復(fù)制)。 多線程復(fù)制博客

細(xì)節(jié)介紹

我這里是使用一個(gè)Timer類(java.util.Timer)來實(shí)現(xiàn)斷點(diǎn)功能的,就是使用這個(gè)類,每隔一段時(shí)間進(jìn)行一次記錄,記錄的內(nèi)容是每個(gè)線程復(fù)制的進(jìn)度。

Timer 類的介紹:

A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. 線程在后臺(tái)線程中調(diào)度任務(wù)以供將來執(zhí)行的工具。任務(wù)可以安排為一次性執(zhí)行,也可以安排為定期重復(fù)執(zhí)行。

根據(jù) API 中的介紹可以看出,這個(gè) Timer 類可以只執(zhí)行一次任務(wù),也可以周期性地執(zhí)行任務(wù)。(注意這個(gè)類是 java.util.Timer 類,不是 javax 包下面的類。)

這個(gè)類的有很多和時(shí)間相關(guān)的方法,這里就不介紹了,感興趣的可以去了解,這里只介紹我們需要使用的一個(gè)方法。

public void schedule(TimerTask task, long delay, long period)

Schedules the specified task for repeated fixed-delay execution beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period. 為指定的任務(wù)安排在指定延遲之后開始的重復(fù)固定延遲執(zhí)行。隨后的執(zhí)行發(fā)生在按規(guī)定時(shí)間間隔的大致間隔。

使用這個(gè)方法,按照一個(gè)固定的時(shí)間間隔記錄各個(gè)線程的復(fù)制進(jìn)度信息即可。

代碼部分

定時(shí)任務(wù)類

package dragon.local;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RecordTask extends TimerTask {
	public static final String filename = "breakPointRecord.txt";
	private Timer timer;
	private List<FileCopyThread> copyThreads;
	private String outputPath;
	
	public RecordTask(Timer timer, List<FileCopyThread> copyThreads, String outputPath) {
		this.timer = timer;
		this.copyThreads = copyThreads;
		this.outputPath = outputPath;
	}
	
	@Override
	public void run() {
		try {
			this.breakPointRecord();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void breakPointRecord() throws FileNotFoundException, IOException {
		int aliveThreadNum = 0;  //存活線程數(shù)目
		//不使用追加方式,這里只需要最新的記錄即可。
		File recordFile = new File(outputPath, filename);
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(recordFile))){
			//每次記錄一個(gè)線程的下載位置,但是取出來又需要進(jìn)行轉(zhuǎn)換,太麻煩了。
			//我們直接使用序列化來進(jìn)行操作,哈哈!
			long[] curlen = new long[4];
			int index = 0;
			for (FileCopyThread copyThread : copyThreads) {
				if (copyThread.isAlive()) {
					aliveThreadNum++;
				}
				curlen[index++] = copyThread.getCurlen();
				System.out.println(index+" curlen: "+copyThread.getCurlen());
			}
			//創(chuàng)建 Record 對(duì)象,并序列化。
			oos.writeObject(new Record(curlen));
		}
		//當(dāng)所有的線程都死亡時(shí),關(guān)閉計(jì)時(shí)器,刪除記錄文件。(所有線程死亡的話,就是文件已經(jīng)復(fù)制完成了?。?
		if (aliveThreadNum == 0) {
			timer.cancel();
			recordFile.delete();
		}
		System.out.println("線程數(shù)量: "+aliveThreadNum);
	}
}

說明:

if (aliveThreadNum == 0) {
	timer.cancel();
	recordFile.delete();
}

如果線程都已經(jīng)結(jié)束了,就表示程序已經(jīng)正常執(zhí)行結(jié)束了。這個(gè)時(shí)候就刪除記錄文件。這里這個(gè)記錄文件是一個(gè)標(biāo)志(flag),如果存在記錄文件就表示程序沒有正常結(jié)束,再次啟動(dòng)時(shí),會(huì)進(jìn)行斷點(diǎn)復(fù)制。

注意:這里沒有考慮復(fù)制過程中的 IO 異常,如果線程拋出 IO 異常,那么線程的狀態(tài)也是結(jié)束了。但是考慮,本地文件復(fù)制出現(xiàn) IO 異常的情況還是比較少的,就沒有考慮,如果是網(wǎng)絡(luò)下載的話,這個(gè)程序的功能可能就需要進(jìn)行改進(jìn)了。

記錄信息類

每次需要依次寫入各個(gè)線程的信息,但是讀取出來還需要進(jìn)行轉(zhuǎn)換,還是感覺過于麻煩了,這里直接利用Java的序列化機(jī)制了。 有時(shí)候,直接操作對(duì)象是很方便的。 注意: 數(shù)組的下標(biāo)表示的就是每個(gè)線程的位置。

package dragon.local;

import java.io.Serializable;

public class Record implements Serializable{
	/**
	 * 序列化 id
	 */
	private static final long serialVersionUID = 1L;
	private long[] curlen;
	
	public Record(long[] curlen) {
		this.curlen = curlen;
	} 
	
	public long[] getCurlen() {
		return this.curlen;
	}
}

復(fù)制線程類

package dragon.local;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileCopyThread extends Thread {
	private int index;
	private long position;
	private long size;
	private File targetFile;
	private File outputFile;
	private long curlen;      //當(dāng)前下載的長度
	
	public FileCopyThread(int index, long position, long size, File targetFile, File outputFile) {
		this.index = index;
		this.position = position;
		this.size = size;
		this.targetFile = targetFile;
		this.outputFile = outputFile;
		this.curlen = 0L;
	}
	
	@Override
	public void run() {
		try (
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(targetFile));
			RandomAccessFile raf = new RandomAccessFile(outputFile, "rw")){
			bis.skip(position);  //跳過不需要讀取的字節(jié)數(shù),注意只能先后跳
			raf.seek(position);  //跳到需要寫入的位置,沒有這句話,會(huì)出錯(cuò),但是很難改。
			int hasRead = 0;
			byte[] b = new byte[1024];
			/**
			 * 注意,每個(gè)線程只是讀取一部分?jǐn)?shù)據(jù),不能只以 -1 作為循環(huán)結(jié)束的條件
			 * 循環(huán)退出條件應(yīng)該是兩個(gè),即寫入的字節(jié)數(shù)大于需要讀取的字節(jié)數(shù) 或者 文件讀取結(jié)束(最后一個(gè)線程讀取到文件末尾)
			 */
			while(curlen < size && (hasRead = bis.read(b)) != -1) {
				raf.write(b, 0, hasRead);
				curlen += (long)hasRead;
				//強(qiáng)制停止程序。
//				if (curlen > 17_000_000) {
//					System.exit(0);
//				}
			}

			System.out.println(index+" "+position+" "+curlen+" "+size);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public long getCurlen() {   //獲取當(dāng)前的進(jìn)度,用于記錄,以便必要時(shí)恢復(fù)讀取進(jìn)度。
		return position+this.curlen;
	}
}

這段代碼是為了測(cè)試斷點(diǎn)復(fù)制的。如果你想要進(jìn)行測(cè)試,可以將 if 判斷中的條件按照你要復(fù)制的文件大小進(jìn)行相應(yīng)的調(diào)整。如果要進(jìn)行測(cè)試,可以先將這段代碼的注釋取消再執(zhí)行程序(然后程序退出,這時(shí)候文件沒有復(fù)制完成。),然后再將這段代碼注釋再次執(zhí)行程序,文件將會(huì)復(fù)制成功。

				//強(qiáng)制停止程序。
//				if (curlen > 17_000_000) {
//					System.exit(0);
//				}

復(fù)制工具類

package dragon.local;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;



/**
 * 設(shè)計(jì)思路:
 * 獲取目標(biāo)文件的大小,然后設(shè)置復(fù)制文件的大小(這樣做是有好處的),
 * 然后使用將文件分為 n 分,使用 n 個(gè)線程同時(shí)進(jìn)行復(fù)制(這里我將 n 取為 4)。
 * 
 * 進(jìn)一步拓展:
 * 加強(qiáng)為斷點(diǎn)復(fù)制功能,即程序中斷以后,
 * 仍然可以繼續(xù)從上次位置恢復(fù)復(fù)制,減少不必要的重復(fù)開銷
 * */

public class FileCopyUtil {
	//設(shè)置一個(gè)常量,復(fù)制線程的數(shù)量
	private static final int THREAD_NUM = 4;
	
	private FileCopyUtil() {}
	
	/**
	 * @param targetPath 目標(biāo)文件的路徑
	 * @param outputPath 復(fù)制輸出文件的路徑
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 * */
	public static void transferFile(String targetPath, String outputPath) throws IOException, ClassNotFoundException {
		File targetFile = new File(targetPath);
		File outputFilePath = new File(outputPath);
		if (!targetFile.exists() || targetFile.isDirectory()) {   //目標(biāo)文件不存在,或者是一個(gè)文件夾,則拋出異常
			throw new FileNotFoundException("目標(biāo)文件不存在:"+targetPath);
		}
		if (!outputFilePath.exists()) {     //如果輸出文件夾不存在,將會(huì)嘗試創(chuàng)建,創(chuàng)建失敗,則拋出異常。
			if(!outputFilePath.mkdir()) {
				throw new FileNotFoundException("無法創(chuàng)建輸出文件:"+outputPath);
			}
		}
		
		long len = targetFile.length();
		
		File outputFile = new File(outputFilePath, "copy"+targetFile.getName());
		createOutputFile(outputFile, len);    //創(chuàng)建輸出文件,設(shè)置好大小。
		
		//創(chuàng)建計(jì)時(shí)器 Timer 對(duì)象
		Timer timer = new Timer();
	
		long[] position = new long[4];
		//每一個(gè)線程需要復(fù)制文件的起點(diǎn)
		long size = len / FileCopyUtil.THREAD_NUM + 1;     //保存復(fù)制線程的集合
		List<FileCopyThread> copyThreads = new ArrayList<>();
		Record record = getRecord(outputPath);
		
		for (int i = 0; i < FileCopyUtil.THREAD_NUM; i++) {
			//如果已經(jīng)有了 記錄文件,就從使用記錄數(shù)據(jù),否則就是新的下載。
			position[i] = record == null ? i*size : record.getCurlen()[i];
			FileCopyThread copyThread = new FileCopyThread(i, position[i], size, targetFile, outputFile);
			copyThread.start();     //啟動(dòng)復(fù)制線程
			copyThreads.add(copyThread);   //將復(fù)制線程添加到集合中。
		}
	
		timer.schedule(new RecordTask(timer, copyThreads, outputPath), 0L, 100L);  //立即啟動(dòng)計(jì)時(shí)器,每隔10秒記錄一次位置。
		System.out.println("開始了!");
	}
	
	//創(chuàng)建輸出文件,設(shè)置好大小。
	private static void createOutputFile(File file, long length) throws IOException {
		try (   
			RandomAccessFile raf = new RandomAccessFile(file, "rw")){
			raf.setLength(length);
		}
	}
	
	//獲取以及下載的位置
	private static Record getRecord(String outputPath) throws FileNotFoundException, IOException, ClassNotFoundException {
		File recordFile = new File(outputPath, RecordTask.filename);
		if (recordFile.exists()) {
			try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(recordFile))){
				return (Record) ois.readObject();
			}
		}
		return null;
	}
}

說明: 根據(jù)復(fù)制的目錄中,是否存在記錄文件來判斷是否啟動(dòng)斷點(diǎn)復(fù)制。

private static Record getRecord(String outputPath) throws FileNotFoundException, IOException, ClassNotFoundException {
		File recordFile = new File(outputPath, RecordTask.filename);
		if (recordFile.exists()) {
			try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(recordFile))){
				return (Record) ois.readObject();
			}
		}
		return null;
	}

啟動(dòng)斷點(diǎn)復(fù)制原來其實(shí)很簡單,就是和復(fù)制一樣,只不過起始復(fù)制位置變成了記錄的位置了。

//如果已經(jīng)有了 記錄文件,就從使用記錄數(shù)據(jù),否則就是新的下載。
position[i] = record == null ? i*size : record.getCurlen()[i];

總結(jié)

采用定時(shí)記錄的方法,感覺也是很不錯(cuò)的,但是似乎又一個(gè)問題,當(dāng)程序正在記錄序列化信息的時(shí)候,如果出現(xiàn)了錯(cuò)誤(導(dǎo)致序列化信息沒有寫入完整),當(dāng)反序列化讀取的時(shí)候,會(huì)拋出 EOFException 。不過這種情況很少發(fā)生,但是似乎在強(qiáng)制關(guān)閉tomcat的過程中,可能會(huì)出現(xiàn)這個(gè)問題。(Tomcat的序列化信息很多,IO 時(shí)間較長,但是我這里記錄的信息很少的,就只是一個(gè) Java 對(duì)象而已。)

如果出現(xiàn)了這個(gè)異常,解決辦法就是刪除記錄文件,但是因?yàn)檫@個(gè)錯(cuò)誤就無法使用斷點(diǎn)復(fù)制的功能了。

關(guān)于多線程下載的那部分我沒有寫,我自己想了好久,沒有想出來很好的方法(我對(duì)于線程不是很了解),我參考了網(wǎng)上的幾個(gè)實(shí)現(xiàn)(都是將每個(gè)線程的記錄寫入一個(gè)單獨(dú)文件中,但是感覺這樣不是很好,我是想寫入一個(gè)文件中,但是這樣又很麻煩。)。我想寫一個(gè)自己的方法,但是沒有想出來,就暫時(shí)放棄了。

到此這篇關(guān)于淺談一下Java多線程斷點(diǎn)復(fù)制的文章就介紹到這了,更多相關(guān)Java多線程斷點(diǎn)復(fù)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 實(shí)現(xiàn)最小二叉樹堆排序的實(shí)例

    java 實(shí)現(xiàn)最小二叉樹堆排序的實(shí)例

    這篇文章主要介紹了java 實(shí)現(xiàn)最小二叉樹堆排序的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 詳解Spring中的@Scope注解

    詳解Spring中的@Scope注解

    這篇文章主要介紹了詳解Spring中的@Scope注解,@Scope注解是Spring IOC容器中的一個(gè)作用域,在Spring IOC容器中,他用來配置Bean實(shí)例的作用域?qū)ο?需要的朋友可以參考下
    2023-07-07
  • Java中的MapStruct用法詳解

    Java中的MapStruct用法詳解

    這篇文章主要介紹了Java中的MapStruct用法詳解,MapStuct的使用非常簡單,把對(duì)應(yīng)的jar包引入即可,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Java中notify是順序喚醒還是隨機(jī)喚醒的

    Java中notify是順序喚醒還是隨機(jī)喚醒的

    這篇文章主要介紹了Java中notify是順序喚醒還是隨機(jī)喚醒的,有很多人會(huì)認(rèn)為?notify?是隨機(jī)喚醒的,但它真的是隨機(jī)喚醒的嗎?帶著疑問一起進(jìn)入文章了解具體的內(nèi)容吧
    2022-05-05
  • MyBatis中多對(duì)多關(guān)系的映射和查詢

    MyBatis中多對(duì)多關(guān)系的映射和查詢

    本文主要介紹了MyBatis中多對(duì)多關(guān)系的映射和查詢的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • Java自然排序Comparable使用方法解析

    Java自然排序Comparable使用方法解析

    這篇文章主要介紹了Java自然排序Comparable使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Python如何使用@property @x.setter及@x.deleter

    Python如何使用@property @x.setter及@x.deleter

    這篇文章主要介紹了Python如何使用@property @x.setter及@x.deleter,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring Cloud中關(guān)于Feign的常見問題總結(jié)

    Spring Cloud中關(guān)于Feign的常見問題總結(jié)

    這篇文章主要給大家介紹了Spring Cloud中關(guān)于Feign的常見問題,文中通過示例代碼介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • Spark調(diào)度架構(gòu)原理詳解

    Spark調(diào)度架構(gòu)原理詳解

    這篇文章主要介紹了Spark 調(diào)度架構(gòu)原理詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • java 啟動(dòng)exe程序,傳遞參數(shù)和獲取參數(shù)操作

    java 啟動(dòng)exe程序,傳遞參數(shù)和獲取參數(shù)操作

    這篇文章主要介紹了java 啟動(dòng)exe程序,傳遞參數(shù)和獲取參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評(píng)論