哪位java大神帮帮这个碎催

草莓味的J先生 2019-09-07 11:08:48
一家店,有两个试衣间,有三个人,三个人同时挑衣服,前两个人一起去试衣服,第一个人试完衣服出来,结账走了,第三个人进去试衣服,第二个人试完衣服出来,觉得不合适又挑了一遍,又进去试衣服,第二个人试完衣服出来,结账走了,第三个人试完衣服出来没买走了 怎么用线程做出来呀,求助求助
...全文
192 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
哇,感谢感谢,膜拜大神
qq_39936465 2019-09-09
  • 打赏
  • 举报
回复
引用 楼主 草莓味的J先生 的回复:
一家店,有两个试衣间,有三个人,三个人同时挑衣服,前两个人一起去试衣服,第一个人试完衣服出来,结账走了,第三个人进去试衣服,第二个人试完衣服出来,觉得不合适又挑了一遍,又进去试衣服,第二个人试完衣服出来,结账走了,第三个人试完衣服出来没买走了 怎么用线程做出来呀,求助求助

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Action {

	private Semaphore roomManager = new Semaphore(2, true);
	private Lock room1 = new ReentrantLock();
	private Lock room2 = new ReentrantLock();

	public void choose() {
		try {
			System.out.println(Thread.currentThread().getName() + "正在挑衣服.....");
			Thread.currentThread().sleep(3000);
			System.out.println(Thread.currentThread().getName() + "挑选完成准备试衣");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void fitting() {
		try {
			roomManager.acquire();
			if (room1.tryLock()) {
				try {
					System.out.println(Thread.currentThread().getName() + "正在用1号试衣室");
					Thread.currentThread().sleep(3000);
					System.out.println(Thread.currentThread().getName() + "试衣完成,1号试衣室可以使用了");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					room1.unlock();
				}
			} else if (room2.tryLock()) {
				try {
					System.out.println(Thread.currentThread().getName() + "正在用2号试衣室");
					Thread.currentThread().sleep(3000);
					System.out.println(Thread.currentThread().getName() + "试衣完成,2号试衣室可以使用了");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					room2.unlock();
				}	
			}
			roomManager.release();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void pay(boolean flag) {
		if (flag) {
			try {
				System.out.println(Thread.currentThread().getName() + "正在买单.....");
				Thread.currentThread().sleep(3000);
				System.out.println(Thread.currentThread().getName() + "买单购物结束");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			System.out.println(Thread.currentThread().getName() + "决定不买了");
		}
	}
}

public class Person extends Thread {
	private Action action;
	private int count;
	private boolean flag;

	/**
	 * @param action
	 */
	public Person(String name, Action action, int count, boolean flag) {
		super(name);
		this.action = action;
		this.count = count;
		this.flag = flag;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (count > 0) {
			action.choose();
			action.fitting();
			count--;
		}
		action.pay(flag);
	}

}

public class test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Action act = new Action();
		Person p1 = new Person("P1", act, 1, true);
		Person p2 = new Person("P2", act, 2, true);
		Person p3 = new Person("P3", act, 1, false);
		p1.start();
		p2.start();
		p3.start();

	}

}
oh_Maxy 2019-09-08
  • 打赏
  • 举报
回复
这个题目,是想让你运用多线程,以及锁的运用。
3个人是3个线程。
简单点,定义3类线程,P1,P2,P3,然后他们的行为是固定的:
P1 要试衣服1次,然后买单;
P2 要试衣服2次,然后买单;
P3 要试衣服1次,然后不买单。

试衣间是2个资源对象:room1 , room2 ,都是ReentrantLock 类型的。

然后3个线程启动时,都会对room1 tryLock,超过一定时间锁不成功,就对room2 tryLock 。 还不成功,就等一会儿,循环这2步骤。
得到某个tryLock成功,则进行试衣服动作。


打思路要比代码累,你先试试coding一下吧。

62,628

社区成员

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

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