JAVA多线程编程实现龟兔赛跑

Java的多线程建立在Thread类和Runnable接口上,可以通过继承Thread类和实现Runnable接口创建一个线程。

实现Runnable接口的类需要实例化run()方法。

实例化Thread类可以创建一个新的线程:我们可以创建一个类扩展Thread类,并且代码覆盖run()方法,与此同时用new创建一个线程的对象,当然还要调用start()方法来启动线程。

isAlive()方法判断一个线程是否结束。

下面简单用线程编写龟兔赛跑。

//跑道

import java.awt.*;

import java.awt.event.*;

public class Chang extends Frame implements ActionListener 

{

 

private Button quit = new Button("Quit");

private Button start = new Button("比赛");

//private Button halt = new Button("睡觉");

private Wg wg = null;

private Tz tz = null;

//Image wgg = Toolkit.getDefaultToolkit().getImage("wgg.GIF");

public Chang()//跑道

{

super("龟兔赛跑");

this.setLayout(new FlowLayout());//按钮浮于上方

add(quit);    quit.addActionListener(this);

add(start);   start.addActionListener(this);

//add(halt);  halt.addActionListener(this);

validate();

setSize(500,500);

setVisible(true);

int x = 100;//产生乌龟

int y = 0;

int x2 = 200;//产生兔子

int y2 = 0;

this.wg = new Wg(this,x,y);

this.tz = new Tz(this,x2,y2);

this.repaint();

}

public void paint(Graphics g)

{

//if(wg[0] != null)

wg.draw(g);

tz.draw(g);

g.fillRect(0, 450, 500, 10);

}

 

public void actionPerformed(ActionEvent arg)

{

if(arg.getSource() == start)

{

wg.start();

tz.start();

}

if(arg.getSource() == quit)

System.exit(0);

}

public static void main(String args[])

{

Chang  table = new Chang();

}

 

}

 //兔子

import java.awt.Color;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.Graphics;

 

public class Tz extends Thread

{

private int xdir = 0;

private int ydir = 10;//单位时间跑3

private boolean running = false;

private Chang chang = null;//操场

 int x,y,time = 0;

 Image tzz = Toolkit.getDefaultToolkit().getImage("tzz.jpg");

 Image shu = Toolkit.getDefaultToolkit().getImage("shu.jpg");

 public Tz(Chang _chang,int _x,int _y)

{

this.chang = _chang;

this.x = _x;

this.y = _y;

start();

}

 public void start ()

{

running = true;

super.start();

}

 public void halt(  )//中止

{

running = false;

}

public void run()

{

while ( running)

{

move();

try {

sleep(200);

} catch (InterruptedException e) {  

e.printStackTrace();

}

chang.repaint();      

}

}

public void move( )

{

if(y <= 450)

 if(y == 200)//兔子睡觉

 {

 try {

sleep(9000);

} catch (InterruptedException e) {  

e.printStackTrace();

}

 }

y = y + 10;

}

 

}

public int getx() {

return x;

}

public int gety() {

return y;

}

 

void draw(Graphics g)

{

g.setColor(Color.RED);

g.drawImage(tzz, x, y, 30, 40, chang);

g.drawImage(shu,240,200,30,40,chang);

}

 

}

//乌龟

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

 

public class Wg extends Thread                                                                                                                            

{

 int xdir = 0;

 int ydir = 3;//单位时间跑3

private boolean running = false;

private Chang chang = null;//操场

 int x,y;

Image wgg = Toolkit.getDefaultToolkit().getImage("wgg.jpg");

public Wg(Chang _chang,int _x,int _y)

{

this.chang = _chang;

this.x = _x;

this.y = _y;

start();

}

public void start ()

{

//Thread w = new Thread(this,"wgg");

running = true;

super.start();

}

public void halt()//中止

{

running = false;

}

public void run()

{

while(running)

{

move();

try{

sleep(100);

}catch(InterruptedException ie){

System.err.println("Thread interrupted");

}    

chang.repaint();             

}

}

public void move()

{

if(y <= chang.getSize().width-50)

{

x += xdir;

y += ydir;

}

}

public void draw(Graphics g)

{

g.drawImage(wgg, x, y, 30, 40, chang);

}

}

本人仍处于学习阶段,所以有什么不对的,请大家多多指教。

你可能感兴趣的:(java,多线程,线程)