编程求助啊!!!!

qq_37281791 2017-01-07 10:13:02
多线程问题 一个桥两边各有5个人要过桥 但桥上每次只能过两个人 线程同步来实现10个人争抢过桥 !!! 菜鸟求教啊 大神帮帮忙
...全文
328 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2017-01-10
  • 打赏
  • 举报
回复
package houlei.support.communicate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;

public class CrossBridge {

	private static class People{
		public int id;
		public RiverSide belong;
		public People(int id){
			this.id = id;
		}
	}
	private static class RiverSide{
		public String name;
		public ConcurrentLinkedQueue<People> waiters = new ConcurrentLinkedQueue<People>();
	}
	private static class Bridge{
		Semaphore semaphore = new Semaphore(2);
		public void cross(RiverSide fromSide, People people, RiverSide toSide) throws InterruptedException{
			synchronized (fromSide) {
				if(!fromSide.waiters.contains(people)){
					throw new IllegalArgumentException("未找到要过河的人");
				}
				fromSide.waiters.remove(people);
			}
			semaphore.acquire();
			// 过河过程中,采用随机延时操作代替。
			SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");
			System.out.println(format.format(new Date()) + " 有个人(id:"+people.id+")从岸边("+fromSide.name+")过和到对岸("+toSide.name+")");
			Random rand = new Random();
			try {
				Thread.sleep(rand.nextInt(500));
			} catch (InterruptedException e) {
			}
			System.out.println(format.format(new Date()) + " 有个人(id:"+people.id+")从岸边("+fromSide.name+")过和到对岸("+toSide.name+")");
			synchronized (toSide) {
				if(toSide.waiters.contains(people)){
					throw new IllegalArgumentException("过河的人已经在对岸了");
				}
				toSide.waiters.add(people);
			}
			semaphore.release();
		}
	}
	private static class CrossExecutor extends Thread{
		final RiverSide from;
		final Bridge bridge;
		final RiverSide to;
		public CrossExecutor(RiverSide from, Bridge bridge, RiverSide to) {
			super();
			this.from = from;
			this.bridge = bridge;
			this.to = to;
		}
		public void run(){
			for(People people : from.waiters){
				if(people.belong != from){
					continue;
				}
				try {
					bridge.cross(from, people, to);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) throws InterruptedException {
		RiverSide sideA = new RiverSide();
		sideA.name = "Side_A";
		for(int i=0; i<5; i++){
			People p = new People(11 + i);
			p.belong = sideA;
			sideA.waiters.add(p);
		}
		RiverSide sideB = new RiverSide();
		sideB.name = "Side_B";
		for(int i=0; i<5; i++){
			People p = new People(21 + i);
			p.belong = sideB;
			sideB.waiters.add(p);
		}
		Bridge bridge = new Bridge();
		CrossExecutor crossA2B = new CrossExecutor(sideA, bridge, sideB);
		CrossExecutor crossB2A = new CrossExecutor(sideB, bridge, sideA);
		crossA2B.start();
		crossB2A.start();
		// 等待完成
		Thread.sleep(600);
	}

}
ChanceZhao 2017-01-09
  • 打赏
  • 举报
回复
引用 4 楼 baidu_37015777 的回复:
[quote=引用 3 楼 baidu_37015777 的回复:] public class test1 { public static void main(String[] args) throws Exception { test1 t=new test1(); for (int i = 0; i < 10; i++) { t.guoqiang(i); } } public synchronized void guoqiang(int threadName) throws Exception{ System.out.println(threadName+"过桥"); Thread.sleep(2000); } }
10个人轮流过[/quote] 看错原来是两人过
ChanceZhao 2017-01-09
  • 打赏
  • 举报
回复
引用 3 楼 baidu_37015777 的回复:
public class test1 { public static void main(String[] args) throws Exception { test1 t=new test1(); for (int i = 0; i < 10; i++) { t.guoqiang(i); } } public synchronized void guoqiang(int threadName) throws Exception{ System.out.println(threadName+"过桥"); Thread.sleep(2000); } }
10个人轮流过
ChanceZhao 2017-01-09
  • 打赏
  • 举报
回复
public class test1 { public static void main(String[] args) throws Exception { test1 t=new test1(); for (int i = 0; i < 10; i++) { t.guoqiang(i); } } public synchronized void guoqiang(int threadName) throws Exception{ System.out.println(threadName+"过桥"); Thread.sleep(2000); } }
ryuugu_rena 2017-01-09
  • 打赏
  • 举报
回复
引用 1 楼 qq_33787137 的回复:
能不能就给2条线程,然后10个人排队呢
这个应该可以
卡桑的大黄瓜 2017-01-08
  • 打赏
  • 举报
回复
能不能就给2条线程,然后10个人排队呢

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧