初学者流泪泣问(想了一天也想不出阿,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();
}
}





...全文
51 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
  • 打赏
  • 举报
回复
其实只要用一个线程,两个线程的话,分别各自运行,你要处理好同步的问题。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧