Java多线程编程小实例模拟停车场系统
作者:diaotai 时间:2022-07-07 06:36:29
下面分享的是一个Java多线程模拟停车场系统的小实例(Java的应用还是很广泛的,哈哈),具体代码如下:
Park类
public class Park {
boolean []park=new boolean[3];
public boolean equals()
{
return true;
}
}
Car:
public class Car {
private String number;
private int position=0;
public Car(String number)
{
this.number=number;
}
public void setNumber(String number)
{
this.number=number;
}
public String getNumber()
{
return number;
}
public void setPosition(int i)
{
position=i;
}
public int getPosition()
{
return position;
}
public int hashCode()
{
return number.hashCode();
}
public boolean equals(Object obj)
{
if(obj==this)
return true;
if(obj!=null&&obj.getClass()==Car.class)
{
Car c=(Car)obj;
return c.number==this.number;
}
return false;
}
}
ParkCar:
public class ParkCar implements Runnable
{
Car c;
Park p;
public ParkCar(Car c,Park p)
{
this.c=c;
this.p=p;
}
@Override
public void run()
{
while(true)
{
int i=0;
synchronized(p)
{
if(c.getPosition()==0)
{
for(i=0;i<p.park.length;i++)
{
if(p.park[i])
{
continue;
}
else
{
System.out.println("车 "+c.getNumber()+"成功停在"+(i+1)+"号停车位上");
p.park[i]=true;
c.setPosition(i);
break;
}
}
if(i==3)
System.out.println("停车场车位已满,车 "+c.getNumber()+"停车失败");
}
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
synchronized(p)
{
if(c.getPosition()!=0)
{
p.park[c.getPosition()]=false;
c.setPosition(0);
System.out.println("车 "+c.getNumber()+"已离开停车场");
}
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
}
}
Test2:
public class Test2 {
public static void main(String []args)
{
Park p=new Park();
Car a=new Car("111111");
Car b=new Car("222222");
Car c=new Car("333333");
Car d=new Car("444444");
Car e=new Car("555555");
new Thread (new ParkCar(a,p)).start();
new Thread (new ParkCar(c,p)).start();
new Thread (new ParkCar(d,p)).start();
new Thread (new ParkCar(e,p)).start();
new Thread (new ParkCar(b,p)).start();
}
}
运行结果如下:
车 111111成功停在1号停车位上
车 111111成功停在2号停车位上
车 222222成功停在3号停车位上
车 222222已离开停车场
车 222222成功停在3号停车位上
车 222222已离开停车场
车 222222成功停在3号停车位上
车 222222已离开停车场
车 555555成功停在3号停车位上
车 555555已离开停车场
车 555555成功停在3号停车位上
停车场车位已满,车 444444停车失败
····
总结
Java多线程编程实现socket通信示例代码
浅谈Java多线程处理中Future的妙用(附源码)
Java通过卖票理解多线程
更多精彩内容尽在:脚本之家!
在代码的世界里,你就是上帝!
来源:http://blog.csdn.net/diaotai/article/details/52790942
标签:java,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
idea中使用(Revert Commit)图解
2023-03-22 18:45:58
![](https://img.aspxhome.com/file/2023/9/110659_0s.png)
Flutter质感设计之持久底部面板
2021-10-19 07:54:18
![](https://img.aspxhome.com/file/2023/3/123323_0s.jpg)
详解C++设计模式编程中建造者模式的实现
2022-10-25 01:33:20
![](https://img.aspxhome.com/file/2023/2/129552_0s.jpg)
Mybatis如何使用ognl表达式实现动态sql
2021-06-22 03:34:49
volatile与happens-before的关系与内存一致性错误
2021-12-13 20:25:37
Android EditText 监听用户输入完成的实例
2022-06-22 18:41:54
C#中WPF使用多线程调用窗体组件的方法
2023-04-24 11:47:57
解读Spring定义Bean的两种方式:<bean>和@Bean
2023-01-25 23:37:51
Java 设计模式中的命令模式详情
2023-11-15 23:25:33
![](https://img.aspxhome.com/file/2023/3/59683_0s.png)
Java利用反射自动封装成实体对象的方法
2022-01-06 05:54:29
关于eclipse安装spring插件报错An error occurred while collecting items to be installed...解决方案
2023-05-27 03:34:45
![](https://img.aspxhome.com/file/2023/1/78351_0s.png)
在IDEA使用中directory和package的操作
2022-07-05 22:05:53
![](https://img.aspxhome.com/file/2023/9/79339_0s.jpg)
Android Support Library 标题栏(Toolbar)滚动效果实现方法
2022-10-24 03:25:12
![](https://img.aspxhome.com/file/2023/4/138974_0s.gif)
Java数据结构超详细分析二叉搜索树
2022-12-01 01:34:20
![](https://img.aspxhome.com/file/2023/3/61023_0s.jpg)
Android Studio做超好玩的拼图游戏 附送详细注释源码
2023-08-05 12:19:16
![](https://img.aspxhome.com/file/2023/4/84744_0s.jpg)
深入理解Java设计模式之外观模式
2023-05-02 09:48:00
![](https://img.aspxhome.com/file/2023/2/68562_0s.jpg)
android中Bitmap用法(显示,保存,缩放,旋转)实例分析
2022-07-03 14:18:41
Android仿微信朋友圈图片查看器
2023-01-27 18:07:20
![](https://img.aspxhome.com/file/2023/8/122228_0s.jpg)
Android判断11位手机号码的方法(正则表达式)
2022-03-06 03:17:43
Spring AOP与AspectJ的对比及应用详解
2022-07-07 12:50:47
![](https://img.aspxhome.com/file/2023/3/81363_0s.png)