初学者流泪泣问(想了一天也想不出阿,555555)

fansy007 2004-04-30 02:08:58
东西方向,南北方向各一个交通灯

我想让南北向交通灯绿色延迟5秒钟跳成黄色,再延迟1秒钟,跳成红色

此时进入东西线程的交通灯,东西方向的灯由红跳成绿让绿色延迟5秒钟跳成黄色,再延迟1秒钟,跳成红色

不断循环

可是当我用sleep时(让东西方向绿灯延迟时,会跳到南北方向上运行,乱套了),谁帮我该该程序阿,谢了!
--------------------------------------------------------------------------
class TrafficLight implements Runnable
{
private String position;
private String str=new String(" ");
private boolean startdemo=true;
private final int red=0;
private final int yellow=1;
private final int green=2;

private void getout(int x)
{
switch (x)

{
case 0:
System.out.println("the trafficlight on "+position+" is red!");
break;

case 1:
System.out.println("the trafficlight on "+position+"is yellow!");
break;

case 2:
System.out.println("the trafficlight on "+position+" is green!");
break;
}
}

private void hold(int x)
{
try
{
Thread.sleep(x);
}
catch(Exception e){}
}






public TrafficLight(String x)
{
position=x;
}

public void run()
{
while(startdemo)
{
synchronized(str)
{

getout(green);
hold(3000);
getout(yellow);
hold(1000);
getout(red);

}
}
}
}



public class TrafficDemo
{
public static void main(String args[])
{
TrafficLight a=new TrafficLight("southnorth");
TrafficLight b=new TrafficLight("eastwest");
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}





...全文
25 8 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
nwsl 2004-04-30
我是这么做的。
package test.thread;

class TrafficLight implements Runnable {
private String position;
private String str = new String(" ");
private boolean startdemo = true;
private final int red = 0;
private final int yellow = 1;
private final int green = 2;

public TrafficLight(String x) {
position = x;
}


private void getout(int x) {
switch (x) {
case 0 :
System.out.println(
" " + position + " ======= red!");
break;

case 1 :
System.out.println(
" " + position + " ======= yellow!");
break;

case 2 :
System.out.println(
" " + position + " ======= green!");
break;
}
}

private void hold(int x) {
try {
Thread.sleep(x);
} catch (Exception e) {
}
}

public void run() {
while (startdemo) {
synchronized (str) {

getout(green);
hold(3000);
getout(yellow);
hold(1000);
getout(red);
hold(3000);

}
}
}
}

public class TrafficDemo {

public static void main(String args[]) {

TrafficLight a = new TrafficLight("south_north");
TrafficLight b = new TrafficLight("east_west");

Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
}
}
  • 打赏
  • 举报
回复
nwsl 2004-04-30
那红灯停多长时间?
  • 打赏
  • 举报
回复
nwsl 2004-04-30
延迟5秒吗?还是3秒钟?
  • 打赏
  • 举报
回复
fansy007 2004-04-30
to fantasycoder
搞定,谢了!

接着搞图形界面的去也,,,以后多多关照
  • 打赏
  • 举报
回复
IT源哥 2004-04-30
不要用线程,像下面这样写:
class TrafficLight //implements Runnable
{
private String position;
private String str=new String(" ");
private boolean startdemo=true;
private final int red=0;
private final int yellow=1;
private final int green=2;

private void getout(int x)
{
switch (x)

{
case 0:
System.out.println("the trafficlight on "+position+" is red!");
break;

case 1:
System.out.println("the trafficlight on "+position+"is yellow!");
break;

case 2:
System.out.println("the trafficlight on "+position+" is green!");
break;
}
}

private void hold(int x)
{
try
{
Thread.sleep(x);
}
catch(Exception e){}
}






public TrafficLight(String x)
{
position=x;
}

public void run()
{
//while(startdemo)
//{
synchronized(str)
{

getout(green);
hold(3000);
getout(yellow);
hold(1000);
getout(red);

}
//}
}
}



public class TrafficDemo
{
public static void main(String args[])
{
TrafficLight a;
int i = 0;
while(true)
{
if(0==i)
{
a=new TrafficLight("southnorth");
}
else
{
a=new TrafficLight("eastwest");
}
a.run();
i = (i+1)%2;
System.out.println("i= " + i);
}
/*
TrafficLight a=new TrafficLight("southnorth");
TrafficLight b=new TrafficLight("eastwest");
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
*/
}
}
  • 打赏
  • 举报
回复
bigcrazy 2004-04-30
用两个线程,然后用wait\notify来控制比较好。
  • 打赏
  • 举报
回复
fantasyCoder 2004-04-30
试试
public class TrafficDemo
{
public static void main(String args[])
{
TrafficLight a=new TrafficLight("southnorth");
TrafficLight b=new TrafficLight("eastwest");
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
Thread.sleep(3000);
t2.start();
}
}
  • 打赏
  • 举报
回复
寂寞沙洲 2004-04-30
其实只要用一个线程,两个线程的话,分别各自运行,你要处理好同步的问题。
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
社区管理员
  • Java SE
申请成为版主
帖子事件
创建了帖子
2004-04-30 02:08
社区公告
暂无公告