哪位java大神帮帮这个碎催 一家店,有两个试衣间,有三个人,三个人同时挑衣服,前两个人一起去试衣服,第一个人试完衣服出来,结账走了,第三个人进去试衣服,第二个人试完衣服出来,觉得不合适又挑了一遍,又进去试衣服,第二个人试完衣服出来,结账走了,第三个人试完衣服出来没买走了怎么用线程做出来呀,求助求助 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个题目,是想让你运用多线程,以及锁的运用。3个人是3个线程。简单点,定义3类线程,P1,P2,P3,然后他们的行为是固定的:P1 要试衣服1次,然后买单;P2 要试衣服2次,然后买单;P3 要试衣服1次,然后不买单。试衣间是2个资源对象:room1 , room2 ,都是ReentrantLock 类型的。然后3个线程启动时,都会对room1 tryLock,超过一定时间锁不成功,就对room2 tryLock 。 还不成功,就等一会儿,循环这2步骤。得到某个tryLock成功,则进行试衣服动作。打思路要比代码累,你先试试coding一下吧。 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(); }} 遇到的JAVA问题 大家来看看 顺便祝大家5 1节快乐 线程问题 驱动包下载? 【java】 关于动态绑定 JAVA后面的TM是什么意思。 Exception in thread "main" java.lang.NoSuchMethodError: main InputStream的有关问题! jbuilder5中的问题 Java 工具 如何解决jbuilder 太慢的问题? jdk安装 ztree实用案例
3个人是3个线程。
简单点,定义3类线程,P1,P2,P3,然后他们的行为是固定的:
P1 要试衣服1次,然后买单;
P2 要试衣服2次,然后买单;
P3 要试衣服1次,然后不买单。试衣间是2个资源对象:room1 , room2 ,都是ReentrantLock 类型的。然后3个线程启动时,都会对room1 tryLock,超过一定时间锁不成功,就对room2 tryLock 。 还不成功,就等一会儿,循环这2步骤。
得到某个tryLock成功,则进行试衣服动作。
打思路要比代码累,你先试试coding一下吧。
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(); }}