(多线程)利用线程比较插入、冒泡排序算法的运行时间。

实验五 多线程

一、实验目的

  1. 理解Java的多线程机制。
  2. 掌握线程的创建。
  3. 了解线程的生命周期。
  4. 了解线程的调度和优先级。
  5. 掌握如何改变线程的状态。
  6. 理解线程的同步机制。
  7. 掌握线程的互斥锁定。
  8. 掌握线程的同步运行。

二、实验内容和要求

题目:利用线程比较插入、冒泡排序算法的运行时间。

  1. 建立一个Person类,主要有name、age等私有成员变量,以age来对Person类的对象进行排序(升序)。该类实现Comparable接口,故必须实现方法public int compareTo(Object p)。
  2. 了解Comparable接口。
  3. 写两个类分别实现插入、冒泡排序,并获取其运行时间。
  4. 建立线程实现排序,注意一个时刻只让一个排序线程运行。
  5. 在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());
            }
        }
    }
}

四、运行结果

 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烟敛寒林o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值