用线程解决简单的两车相遇
来源:互联网 发布:科尔盖特大学 知乎 编辑:程序博客网 时间:2024/06/06 07:04
第一辆左向右开的车每小时行驶60千米,第二辆从右向左的每小时行驶
38千米。问在这条路上,多长时间两车相遇(以每秒模拟每小时)
public class TestMain {
public static void main(String[] args) throws InterruptedException {
LeftCar leftcar = new LeftCar();
RightThread rightcar = new RightThread();
leftcar.start();
rightcar.start();
while(true){
int dis = leftcar.speed*leftcar.times+rightcar.speed*rightcar.times;
if (dis>333) {
System.out.println("会车了,用了"+rightcar.times+"秒");
System.exit(0);
}
Thread.sleep(500);
}
}
}
public class LeftCar extends Thread{
public int speed = 60;
public int times =0;
@Override
public void run() {
while (true){
times++;
System.out.println("左车行驶了"+speed*times);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class RightThread extends Thread{
public int speed = 38;
public int times =0;
@Override
public void run() {
while(true){
times++;
System.out.println("右车行驶了"+speed*times);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
- 用线程解决简单的两车相遇
- 鸽子在两人相遇前的飞行路程
- 多线程——用创建线程的两种方式分别解决经典窗口卖票问题
- 两强相遇“乐”者胜
- 线程的两种创建方式及简单的对比
- 本人原创!解决安卓ListView复用问题最简单的两种方法。
- hdu2571 简单dp的两种解决思路
- 世界上,两个人能相遇不容易,有你这样的朋友,我很感激!!
- 相遇的魔咒
- 美丽的相遇
- 相遇的魔咒
- 最美的相遇
- 那次的相遇
- 淡淡的相遇
- 最初的相遇
- 注定离别的相遇
- 单淘汰赛制两队相遇算法
- 相遇
- C#学习笔记-C#中Json.Net(newtonjs)的使用
- github|fatal:unable to access|OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com 443
- Ctrl+H 浪潮Raid配置文档
- 好用的图标插件 Font Awesome
- 决策树小结
- 用线程解决简单的两车相遇
- Makefile执行过程中出错:make: *** No rule to make target ` ‘, needed by xxx. Stop.
- html5中canvas元素使用
- Eslint规则
- 微信扫码支付--模式一
- FreeRTOS学习笔记三
- 链表中倒数第k个结点
- js手机端判断滑动还是点击
- 机器学习之朴素贝叶斯(四)