java多线程的同步控制与线程间的通信
多线程的同步控制与线程间的通信:用synchronized、wait()和notifyAll()完成以下情景模拟3个人排队买票,每人买一张票。售票员(TicketSell...
多线程的同步控制与线程间的通信:用synchronized、wait()和notifyAll()完成以下情景
模拟3个人排队买票,每人买一张票。售票员(TicketSeller类)只有1张5元的钱,电影票5元一张。张某拿着1张20元的人民币排在第一,孙某拿着1张10元的人民币排在第二,赵某拿着1张5元的人民币排在第三。
(提示:定义一个售票员TicketSeller类,属性包括5元钱张数fiveNumber、10元钱张数tenNumber和20元钱张数twentyNumber,方法为同步方法卖票sellTicket(int receiveMoney), 创建三个线程张某Zhang、孙某Sun和赵某Zhao,这三个线程共享一个售票员类对象。)
java编程 展开
模拟3个人排队买票,每人买一张票。售票员(TicketSeller类)只有1张5元的钱,电影票5元一张。张某拿着1张20元的人民币排在第一,孙某拿着1张10元的人民币排在第二,赵某拿着1张5元的人民币排在第三。
(提示:定义一个售票员TicketSeller类,属性包括5元钱张数fiveNumber、10元钱张数tenNumber和20元钱张数twentyNumber,方法为同步方法卖票sellTicket(int receiveMoney), 创建三个线程张某Zhang、孙某Sun和赵某Zhao,这三个线程共享一个售票员类对象。)
java编程 展开
2个回答
展开全部
按照下面这个运行结果(最后两行顺序不一定):
小张 用 20 元买票
小孙 用 10 元买票
小孙 买到票并拿回 5 元
小赵 用 5 元买票
小赵 买到票并拿回 0 元
小张 买到票并拿回 15 元
public class Main {
public static void main(String[] args) {
try {
TicketSeller ts = new TicketSeller();
// 张某拿着1张20元的人民币排在第一,孙某拿着1张10元的人民币排在第二,赵某拿着1张5元的人民币排在第三。
TicketBuyer zhang = new TicketBuyer(ts, "小张", 20);
Thread tZhang = new Thread(zhang);
tZhang.start();
Thread.sleep(100); // 确保买票顺序
TicketBuyer sun = new TicketBuyer(ts, "小孙", 10);
Thread tSun = new Thread(sun);
tSun.start();
Thread.sleep(100);
TicketBuyer zhao = new TicketBuyer(ts, "小赵", 5);
Thread tZhao = new Thread(zhao);
tZhao.start();
} catch (InterruptedException ex) {
}
}
}
class TicketSeller{
//电影票5元一张
private static final int TICKET_PRICE = 5;
private int fiveNumber, tenNumber, twentyNumber;
public TicketSeller(){
//售票员只有1张5元的钱
fiveNumber = 1;
tenNumber = twentyNumber = 0;
}
public synchronized int sellTicket(int receiveMoney){
int changeRequired = receiveMoney - TICKET_PRICE;
while(!prepareChange(changeRequired)){
try {
this.wait();
} catch (InterruptedException ex) {
// Do nothing
}
}
switch(receiveMoney){
case 5: fiveNumber++; break;
case 10: tenNumber++; break;
case 20: twentyNumber++; break;
default: System.out.println("错误001");
}
this.notify();
return changeRequired;
}
/**
* 给出找钱
* @param changeRequired 需要找的钱数
* @return 是否能给出
*/
private boolean prepareChange(int changeRequired) {
switch(changeRequired){
case 0:
return true;
case 5:
if(fiveNumber >= 1){
fiveNumber--;
return true;
}
break;
case 10:
// 找一张10元的
if(tenNumber >= 1){
tenNumber--;
return true;
}
// 找两张5元的
if(fiveNumber >= 2){
fiveNumber-=2;
return true;
}
break;
case 15:
// 找一张10元的+一张5元的
if(tenNumber >= 1 && fiveNumber >= 1){
tenNumber--;
fiveNumber--;
return true;
}
// 找3张5元的
if(fiveNumber >= 3){
fiveNumber-=3;
return true;
}
break;
}
return false;
}
}
class TicketBuyer implements Runnable{
private TicketSeller ts;
private int moneyAvailable;
private String myName;
public TicketBuyer(TicketSeller ts, String name, int moneyAvailable){
this.ts = ts;
this.myName = name;
this.moneyAvailable = moneyAvailable;
}
public void run() {
System.out.printf("%s 用 %d 元买票\r\n", this.myName, this.moneyAvailable);
int change = this.ts.sellTicket(this.moneyAvailable);
System.out.printf("%s 买到票并拿回 %d 元\r\n", this.myName, change);
}
}
小张 用 20 元买票
小孙 用 10 元买票
小孙 买到票并拿回 5 元
小赵 用 5 元买票
小赵 买到票并拿回 0 元
小张 买到票并拿回 15 元
public class Main {
public static void main(String[] args) {
try {
TicketSeller ts = new TicketSeller();
// 张某拿着1张20元的人民币排在第一,孙某拿着1张10元的人民币排在第二,赵某拿着1张5元的人民币排在第三。
TicketBuyer zhang = new TicketBuyer(ts, "小张", 20);
Thread tZhang = new Thread(zhang);
tZhang.start();
Thread.sleep(100); // 确保买票顺序
TicketBuyer sun = new TicketBuyer(ts, "小孙", 10);
Thread tSun = new Thread(sun);
tSun.start();
Thread.sleep(100);
TicketBuyer zhao = new TicketBuyer(ts, "小赵", 5);
Thread tZhao = new Thread(zhao);
tZhao.start();
} catch (InterruptedException ex) {
}
}
}
class TicketSeller{
//电影票5元一张
private static final int TICKET_PRICE = 5;
private int fiveNumber, tenNumber, twentyNumber;
public TicketSeller(){
//售票员只有1张5元的钱
fiveNumber = 1;
tenNumber = twentyNumber = 0;
}
public synchronized int sellTicket(int receiveMoney){
int changeRequired = receiveMoney - TICKET_PRICE;
while(!prepareChange(changeRequired)){
try {
this.wait();
} catch (InterruptedException ex) {
// Do nothing
}
}
switch(receiveMoney){
case 5: fiveNumber++; break;
case 10: tenNumber++; break;
case 20: twentyNumber++; break;
default: System.out.println("错误001");
}
this.notify();
return changeRequired;
}
/**
* 给出找钱
* @param changeRequired 需要找的钱数
* @return 是否能给出
*/
private boolean prepareChange(int changeRequired) {
switch(changeRequired){
case 0:
return true;
case 5:
if(fiveNumber >= 1){
fiveNumber--;
return true;
}
break;
case 10:
// 找一张10元的
if(tenNumber >= 1){
tenNumber--;
return true;
}
// 找两张5元的
if(fiveNumber >= 2){
fiveNumber-=2;
return true;
}
break;
case 15:
// 找一张10元的+一张5元的
if(tenNumber >= 1 && fiveNumber >= 1){
tenNumber--;
fiveNumber--;
return true;
}
// 找3张5元的
if(fiveNumber >= 3){
fiveNumber-=3;
return true;
}
break;
}
return false;
}
}
class TicketBuyer implements Runnable{
private TicketSeller ts;
private int moneyAvailable;
private String myName;
public TicketBuyer(TicketSeller ts, String name, int moneyAvailable){
this.ts = ts;
this.myName = name;
this.moneyAvailable = moneyAvailable;
}
public void run() {
System.out.printf("%s 用 %d 元买票\r\n", this.myName, this.moneyAvailable);
int change = this.ts.sellTicket(this.moneyAvailable);
System.out.printf("%s 买到票并拿回 %d 元\r\n", this.myName, change);
}
}
展开全部
按照下面这个运行结果(最后两行顺序不一定):
小张
用
20
元买票
小孙
用
10
元买票
小孙
买到票并拿回
5
元
小赵
用
5
元买票
小赵
买到票并拿回
0
元
小张
买到票并拿回
15
元
public
class
Main
{
public
static
void
main(String[]
args)
{
try
{
TicketSeller
ts
=
new
TicketSeller();
//
张某拿着1张20元的人民币排在第一,孙某拿着1张10元的人民币排在第二,赵某拿着1张5元的人民币排在第三。
TicketBuyer
zhang
=
new
TicketBuyer(ts,
"小张",
20);
Thread
tZhang
=
new
Thread(zhang);
tZhang.start();
Thread.sleep(100);
//
确保买票顺序
TicketBuyer
sun
=
new
TicketBuyer(ts,
"小孙",
10);
Thread
tSun
=
new
Thread(sun);
tSun.start();
Thread.sleep(100);
TicketBuyer
zhao
=
new
TicketBuyer(ts,
"小赵",
5);
Thread
tZhao
=
new
Thread(zhao);
tZhao.start();
}
catch
(InterruptedException
ex)
{
}
}
}
class
TicketSeller{
//电影票5元一张
private
static
final
int
TICKET_PRICE
=
5;
private
int
fiveNumber,
tenNumber,
twentyNumber;
public
TicketSeller(){
//售票员只有1张5元的钱
fiveNumber
=
1;
tenNumber
=
twentyNumber
=
0;
}
public
synchronized
int
sellTicket(int
receiveMoney){
int
changeRequired
=
receiveMoney
-
TICKET_PRICE;
while(!prepareChange(changeRequired)){
try
{
this.wait();
}
catch
(InterruptedException
ex)
{
//
Do
nothing
}
}
switch(receiveMoney){
case
5:
fiveNumber++;
break;
case
10:
tenNumber++;
break;
case
20:
twentyNumber++;
break;
default:
System.out.println("错误001");
}
this.notify();
return
changeRequired;
}
/**
*
给出找钱
*
@param
changeRequired
需要找的钱数
*
@return
是否能给出
*/
private
boolean
prepareChange(int
changeRequired)
{
switch(changeRequired){
case
0:
return
true;
case
5:
if(fiveNumber
>=
1){
fiveNumber--;
return
true;
}
break;
case
10:
//
找一张10元的
if(tenNumber
>=
1){
tenNumber--;
return
true;
}
//
找两张5元的
if(fiveNumber
>=
2){
fiveNumber-=2;
return
true;
}
break;
case
15:
//
找一张10元的+一张5元的
if(tenNumber
>=
1
&&
fiveNumber
>=
1){
tenNumber--;
fiveNumber--;
return
true;
}
//
找3张5元的
if(fiveNumber
>=
3){
fiveNumber-=3;
return
true;
}
break;
}
return
false;
}
}
class
TicketBuyer
implements
Runnable{
private
TicketSeller
ts;
private
int
moneyAvailable;
private
String
myName;
public
TicketBuyer(TicketSeller
ts,
String
name,
int
moneyAvailable){
this.ts
=
ts;
this.myName
=
name;
this.moneyAvailable
=
moneyAvailable;
}
public
void
run()
{
System.out.printf("%s
用
%d
元买票\r\n",
this.myName,
this.moneyAvailable);
int
change
=
this.ts.sellTicket(this.moneyAvailable);
System.out.printf("%s
买到票并拿回
%d
元\r\n",
this.myName,
change);
}
}
小张
用
20
元买票
小孙
用
10
元买票
小孙
买到票并拿回
5
元
小赵
用
5
元买票
小赵
买到票并拿回
0
元
小张
买到票并拿回
15
元
public
class
Main
{
public
static
void
main(String[]
args)
{
try
{
TicketSeller
ts
=
new
TicketSeller();
//
张某拿着1张20元的人民币排在第一,孙某拿着1张10元的人民币排在第二,赵某拿着1张5元的人民币排在第三。
TicketBuyer
zhang
=
new
TicketBuyer(ts,
"小张",
20);
Thread
tZhang
=
new
Thread(zhang);
tZhang.start();
Thread.sleep(100);
//
确保买票顺序
TicketBuyer
sun
=
new
TicketBuyer(ts,
"小孙",
10);
Thread
tSun
=
new
Thread(sun);
tSun.start();
Thread.sleep(100);
TicketBuyer
zhao
=
new
TicketBuyer(ts,
"小赵",
5);
Thread
tZhao
=
new
Thread(zhao);
tZhao.start();
}
catch
(InterruptedException
ex)
{
}
}
}
class
TicketSeller{
//电影票5元一张
private
static
final
int
TICKET_PRICE
=
5;
private
int
fiveNumber,
tenNumber,
twentyNumber;
public
TicketSeller(){
//售票员只有1张5元的钱
fiveNumber
=
1;
tenNumber
=
twentyNumber
=
0;
}
public
synchronized
int
sellTicket(int
receiveMoney){
int
changeRequired
=
receiveMoney
-
TICKET_PRICE;
while(!prepareChange(changeRequired)){
try
{
this.wait();
}
catch
(InterruptedException
ex)
{
//
Do
nothing
}
}
switch(receiveMoney){
case
5:
fiveNumber++;
break;
case
10:
tenNumber++;
break;
case
20:
twentyNumber++;
break;
default:
System.out.println("错误001");
}
this.notify();
return
changeRequired;
}
/**
*
给出找钱
*
@param
changeRequired
需要找的钱数
*
@return
是否能给出
*/
private
boolean
prepareChange(int
changeRequired)
{
switch(changeRequired){
case
0:
return
true;
case
5:
if(fiveNumber
>=
1){
fiveNumber--;
return
true;
}
break;
case
10:
//
找一张10元的
if(tenNumber
>=
1){
tenNumber--;
return
true;
}
//
找两张5元的
if(fiveNumber
>=
2){
fiveNumber-=2;
return
true;
}
break;
case
15:
//
找一张10元的+一张5元的
if(tenNumber
>=
1
&&
fiveNumber
>=
1){
tenNumber--;
fiveNumber--;
return
true;
}
//
找3张5元的
if(fiveNumber
>=
3){
fiveNumber-=3;
return
true;
}
break;
}
return
false;
}
}
class
TicketBuyer
implements
Runnable{
private
TicketSeller
ts;
private
int
moneyAvailable;
private
String
myName;
public
TicketBuyer(TicketSeller
ts,
String
name,
int
moneyAvailable){
this.ts
=
ts;
this.myName
=
name;
this.moneyAvailable
=
moneyAvailable;
}
public
void
run()
{
System.out.printf("%s
用
%d
元买票\r\n",
this.myName,
this.moneyAvailable);
int
change
=
this.ts.sellTicket(this.moneyAvailable);
System.out.printf("%s
买到票并拿回
%d
元\r\n",
this.myName,
change);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询