Java实验七 多线程并发实验(没写完)

实验内容    

1(题目编号7179)、利用多线程技术编写一个模拟龟兔赛跑的程序,要求如下:(1)乌龟每次跑一个单位,兔子每次跑10个单位;(2)每个线程运行时,判断是否达到终点,如果到达终点,给出提示信息,未到终点则提示目前距离终点的距离,并判断是否领先;(3)如果兔子领先,则显示“我跑得快,睡一觉”信息,并睡一会。

2(题目编号8690)、编写多线程应用程序,模拟多人过独木桥的模拟。独木桥每次只能通过一个人,每个人通过木桥的时间为5秒,随机生成10个人,同时准备过此独木桥,显示一下每次通过独木桥人的姓名。需要用到随机数。

3(题目编号7180)、哈尔滨火车站下面有三个火车票代售点:哈站、哈东站、哈西站,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。注意:考虑线程同步问题,避免出现重复卖票问题。需要考虑同步问题。


public class Test7179 extends Thread{
    /*
1(题目编号7179)、利用多线程技术编写一个模拟龟兔赛跑的程序,要求如下:(1)乌龟每次跑一个单位,兔子每次跑10个单位;
(2)每个线程运行时,判断是否达到终点,如果到达终点,给出提示信息,未到终点则提示目前距离终点的距离,并判断是否领先;
(3)如果兔子领先,则显示“我跑得快,睡一觉”信息,并睡一会。
     */
    private int distance=100;
    static int rubdistance=0;
    static int turdistance=0;
    static boolean flag=true;
    public void run(){
        while(flag){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String name=Thread.currentThread().getName();
            if(name.equals("兔子")){
                rubdistance+=10;
                if(rubdistance>turdistance&&rubdistance<distance){
                    System.out.println(name+"还剩"+(distance-rubdistance)+"米");
                    System.out.println("我跑得快先睡一觉");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                else{
                    System.out.println(name+"还剩"+(distance-rubdistance)+"米");
                }
                if(rubdistance==distance){
                    System.out.println(name+"到达了终点,比赛结束!");
                    flag=false;
                    break;
                }
                if(!flag){
                    System.out.println("兔子输了");
                }
            }
            if(name.equals("乌龟")){
                turdistance+=1;
                if(!flag){
                    System.out.println("乌龟输了");
                }
                else{
                    System.out.println(name+"还剩"+(distance-turdistance)+"米");

                }
                if(turdistance==distance&&flag){
                    System.out.println("乌龟到达了终点,比赛结束!");
                    flag=false;
                    break;
                }
            }

        }
    }
    public static void main(String[] args) {
        Test7179 rubbit=new Test7179();
        rubbit.setName("兔子");
        Test7179 turtle=new Test7179();
        turtle.setName("乌龟");
        rubbit.start();
        turtle.start();

    }

}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class singlebridge implements Runnable{

    @Override
    public void run() {
        synchronized (this){
            System.out.println(Thread.currentThread().getName()+"开始过桥");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"已过桥");
        }
    }
}
public class Test8690 extends Thread {
    /*编写多线程应用程序,模拟多人过独木桥的模拟。独木桥每次只能通过一个人,每个人通过木桥的时间为5秒,随机生成10个人,
    同时准备过此独木桥,显示一下每次通过独木桥人的姓名。需要用到随机数。
     */
    public static void main(String[] args) {
        int N=10;
        singlebridge s=new singlebridge();
        List<Thread>threads=new ArrayList<>();
        for(int i=0;i<10;i++){
            threads.add(new Thread(s,"name"+i));
        }
        Random random=new Random();
        while(threads.size()>0){
            int index=random.nextInt(threads.size());
            threads.get((index)).start();
            threads.remove(index);
        }

    }
}
/*3(题目编号7180)、哈尔滨火车站下面有三个火车票代售点:哈站、哈东站、哈西站
,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。注意:考虑线程同步问题,
避免出现重复卖票问题。需要考虑同步问题。
 */
class Ticket implements Runnable{
    boolean flag=true;
    int ticketCount=200;//总票数
    @Override
    public void run() {
        while(flag){//循环让线程不停买票
            synchronized (this){//锁线程
                if(ticketCount>0) {
                    ticketCount--;
                    System.out.println(Thread.currentThread().getName() + "正在卖出一张票,还剩" + ticketCount + "张票");
                    if (ticketCount == 0) {
                        System.out.println("今天得票卖光了");
                        flag = false;
                        break;
                    }
                }
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
public class Test7180 {
    public static void main(String[] args) {
        Ticket ticket=new Ticket();
        //建立三个卖票线程;
        Thread haxi=new Thread(ticket);
        haxi.setName("哈西");
        Thread hadong=new Thread(ticket);
        hadong.setName("哈东");
        Thread hazhan=new Thread(ticket);
        hazhan.setName("哈站");
        haxi.start();
        hazhan.start();
        hadong.start();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_56350439/article/details/124459993