一、实验目的
1.掌握使用Thread子类和Runnable接口创建多线程的方法。
2.掌握线程的执行过程。
二、实验内容及代码
1(题目编号7179)、利用多线程技术编写一个模拟龟兔赛跑的程序,要求如下:
(1)乌龟每次跑一个单位,兔子每次跑10个单位;
(2)每个线程运行时,判断是否达到终点,如果到达终点,给出提示信息,未到终点则提示目前距离终点的距离,并判断是否领先;
(3)如果兔子领先,则显示“我跑得快,睡一觉”信息,并睡一会。
package com.homework7;
public class test1 extends Thread{
private int distance=100;
static int rubDistance=0;
static int turDistance=0;
static boolean flag= true;
public void run(){
String name= Thread.currentThread().getName();
while(flag){
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
if(name.equals("兔子")){
rubDistance=rubDistance+10;
//兔子休息
if(rubDistance>=turDistance&&rubDistance<distance){
System.out.println(name+"说:我还剩"+(distance-rubDistance)+"米!"+"我跑的快,先睡一觉。");
try{
Thread.sleep((long) (Math.random()*4));
}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(name+"说:我输了! 早知道不睡了!");
}
}
if(name.equals("乌龟")){
turDistance=turDistance+1;
if(!flag){
System.out.println(name+"说:我输了!");
}else{
System.out.println(name+"说:我还剩"+(distance-turDistance)+"米!");
}
if(turDistance==distance&&flag){//兔子未到达终点
System.out.println(name+"达到了终点,比赛结束!");
flag=false;
break;
}
}
}
}
public static void main(String[] args){
test1 rubbit=new test1();
rubbit.setName("兔子");
test1 turtle=new test1();
turtle.setName("乌龟");
rubbit.start();
turtle.start();
}
}
2(题目编号8690)、编写多线程应用程序,模拟多人过独木桥的模拟。独木桥每次只能通过一个人,每个人通过木桥的时间为5秒,随机生成10个人,同时准备过此独木桥,显示一下每次通过独木桥人的姓名。需要用到随机数
package com.homework7;
import java.util.*;
class SingleBridge implements Runnable {
@Override
public void run() {
synchronized(this) {//synchronized保证线程不被打断
System.out.println(Thread.currentThread().getName() + " 开始过桥");
try {
Thread.sleep(5000);//过桥
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 已过桥");
}
}
}
public class test2 {
static final int N = 10; // 人数
public static void main(String[] args) throws InterruptedException {
SingleBridge s = new SingleBridge();
List<Thread> threads = new ArrayList<>();
for(int i = 0; i < N; 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、哈尔滨火车站下面有三个火车票代售点:哈站、哈东站、哈西站,假如哈尔滨到北京的火车票总共是300张,如何用程序来实现三个售票点同时卖票的功能。注意:考虑线程同步问题,避免出现重复卖票问题7
package com.homework7;
class Ticket implements Runnable {
static boolean flag=true;
private int ticketCount = 20;// 总的票数,这个是共享资源,多个线程都会访问
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(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class test3 {
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();
}
}
方法二
package com.homework7;
class Ticket extends Thread {
private static int ticketCount = 100; // 总的票数,这个是共享资源,多个线程都会访问
private static Object lock = new Object(); // 锁对象,用于线程同步
private String stationName; // 车站名称
public Ticket(String stationName) {
this.stationName = stationName;
}
public void run() {
while (true) {
synchronized (lock) { // 加锁,保证同一时间只有一个线程访问共享资源
if (ticketCount > 0) {
ticketCount--;
System.out.println(stationName + "车站售出一张票,剩余票数:" + ticketCount);
} else {
System.out.println(stationName + "车站的票已售完");
break;
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class test4 {
public static void main(String[] args) {
Ticket haxi = new Ticket("哈西");
Ticket hadong = new Ticket("哈东");
Ticket hazhan = new Ticket("哈站");
haxi.start();
hadong.start();
hazhan.start();
}
}