实验五 多线程
一、实验目的
- 理解Java的多线程机制。
- 掌握线程的创建。
- 了解线程的生命周期。
- 了解线程的调度和优先级。
- 掌握如何改变线程的状态。
- 理解线程的同步机制。
- 掌握线程的互斥锁定。
- 掌握线程的同步运行。
二、实验内容和要求
题目:利用线程比较插入、冒泡排序算法的运行时间。
- 建立一个Person类,主要有name、age等私有成员变量,以age来对Person类的对象进行排序(升序)。该类实现Comparable接口,故必须实现方法public int compareTo(Object p)。
- 了解Comparable接口。
- 写两个类分别实现插入、冒泡排序,并获取其运行时间。
- 建立线程实现排序,注意一个时刻只让一个排序线程运行。
- 在main方法里启动两个线程分别实现插入和冒泡排序。
三、实验代码
(1) 建立一个Person类
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person(String s, int i) {
name = s;
age = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Person p) {
/*if(this.age > p.getAge()) {
return 1; //正整数是大于
} else if(this.age < p.getAge()) {
return -1; //负整数是小于
} else {
return 0; //0为等于
}*/
return this.age - ((Person)p).getAge();
}
public static void main(String args[]) {
}
}
(5) 建立一个Test类在main方法中写入如下代码
public class Test {
public static void main(String[] args) {
/* Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.setName("小明");
p1.setAge(15);
p2.setName("小红");
p2.setAge(12);
p3.setName("大红");
p3.setAge(13);
ArrayList<Person> list=new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);
System.out.println("排序前-------------------");
for (Person p : list) {
//System.out.println(000);
System.out.println(p.getAge());
}
Collections.sort(list);
System.out.println("排序后-------------------");
for (Person p : list) {
//System.out.println(000);
System.out.println(p.getAge());
}
System.out.println("年龄最大的为"+list.get(list.size()-1).getAge());*/
Person p[]=new Person[10000];
int i;
for(i=0;i<p.length;i++){
p[i]=new Person("平民"+i,(int)(120*Math.random()));
}
ThreadB t1=new ThreadB("冒泡排序",p);
ThreadB t2=new ThreadB("插入排序",p);
t1.start();
t2.start();
}
}
(2) 建立插入排序类,并获取运行时间
public class insertionSort {
long t1;
long t2;
public void sort(Person[] p) {
if (p.length == 0) {
throw new NullPointerException("内容为空,没有要排序的内容!");
}
t1 = System.currentTimeMillis();
int size = 1;
t1 = System.currentTimeMillis();
while (size < p.length) {
insertionSort(p, size++, p[size - 1]);
}
t2 = System.currentTimeMillis();
System.out.println("使用插入排序使用的时间是:" + (t2 - t1) + "毫秒");
System.out.println("排序结果为:");
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getName()+" "+p[i].getAge()+";");
}
}
private void insertionSort(Person[] p, int size, Person c) {
for (int i = 0; i < size; i++) {
if (c.compareTo(p[i]) < 0) {
System.out.println(p[i]);
for (int j = size; j > i; j--) {
p[j] = p[j - 1];
}
p[i] = c;
break;
}
}
}
}
(3) 建立冒泡排序类,并获取运行时间
public class bubbleSort {
long t1;
long t2;
public void sort(Person[] p) {
if (p == null) {
throw new NullPointerException("内容为空,没有要排序的内容!");
}
Person t;
t1 = System.currentTimeMillis();
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < (p.length - i - 1); j++) {
if (p[j].compareTo(p[j + 1]) > 0) {
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
}
}
}
t2 = System.currentTimeMillis();
System.out.println("使用冒泡排序所花的时间是:" + (t2 - t1) + "毫秒");
System.out.println("排序结果为:");
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getName()+" "+p[i].getAge()+";");
}
}
}
(4) 建立线程实现排序
class ThreadB extends Thread {
String s;
Object b;
Person[] c;
public ThreadB(String b, Person[] p) {
s = b;
c = p;
}
public void run() {
synchronized (c) {
if (s.equals("冒泡排序")) {
b = new bubbleSort();
((bubbleSort) b).sort(c);
} else {
b = new insertionSort();
((insertionSort) b).sort(c);
}
System.out.println();
System.out.println();
System.out.println("相隔一秒钟执行下一次的排序或者结束!");
System.out.println();
System.out.println();
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
}
四、运行结果