Java實驗(八)· 多線程 · 四川師範大學《JAVA》實驗報告 八

寫這個實驗報告八原因主要是前面幾個實驗報告網上都有答案,唯獨實驗八的多線程我沒有找到,感覺蠻坑爹的。

【任務一】:編寫程序比較各種排序算法的效率。

要求:

  1. 生成若干個隨機數,並改變隨機數的數量。
  2. 給出3種以上排序的方法,並寫出它們的算法,每個線程類中實現一種排序方法。
  3. 利用線程技術計算不同排序算法對不同數量的隨機數排序的運行時間。
    提示:爲了公平,同一種算法需要運行多次,取運行時間的平均值。
Sort.class (包爲Main01)
package Main01;

public class Sort implements Runnable {
	private int i;
	private int []a;
	int x;
	public Sort(int i,int []array) {
		this.i = i;
		this.a = array;
	}
	public void run() {
		switch (this.i) {
			case 1 :{
				shellSort();
				break;
			}
			case 2:{
				MaoPaoSort();
				break;
			}
			case 3:{
				Choice();
				break;
			}
		}
	}
	public void shellSort() {
		double t1 = System.currentTimeMillis();
		int i=0,gap=0,k=0,j=0,t=0;
		for(gap=a.length/2;gap>0;gap=gap/2){           //設置增量,每次都是gap/2;
			for(k=gap;k<a.length;k++){
				t=a[k];                  //將要插入的數放入t中     
				j=k-gap;
				while(j>=0&&t<a[j]){     //t若比前面位置上的數字小,前面數就要後移
					a[j+gap]=a[j];   //後移
					j=j-gap;         //找到前面一個位置上的數,反正這步我推了好久
				} 
		    a[j+gap]=t;                          //t插入這個位置
		    }
	    }
		 System.out.println("希爾排序完成,結果:"+"耗時:"+(System.currentTimeMillis()-t1));
//		for(int x:a) {
//			System.out.print(x+" ");
//		}
//		 System.out.println("耗時:"+(System.currentTimeMillis()-t1));
	} 
	public void MaoPaoSort() {
		int n = a.length;
		double t1 = System.currentTimeMillis();
		int i,j,temp;
		 for (i=0; i<n-1; ++i)  //比較n-1輪
		    {
		        for (j=0; j<n-1-i; ++j)  //每輪比較n-1-i次,
		        {
		            if (a[j] < a[j+1])
		            {
		                temp = a[j];
		                a[j] = a[j+1];
		                a[j+1] = temp;
		            }
		        }
		    }
		 System.out.println("冒泡排序完成,結果:"+"耗時:"+(System.currentTimeMillis()-t1));
//		 for(int x:a) {
//				System.out.print(x+" ");
//			}
		 System.out.println();
	}
	public void Choice() {
		double t1 = System.currentTimeMillis();
		int n = a.length;
		int i,j,temp;
		for(i=0;i<n-1;i++)
	    {
	        for(j=i+1;j<n;j++)
	        {
	            if(a[i]>a[j])
	            {
	                temp=a[i];
	                a[i]=a[j];
	                a[j]=temp;
	            }
	        }
	    }
		System.out.println("選擇排序完成,結果:"+"耗時:"+(System.currentTimeMillis()-t1));
//		 for(int x:a) {
//				System.out.print(x+" ");
//			}
		 
//		 System.out.println();
	}
}

main.class
package Main01;

import java.util.Random;

public class main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random r =new Random();
		int i,x;
		int []array=new int [100000];
		for(i=0;i<1000;i++) {
			x = r.nextInt();
			array[i]=x;
		}
		Thread t1 = new Thread(new Sort(1, array),"1");
		Thread t2 = new Thread(new Sort(2, array),"2");
		Thread t3 = new Thread(new Sort(3, array),"3");
		t1.start();
		t2.start();
		t3.start();
	}
	
}

【任務二】:用程序模擬一個銀行扣款程序。

要求:
假設銀行會將用戶賬戶上的錢全部轉入理財賬戶。但是前提是用戶賬戶上有500元以上的金額。

  1. 寫一個賬戶類,其初始金額爲0。
     每次往賬戶類存入金額,則賬戶類需要在文件log.txt文件中寫入一行。例如存入金額爲100,則在文件中寫入一行“+100”(不包含引號)。
     每次從賬戶中扣款,則賬戶類需要在文件log.txt文件中寫入一行。例如扣款金額爲1000,則在文件中寫入一行”-1000”(不包含引號)。
  2. 寫一個存款線程,該線程啓動後睡眠一個隨機時間(100ms1000ms之間)。線程每次醒後,判斷如果賬戶上金額小於500,則往賬戶上存入隨機的金額(10200之間),隨後再次進入睡眠;如果賬戶上金額大於或者500,則通知扣款程序扣款。
  3. 寫一個扣款線程,該線程啓動後判斷如果用戶賬戶上有500元以上金額,則扣款,每次扣款只扣500的整數倍。扣款完成後(剩餘金額小於500),程序進入等待狀態,等待被存款線程喚醒。成功扣款額達到100000後,線程退出。
    寫一個測試程序來使用兩個線程和線程之間的同步。
    提示:
  4. 扣款線程退出後,存款線程也要退出。
  5. 文件換行要求使用Java標準換行符。
main.class
package Main02;

public class main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Account a = new Account();
		Thread t1 = new DebitMoney(  a  );
		Thread t2 = new SaveMoney(  a  );
		t1.start();
		t2.start();                
	}

}
DebitMoney.class
package Main02;

public class DebitMoney extends Thread {
	Account a = null;
	public DebitMoney( Account a ) {
		this.a= a;
	}
	public void run() {
		while(a.sum < 10000) {
			a.debit();
		}
	}
	
}

SaveMoney.class
package Main02;

import java.util.Random;

public class SaveMoney extends Thread {
	Account a =null;
	public SaveMoney(Account a) {
		this.a = a ;
	}
	@Override

	public void run() {
		Random r = new Random();
		int x  ;
		while(a.sum < 10000) {
			a.save();
		}
	}

}

Account.class
package Main02;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

class Account{
//	boolean less500 = true;
	int money = 0,sum=0;
	FileWriter fw ;
	
	public Account() {
		
	}
	
	public synchronized void debit() {
		if(this.money < 500) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//寫入 -record
		int x,n,temp,record;
		temp = this.money;
		this.money%=500;
		record = temp - this.money;
		this.sum+=record;
		try {
			fw= new FileWriter("log.txt",true);
			fw.write("-"+record+"\n");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public synchronized void save() {
		if(this.money>=500) {
			notify();
		}else {
			Random r =new Random();
			int x = (r.nextInt(20)+1) * 10;//0-19 -> 1 20
			this.money +=x;
			try {
				fw= new FileWriter("log.txt",true);
				fw.write("+"+x+"\n");
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("打開文件錯誤");
			}
			
		}
	}
	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章