我是靠谱客的博主 明理豌豆,这篇文章主要介绍Java实验(八)· 多线程 · 四川师范大学《JAVA》实验报告 八,现在分享给大家,希望可以做个参考。

写这个实验报告八原因主要是前面几个实验报告网上都有答案,唯独实验八的多线程我没有找到,感觉蛮坑爹的。

【任务一】:编写程序比较各种排序算法的效率。

要求:

  1. 生成若干个随机数,并改变随机数的数量。
  2. 给出3种以上排序的方法,并写出它们的算法,每个线程类中实现一种排序方法。
  3. 利用线程技术计算不同排序算法对不同数量的随机数排序的运行时间。
    提示:为了公平,同一种算法需要运行多次,取运行时间的平均值。
Sort.class (包为Main01)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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("打开文件错误"); } } } }

最后

以上就是明理豌豆最近收集整理的关于Java实验(八)· 多线程 · 四川师范大学《JAVA》实验报告 八的全部内容,更多相关Java实验(八)·内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部