实验七 多线程并发实验

一、实验目的

1.掌握使用Thread子类和Runnable接口创建多线程的方法。
2.掌握线程的执行过程。

二、 实验内容

7179

利用多线程技术编写一个模拟龟兔赛跑的程序,要求如下:

(1)乌龟每次跑一个单位,兔子每次跑10个单位;

(2)每个线程运行时,判断是否达到终点,如果到达终点,给出提示信息,未到终点则提示目前距离终点的距离,并判断是否领先;

(3)如果兔子领先,则显示“我跑得快,睡一觉”信息,并睡一会。

package Test;
import java.util.*;
import java.math.*;

class Run implements Runnable{
	int distance=50;
	int rabbit=0;
	int turtle=0;
	public void run() {
		if(Thread.currentThread().getName().equals("turtle")) {
			for(;turtle<distance;turtle+=1) 
				System.out.println("乌龟距离终点"+(distance-turtle));					
		    System.out.println("乌龟到达终点");
		}
		if(Thread.currentThread().getName().equals("rabbit")) {
			for(;rabbit<distance;rabbit+=10){
				System.out.println("兔子距离终点"+(distance-rabbit));
				if(rabbit>turtle) {
					System.out.println("兔子领先,将停下休息");
					try{
						Thread.sleep(1);
					}
					catch(Exception e){
						e.printStackTrace();
					}
				}
			}		
			System.out.println("兔子到达终点");
		}
	    	
	}
}
public class Test {
    public static void main(String[] args) throws IllegalArgumentException {
    	Scanner scanner=new Scanner(System.in);
        Run run=new Run();
        Thread thread1=new Thread(run,"turtle");
        Thread thread2=new Thread(run,"rabbit");
        thread1.start();
        thread2.start();
    }
}

结果:

8690

编写多线程应用程序,模拟多人过独木桥的模拟。

独木桥每次只能通过一个人,每个人通过木桥的时间为5秒,随机生成10个人,同时准备过此独木桥,显示一下每次通过独木桥人的姓名。需要用到随机数。 

package Test;
import java.util.*;
import java.math.*;

class Person implements Runnable{
	
	public void run() {
		synchronized(this) {
			System.out.println(Thread.currentThread().getName()+"正在过桥");
	    	try {
	    		Thread.sleep(5000);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	    	System.out.println(Thread.currentThread().getName()+"已过桥");
		}	
	}
}
public class Test {
    public static void main(String[] args) throws IllegalArgumentException {
    	Scanner scanner=new Scanner(System.in);
    	String[] randomName=new String[100];
    	String[] name=new String[10];
    	Person person=new Person();
    	for(int i=0;i<100;i++) {
    		randomName[i]=""+(i+1);
    	}	
    	for(int i=0;i<10;i++) {
    		name[i]=randomName[(int)(Math.random()*15)];
    		//System.out.print((int)(Math.random()*10)+" ");
    	}
    	for(String i:name)
    		System.out.println("名字:"+i);
        for(String i:name){
        	Thread thread=new Thread(person,i);
        	thread.start();
        } 
    }
}

结果:

 7180

哈尔滨火车站下面有三个火车票代售点:哈站、哈东站、哈西站,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。

注意:考虑线程同步问题,避免出现重复卖票问题。需要考虑同步问题。

package Test;
import java.util.*;
import java.math.*;

class Station implements Runnable{
    private int tickets=10; 
	public void run() {
		while(true){
			synchronized(this){
				if(tickets<=0)
            		break;
				System.out.println(Thread.currentThread().getName()+"  "+tickets+"号票已卖出");
            	tickets--; 	 	
			}
			try {
				Thread.sleep(100);
			}
			catch(Exception e) {
				e.printStackTrace();
			}
		}
	}	
}
public class Test {
    public static void main(String[] args) throws IllegalArgumentException {
    	Scanner scanner=new Scanner(System.in);
    	Station station=new Station();
    	Thread haStation=new Thread(station,"哈站");
    	Thread haEastStation=new Thread(station,"哈东站");
    	Thread haWestStation=new Thread(station,"哈西站");
    	haStation.start();
    	haEastStation.start();
    	haWestStation.start();
    }
}

结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

烟芜~镜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值