java实验七多线程并发实验

一、题目描述

1(题目编号7179)、利用多线程技术编写一个模拟龟兔赛跑的程序,要求如下:(1)乌龟每次跑一个单位,兔子每次跑10个单位;(2)每个线程运行时,判断是否达到终点,如果到达终点,给出提示信息,未到终点则提示目前距离终点的距离,并判断是否领先;(3)如果兔子领先,则显示“我跑得快,睡一觉”信息,并睡一会。
2(题目编号8690)、编写多线程应用程序,模拟多人过独木桥的模拟。独木桥每次只能通过一个人,每个人通过木桥的时间为5秒,随机生成10个人,同时准备过此独木桥,显示一下每次通过独木桥人的姓名。需要用到随机数。
3(题目编号7180)、哈尔滨火车站下面有三个火车票代售点:哈站、哈东站、哈西站,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。注意:考虑线程同步问题,避免出现重复卖票问题。需要考虑同步问题。

二、代码实现

题目一内容:
 class Player implements Runnable{
	private int j=1000;
	private int w=0;
	private int t=0;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			if("乌龟".equals(Thread.currentThread().getName())) {
				w=w+1;
				if(w>j) {
					System.out.println("乌龟到达终点");
					break;
				}
				else {
					System.out.println("乌龟距离终点"+(j-w));
				}
			}
			else {
				if("兔子".equals(Thread.currentThread().getName())) {
					t=t+10;
					if(t>j) {
						System.out.println("兔子到达终点");
						break;
					}
					else {
						System.out.println("兔子距离终点"+(j-t));
					}
					if(t>w)
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							System.out.println("我跑得快,睡一觉");
						}
				}
			}
		}
	}
	
}
public class Main {
	public static void main(String[] args){
		Player player=new Player();
		Thread wt=new Thread(player,"乌龟");
		Thread tt =new Thread(player,"兔子");
		wt.start();
		tt.start();
	}
}

 
题目二内容:
 import java.util.HashSet;
import java.util.Set;

class People implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		synchronized(this) {
			System.out.println(Thread.currentThread().getName()+"正在过桥");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"已通过桥");
		}
	}
	
}
public class Main2 {

	public static void main(String[] args) {
	String[] name= {"一","二","三","四","五","六","七","八","九","十"};
	Set<Integer> set=new HashSet<Integer>();
	while(set.size()<10) {
		set.add((int)(Math.random()*10));
	}
	People people =new People();
	for(int a:set) {
		
		Thread thread =new Thread(people,name[a]);
		thread.start();
	}
	}
}



 
题目三内容:
class Sta implements Runnable{

	private int num=200;
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			synchronized(this) {
				System.out.println(Thread.currentThread().getName()+"正在售票"+num);
				num--;
				if(num<=0)
					break;
			}
		}
	}
	
}
public class Main3 {

	public static void main(String[] args) {
		Sta sta=new Sta();
		Thread x=new Thread(sta,"哈西");
		Thread d=new Thread(sta,"哈东");
		Thread h=new Thread(sta,"哈站");
		x.start();
		d.start();
		h.start();
	}

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当涉及到多线程并发控制时,可以使用 `synchronized` 关键字或 `Lock` 接口来实现线程间的同步。下面是一个使用 `synchronized` 关键字的示例: ```java public class ConcurrentThreadExample { private static int counter = 0; private static final int THREAD_COUNT = 5; private static final int INCREMENT_COUNT = 1000; public static void main(String[] args) { // 创建多个线程 Thread[] threads = new Thread[THREAD_COUNT]; for (int i = 0; i < THREAD_COUNT; i++) { threads[i] = new IncrementThread(); threads[i].start(); } // 等待所有线程执行完毕 for (int i = 0; i < THREAD_COUNT; i++) { try { threads[i].join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("最终的计数器值:" + counter); } static class IncrementThread extends Thread { @Override public void run() { for (int i = 0; i < INCREMENT_COUNT; i++) { incrementCounter(); } } private synchronized void incrementCounter() { counter++; } } } ``` 在这个示例中,我们创建了5个线程来增加一个共享的计数器。每个线程会执行1000次递增操作。过在 `incrementCounter()` 方法上添加 `synchronized` 关键字,我们确保了在任何时候只有一个线程可以执行递增操作,从而避免了竞态条件(race condition)。 当然,除了使用 `synchronized` 关键字,你也可以使用 `Lock` 接口及其实现类来实现线程间的同步。这种方式提供了更灵活的控制,例如可以使用 `tryLock()` 方法尝试非阻塞地获取锁。 希望这个示例对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我的大男子主义

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

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

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

打赏作者

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

抵扣说明:

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

余额充值