多线程编程: Java6学习笔记56——多线程编程——线程的创建思路方法2

任何实现了Runnable接口的类都可以创建线程。这是为了弥补JAVA中单继承的特性。


同样的例子我们使用对Runnable接口的实现进行重写,三部曲:创建一个实现Runnable接口的类的实例,创建和其联系的线程,启动线程。

public class RunnableDemo1 implements Runnable {
    String name;
    int count;
    int delay;
    public static void main(String arg[]) {
        RunnableDemo1 one = new RunnableDemo1("one",1000,2);       

        //Thread类的定义是Thread(Runnable runnable),而且Thread类本身也实现了Runnable接口

        Thread threadOne = new Thread(one);//注意要用Thread类创建一个线程类
        threadOne.start();
        RunnableDemo1 two = new RunnableDemo1("two",2000,5);
        Thread threadTwo = new Thread(two);
        threadTwo.start();
    }
    RunnableDemo1(String nameString,int countStart,int seconds) {
        name = nameString;
        count = countStart;
        delay = seconds * 1000;
    }
    public void run() {
        while(true) {
            try {
                System.out.println(name + ": " + count++);
                Thread.sleep(delay);
            } catch(InterruptedException e) {
                return;
            }
        }
    }
}

弹球动画:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Canvas;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class RunnableDemo2 extends JFrame {
    public static void main(String arg[]) {
        new RunnableDemo2();
    }
    public RunnableDemo2() {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
                { System.exit(0); } } );
       
        Container container = getContentPane();
        BounceBallCanvas canvas = new BounceBallCanvas();
        container.add(canvas);
        pack();
        canvas.start();
        setLocation(250,100);
        setVisible(true);
    }
}

class BounceBallCanvas extends Canvas implements Runnable {
    private double y;
    private double x;
    private final static int diameter = 10;
    private final static double gravity = 0.3;
    private double velocity;
    private Rectangle rect;
    private int width;
    private int height;
    private Image image = null;
    private boolean running;
    private Thread looper;
    private int bounceCount;
    BounceBallCanvas() {
        rect = null;
        setSize(100,300);
        running = false;
        height = 0;
        width = 0;
    }
    public void start() {
        if(!running) {
            running = true;
            looper = new Thread(this);
            looper.start();
        }
    }
    public void stop() {
        running = false;
    }
    public void run() {
        try {
            while(running) {
                Thread.sleep(33);
                repaint();
            }
        } catch(Exception e) {
            System.out.println(e.getStackTrace());
        }
    }
    public void update(Graphics g) {
        rect = getBounds(rect);
        if((height != rect.height) || (rect.width != width)) {
            height = rect.height;
            width = rect.width;
            velocity = 0.0;
            bounceCount = 0;
            x = width / 2;
            y = 0;
            image = createImage(rect.width,rect.height);
        }
        if(velocity > 0.0) {
            if(((int)(y) + diameter) > rect.height) {
                velocity = -velocity;
                velocity *= 0.95;
                y = (double)(rect.height - diameter);
                if(++bounceCount > 20) {
                    y = 0;
                    velocity = 0.0;
                    bounceCount = 0;
                }
            }
        }
        velocity += gravity;
        y += velocity;
        paint(image.getGraphics());
        g.drawImage(image,0,0,this);
    }
    public void paint(Graphics g) {
        if(rect == null)
            return;
        g.setColor(Color.white);
        g.fillRect(0,0,rect.width,rect.height);
        g.setColor(Color.black);
        g.fillOval((int)x,(int)y,diameter,diameter);
    }
}

Tags:  linux多线程编程 delphi多线程编程 java多线程编程 多线程编程

延伸阅读

最新评论

发表评论