实验六 线程与集合

原创(都是自己写):

1.用链表来存储内容,有一个Student类,属性有姓名,年龄,身高,姓名按照字典序排序,如果姓名相同再按年龄排序,如果年龄相同按身高排序。

2.要求3个线程:student1、student2、teacher,其中student1准备睡10分钟后再开始上课,student2准备睡一小时后再开始上课。Teacher在输出3句”上课”后,吵醒休眠的线程student1;student1被吵醒后,负责再吵醒休眠的线程studen2。

3.模拟售票处有一张5元钱,三个人排队买票,电影票五元一张,张某(排第一,拿20元一张的新人民币)、李某(排第二,拿10元一张的新人民币)和赵某(排第三,拿一张5元的人民币)按顺序买电影票。

4. 编写一个Java应用程序,在主线程中再创建3个线程:“运货司机”“装运工”和“仓库管理员”。要求线程“运货司机”占有CPU资源后立刻联合线程“装运工”,也就是让“运货司机”一直等到“装运工”完成工作才能开车,而“装运工”占有CPU资源后立刻联合线程“仓库管理员”,也就是让“装运工”一直等到“仓库管理员”打开仓库才能开始搬运货物。

  1. 源程序代码(2个类,Student.java,Demo.java(展示类)

Student.java:

public class Student  {

   public String name;//姓名  

   public int age;//年龄

   public int height;//身高

   Student(String name, int age,int height) {//构造函数,进行初始化

     this.name = name;

     this.age = age;

     this.height = height;

   }

   public String toString() {//转字符串方法

     return "姓名:"+this.name+",年龄:"+this.age+",身高:"+this.height;

   }

}

Demo.java:

import java.util.*;

public class Demo {

   public static void insert(List<Student> list,Student s1) {//按要求的排序进行插入

      ListIterator<Student> it = list.listIterator();

      while(it.hasNext()) {

          Student s2 = it.next();

          if(s1.name.compareTo(s2.name) == 0){//如果姓名一样,按身高排序

             if(s1.height == s2.height) {//如果身高一样,按年龄排序

                if(s1.age == s2.age) {//如果年龄也一样就直接插在他后面就好,没有差别,并跳出

                   it.add(s1);

                   break;

                }else if(s1.age < s2.age) {//如果是年龄小,就插入到他前面,并跳出

                   it.previous();

                    it.add(s1);

                   break;

                }

             }else if(s1.height < s2.height) {//如果身高小,就插入到他前面,并跳出

                it.previous();

                it.add(s1);

                break;

             }

          }else if(s1.name.compareTo(s2.name)<0) {//如果名字的字典序小,就插入到他前面,并跳出

             it.previous();

             it.add(s1);

             break;

          }

      }

      if(it.hasNext() == false) {//如果这时候没有元素说明还没插入,那就说明这个学生的按要求的排序里是最大的,插在最后面

          it.add(s1);

      }

   }

   public static void main(String[] args) {

      LinkedList<Student> list = new LinkedList<Student>();

      // 创建学生对象

      Student s1 = new Student("huassdfadasngxiangping",22,165);

      Student s2 = new Student("huafdgngxiangping",23,160);

      Student s3 = new Student("huangxiangping",23,155);

      Student s4 = new Student("chesdfsnsdfsdkaiwen",20,175);

      Student s5 = new Student("kebsdfsdingda",30,166);

      Student s6 = new Student("hejiahsdfao",33,188);

      Student s7 = new Student("tengfesdfsngda",29,199);

     

      // 添加元素对象

      list.add(s1);

      list.add(s2);

      list.add(s3);

      list.add(s4);

      list.add(s5);

      list.add(s6);

      list.add(s7);

      //重写sort的排序方式

      Collections.sort(list, new Comparator<Student>() {

          public int compare(Student s1, Student s2) {

               if(s1.name.compareTo(s2.name)==0){//如果姓名相同,按年龄排序

                  if(s1.age-s2.age==0) {//如果年龄相同,按身高排序

                     return s1.height-s2.height;

                  }

                  return s1.age-s2.age;

                }

               return s1.name.compareTo(s2.name);

          }

      });

      Student s8 = new Student("leichong", 27,170);

      insert(list,s8);//插入数据

      //遍历集合

      for(Student s : list) {

          System.out.println(s.toString());

      }

   }

}

 

  1. 源程序代码(2个类,ClassRoom.java,Demo.java(展示类)

ClassRoom.java:

public class ClassRoom implements Runnable{

   Thread student1,student2,teacher;

   ClassRoom(){//构造函数,建立三个线程

     student1 = new Thread(this);

     student2 = new Thread(this);

     teacher = new Thread(this);

   }

   public void run() {//重写run的方法

     String name = Thread.currentThread().getName();//当前线程的名字

     if(name.equals("student1")) {//student1的内容操作

        try {

           System.out.println("我是"+name+"在教室上课,想睡10分钟后再上课");

           Thread.sleep(1000*10);//停止10分钟

        }

        catch(InterruptedException ex){

           System.out.println(name+"teacher叫醒了");//如果中断就输出这个内容

        }

        student2.interrupt();//student2被叫醒

        System.out.println(name+"开始上课了");

     }else if(name.equals("student2")) {//student2的内容操作

        try {

           System.out.println("我是"+name+"在教室上课,想睡1小时后再上课");

           Thread.sleep(1000*60*60);//停止1小时

        }

        catch(InterruptedException ex){

           System.out.println(name+"student1叫醒了");//如果中断就输出这个内容

           student2.interrupt();//中断student2

        }

        System.out.println(name+"开始上课了");

     }else if(name.equals("teacher")) {//teacher的内容操作

        for(int i=1;i<=3;++i) {

           System.out.println(name+"喊:上课!");

           try {

             Thread.sleep(1000);

           }

           catch(InterruptedException ex){

            

           }

        }

        student1.interrupt();//老师叫醒学生1

     }

   }

}

Demo.java

public class Demo {

   public static void main(String[] args) {

     ClassRoom classroom = new ClassRoom();

     classroom.student1.setName("student1");//给各个线程进行命名

     classroom.student2.setName("student2");

     classroom.teacher.setName("teacher");

     classroom.student1.setPriority(10);//设置线程的优先级,首先肯定是student1student2先睡觉,也就是优先级比老师高

     classroom.student2.setPriority(9);

     classroom.teacher.setPriority(8);//学生睡了才有老师喊上课,这三个优先级就是先弄好最开始的线程的顺序,之后才能正确的执行接下来的内容

     classroom.student1.start();//线程开始

     classroom.student2.start();

     classroom.teacher.start();

   }

}

 

 

  1. 源程序代码(2个类,TicketCinema.java,Demo.java(展示类)

TicketCinema.java:

public class TicketCinema implements Runnable{

    Thread zhang,li,zhao;

    public int twentyNum=0;//20元的数量

    public int fiveNum=1;//5元的数量

    public int tenNum=0;//10元的数量

    TicketCinema(){//构造函数,建立三个线程

        zhang = new Thread(this);

        li = new Thread(this);

        zhao = new Thread(this);

    }

    public void run() {

        String name = Thread.currentThread().getName();

        if(name.equals("张某")) {

            buyTicket(20);//买票

        }else if(name.equals("李某")) {

            buyTicket(10);//买票

        }else if(name.equals("赵某")) {

            buyTicket(5);//买票

        }

    }

    public synchronized void buyTicket(int money) {//买票方法

        String name = Thread.currentThread().getName();

        if(money == 20) {

            while(!(tenNum>=1&&fiveNum>=1)) {//如果没有110快和一张5快以上那么他就得等待

                try {

                   System.out.println(name+"在一边等待买票......");

                   wait();

                }

                catch(InterruptedException ex) {

                  

                }

            }

            tenNum-=1;

            fiveNum-=1;

            twentyNum+=1;

            System.out.println(name+"20元买一张票,找回15");

        }else if(money == 10) {

            while(fiveNum<1) {//如果一张五块都没有那他就要等待

                try{

                   System.out.println(name+"在一边等待买票......");

                   wait();

                }

                catch(InterruptedException ex) {

                  

                }

            }

            fiveNum-=1;

            tenNum+=1;

            System.out.println(name+"10元买一张票,找回5");

        }else if(money == 5) {

            fiveNum+=1;

            System.out.println(name+"5元买一张票,找回0");

        }

        notifyAll();

    }

}

Demo.java:

public class Demo {

    public static void main(String[] args) {

        TicketCinema  ticket = new TicketCinema();

        ticket.zhang.setName("张某");

        ticket.li.setName("李某");

        ticket.zhao.setName("赵某");

        ticket.zhang.start();//线程开始

        ticket.li.start();

        ticket.zhao.start();

    }

}

运行结果:

 

  1. 源程序代码(2个类,WareHouse.java,Demo.java(展示类)

WareHouse.java:

public class WareHouse implements Runnable{

   Thread driver,remover,admin;

   WareHouse(){//构造函数,建立三个线程

     driver = new Thread(this);

     remover = new Thread(this);

     admin = new Thread(this);

   }

   public void run() {//重写run方法

     String name = Thread.currentThread().getName();//获取当前线程的名字

     if(name.equals("运货司机")) {//货运司机的操作

        System.out.println("我是"+name+",我在等装运工的工作完成了才能开车!");

        try {

           remover.start();//装运工线程开始,叫装运工了

           remover.join();//联合装运工线程。跟他是一条线了

        }

        catch(InterruptedException ex){

           System.out.println("过程出现问题!");

        }

        System.out.println("货运司机开始开车");

     }else if(name.equals("装运工")) {//装运工的操作

        System.out.println("我是"+name+",我在等仓库管理员的开门才能装运货物!");

        try {

           admin.start();//仓库管理员线程开始,叫仓库管理员了

           admin.join();//联合仓库管理员线程,跟他是一条线了

        }

        catch(InterruptedException ex){

           System.out.println("过程出现问题!");

        }

        System.out.println("装运工开始装运货物!");

     }else if(name.equals("仓库管理员")) {//仓库管理员的操作

        System.out.println("我是"+name+",我正在开门!");

        System.out.println("打开仓库门要3");

        try {

           admin.sleep(1000);

           System.out.println("3");

           admin.sleep(1000);

           System.out.println("2");

           admin.sleep(1000);

           System.out.println("1");

        }

        catch(InterruptedException ex){

           System.out.println("过程出现问题!");

        }

        System.out.println("仓库门打开了!");

     }

   }

}

Demo.java:

public class Demo {

   public static void main(String[] args){

     WareHouse warehouse = new WareHouse();

     warehouse.driver.setName("运货司机");//给线程进行命名

     warehouse.remover.setName("装运工");

     warehouse.admin.setName("仓库管理员");

     warehouse.driver.start();//司机的线程开始了

   }

}

 

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