咨询java线程有关问题,请高手解答,谢谢
咨询java线程问题,请高手解答,多谢
最近需要模拟一个程序,试了一天没有找到好的解决方案,特来网上请教,程序大体可以理解如下:
有3辆汽车(线程)同时运行在公路上,公路上有若干个损坏缺口,汽车不能通过。现在要求3辆汽车同时启动,哪个车先遇到损坏缺口就通知其他汽车停车(线程暂停?),发现损坏缺口的汽车停下来修复公路,修复公路后,该车继续前进,并且通知其他2辆车继续前进,如果某一辆汽车再遇到下一个公路损坏缺口,继续以上处理方式。
以上程序多谢高手解答。
------解决方案--------------------
这个其实和红绿灯差不多的。复制个链接吧。
------解决方案--------------------
你在线程中用this.wait的话,把当前线程阻塞了,所以是每个线程遇到rand == 300的时候,自己把自己阻塞了,而且没法再收到notify了
这种情况,肯定要用同一个对象来wait和notify
------解决方案--------------------
------解决方案--------------------
Car.java
public class Car implements Runnable {
private int repairfactor;
private String carName;
final Lock lock;
final Condition condition;
public Car(int repairfactor, String carName, Lock lock, Condition condition) {
this.carName = carName;
this.repairfactor = repairfactor;
this.lock = lock;
this.condition = condition;
}
void process(RoadFault road) {
try {
lock.lock();
int sleep = 10000 * road.getFault() / repairfactor;
System.out.println("Sleep time : " + sleep);
Thread.sleep(sleep);
condition.signalAll();
RoadFaultManager.getRoadFaultManager().setFault(false);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
// 这里假设每一个i是一个道路长度单位
System.out.println(this.toString() + " car moving " + i);
// 模拟道路损坏障碍
最近需要模拟一个程序,试了一天没有找到好的解决方案,特来网上请教,程序大体可以理解如下:
有3辆汽车(线程)同时运行在公路上,公路上有若干个损坏缺口,汽车不能通过。现在要求3辆汽车同时启动,哪个车先遇到损坏缺口就通知其他汽车停车(线程暂停?),发现损坏缺口的汽车停下来修复公路,修复公路后,该车继续前进,并且通知其他2辆车继续前进,如果某一辆汽车再遇到下一个公路损坏缺口,继续以上处理方式。
以上程序多谢高手解答。
------解决方案--------------------
这个其实和红绿灯差不多的。复制个链接吧。
------解决方案--------------------
你在线程中用this.wait的话,把当前线程阻塞了,所以是每个线程遇到rand == 300的时候,自己把自己阻塞了,而且没法再收到notify了
这种情况,肯定要用同一个对象来wait和notify
------解决方案--------------------
import java.util.Random;
public class ThreadTest {
private boolean repairing;
public static void main(String[] args) {
ThreadTest threadTest = new ThreadTest();
new Thread(threadTest.new MyCarThread(), "a").start();
new Thread(threadTest.new MyCarThread(), "b").start();
new Thread(threadTest.new MyCarThread(), "c").start();
}
class MyCarThread implements Runnable {
@Override
public void run() {
while (true) {
while (repairing) {
System.out.println(Thread.currentThread().getName()+ "车辆正在等待道路修复。。");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "车辆运行中。。");
//发现路坏了
int i = new Random().nextInt(10);
if (i == 5) {
repairing = true;
System.out.println(Thread.currentThread().getName()+ "发现道路坏了,通知所有其它车辆先暂停行驶,正在修复,预计要5秒");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+ "发现把道路修复了,各车辆可以继续行驶。");
repairing = false;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
------解决方案--------------------
Car.java
public class Car implements Runnable {
private int repairfactor;
private String carName;
final Lock lock;
final Condition condition;
public Car(int repairfactor, String carName, Lock lock, Condition condition) {
this.carName = carName;
this.repairfactor = repairfactor;
this.lock = lock;
this.condition = condition;
}
void process(RoadFault road) {
try {
lock.lock();
int sleep = 10000 * road.getFault() / repairfactor;
System.out.println("Sleep time : " + sleep);
Thread.sleep(sleep);
condition.signalAll();
RoadFaultManager.getRoadFaultManager().setFault(false);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
// 这里假设每一个i是一个道路长度单位
System.out.println(this.toString() + " car moving " + i);
// 模拟道路损坏障碍