「java多线程红绿灯」java多线程红绿灯编程题
今天给各位分享java多线程红绿灯的知识,其中也会对java多线程红绿灯编程题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
请用Java多线程模拟自动路灯控制系统:
/*小弟,会的只有这些,基本的功能吧!我信相还有很多地方还要改进,但我的能力只能去这里了,要等大侠位完善它了!*/
class A implements Runnable { //A为北
private Crossing crossing = null ;
public A(Crossing crossing){
this.crossing = crossing ;
}
public void run(){
int j = 0 ;
for(int i = 0 ; i 5 ; i++){
j++ ;
this.crossing.ACset("车辆从A到B,第 " + j + "号车辆") ;
}
}
}
class B implements Runnable { //B为南
private Crossing crossing = null ;
public B(Crossing crossing){
this.crossing = crossing ;
}
public void run(){
for(int i = 0 ; i 5 ; i++){
this.crossing.BDget() ;
}
}
}
class C implements Runnable { //C为西
private Crossing crossing = null ;
public C(Crossing crossing){
this.crossing = crossing ;
}
public void run(){
int j = 0 ;
for(int i = 0 ; i 5 ; i++){
j++ ;
this.crossing.ACset("车辆从C到D,第 " + j + "号车辆") ;
}
}
}
class D implements Runnable { //D为东
private Crossing crossing = null ;
public D(Crossing crossing){
this.crossing = crossing ;
}
public void run(){
for(int i = 0 ; i 5 ; i++){
this.crossing.BDget() ;
}
}
}
public class Crossing { //为路口
private String car = "车辆从A到B" ;
private boolean flag = false ; //默认是false
/*
*1、flag = true,表示绿灯,可以A往B;
*2、flag = false,表示红灯,可以C往D;
**/
public void setCar(String car){
this.car = car ;
}
public String getCar(){
return car ;
}
public synchronized void ACset(String car){ //A 和 C去的去的方向
if(flag == false){
try {
Thread.sleep(20000) ; //红绿灯的时间
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
this.setCar(car) ;
}
this.flag = true ;
if(flag){
this.setCar(car) ;
} else {
try {
Thread.sleep(20000) ; //红绿灯的时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.flag = false ;
}
public synchronized void BDget(){ //B 和 D得到的车辆
if(flag){
System.out.println(this.getCar()) ;
}
this.flag = false ;
if(flag = false){
System.out.println(this.getCar()) ;
}
this.flag = true ;
}
public static void main(String[] args) {
Crossing crossing = new Crossing() ;
A a = new A(crossing) ;
B b = new B(crossing) ;
C c = new C(crossing) ;
D d = new D(crossing) ;
new Thread(a).start() ;
new Thread(b).start() ;
new Thread(c).start() ;
new Thread(d).start() ;
}
}
使用java多线程和图形编程,完成红绿灯模拟软件
boolean flag = true;
while(flag){
//绿灯显示
//sleep(3000);
//黄灯显示
sleep(3000);
//红灯显示
sleep(3000);
}
如果点击结束,退出while
请问如何用JAVA多线程写一个红黄绿依次亮的交通灯程序?
import java.awt.*;
import java.awt.event.*;
public class JiaoTong extends Frame {
boolean redStatus=false,greenStatus=false,yellowStatus=false;
int j=0;
public void paint(Graphics g) {
Color c=g.getColor();
if(redStatus==true)
{j++;
g.setColor(Color.RED);
g.fillOval(100,50, 50,50);
g.drawString("红灯亮了"+j+"秒", 100, 120);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100,50, 50,50);
}
if(yellowStatus==true){
j++;
g.setColor(Color.YELLOW);
g.fillOval(100, 150, 50, 50);
g.drawString("黄灯注意啦"+j+"秒", 100, 220);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 150, 50, 50);
}
if(greenStatus==true){
j++;
g.setColor(Color.GREEN);
g.fillOval(100, 250, 50, 50);
g.drawString("绿灯行"+j+"秒", 100, 320);
}
else
{
g.setColor(Color.BLACK);
g.fillOval(100, 250, 50, 50);
}
g.setColor(c);
}
public void Lauch() {
setTitle("交通灯指示");
setSize(300, 400);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
JiaoTong a=new JiaoTong();
a.Lauch();
}
public class PaintThread implements Runnable
{
public void run() {
for(int i=0;i80;i++){
switch (i) {
case 0:
j=0;
redStatus=true;
greenStatus=false;
yellowStatus=false;
break;
case 40:
j=0;
redStatus=false;
greenStatus=false;
yellowStatus=true;
break;
case 50:
j=0;
redStatus=false;
greenStatus=true;
yellowStatus=false;
break;
default:
break;
}
repaint();
if(i==79)
i=-1;
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
java 线程实现一个红绿灯问题
关键是启动一个线程控制颜色。代码如下。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Signal extends Applet {
int width = 200, height = 240;
int w = 50, h = 50;
int x = (width - w) / 2, y1 = (height - h * 3) / 3, y2 = y1 + h, y3 = y2 + h;
Color c = Color.RED;
@Override
public void init() {
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (c == Color.RED) {
c = Color.YELLOW;
} else if (c == Color.YELLOW) {
c = Color.GREEN;
} else if (c == Color.GREEN) {
c = Color.RED;
}
repaint();
}
}, 5, 5, TimeUnit.SECONDS);
}
@Override
public void paint(Graphics g) {
setBackground(Color.white);
// all gray
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y1, w, h);
g.fillOval(x, y2, w, h);
g.fillOval(x, y3, w, h);
if (c == Color.RED) {
g.setColor(Color.RED);
g.fillOval(x, y1, w, h);
} else if (c == Color.YELLOW) {
g.setColor(Color.YELLOW);
g.fillOval(x, y2, w, h);
} else if (c == Color.GREEN) {
g.setColor(Color.GREEN);
g.fillOval(x, y3, w, h);
}
}
}
java多线程红绿灯的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java多线程红绿灯编程题、java多线程红绿灯的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。