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("打开文件错误");
			}
			
		}
	}
	
}

发布了8 篇原创文章 · 获赞 15 · 访问量 6765

猜你喜欢

转载自blog.csdn.net/wang__Ray/article/details/104883050