java线程初体验

概述

        本文基于“Java核心技术卷I -- 第14章 并发”实验程序-弹跳小球。如果运行程序球就会自如地来回弹跳,但是在单线程版本这个程序完全控制了整个应用程序,你甚至都不能把它占用的资源释放,当你关闭这个程序的时候,你不得不等待小球运动完毕。在球弹跳结束前你无法再与程序进行交互,更无法让多个小球同时运行,这意味着在单线程的世界里你启动程序等待结果,而无法控制程序运行的过程。这种无法交互的程序几乎没有意义。在多线程版本中使用线程分割不同任务,即将任务拆分成子任务并放在独立的线程中,多个小球可以同时运行,不仅大大提高跳球的运动能力,还可以提高程序的交互性,更可给予重负载任务更多线程资源的支持。每个球都在自己的线程中,用户界面操作事件线程也独立运行,由于每个线程都有机会运行,所以在多线程版本中,在球弹跳期间,点击关闭按钮,事件调度线程将会注意到该事件并处理“关闭”动作,事件得以及时响应。多任务拆分多线程运行非常重要!

版本1-单线程版

程序模拟了一个在窗口中反弹的球,主要分为以下几个类:

  1. Ball 类:表示一个球对象,包含球的位置、速度等属性,以及球的移动和绘制方法。

  2. Bounce 类:程序的主入口,包含 main 方法,用于启动图形界面。

  3. BounceFrame 类:表示主窗口,包含一个球组件和几个按钮,用于控制球的运动。

  4. BallComponent 类:表示球的组件,负责绘制球,并管理球的添加和移动。


详细类说明

1. Ball 类
  • 属性

    • x 和 y:球的当前位置。

    • dx 和 dy:球的水平和垂直速度。

    • XSIZE 和 YSIZE:球的大小。

  • 方法

    • move:根据当前速度移动球的位置,如果碰到边界则反弹。

    • getShape:获取球的形状,用于绘制。

2. Bounce 类
  • 方法

    • main:程序的入口,创建并显示主窗口。

3. BounceFrame 类
  • 属性

    • comp:一个球组件,用于显示球的运动。

    • STEPS 和 DELAY:球的运动步数和每步的延迟时间。

  • 方法

    • addButton:向窗口添加按钮,并设置按钮的事件监听器。

    • addBall:向组件中添加一个球,并控制球的运动。

4. BallComponent 类
  • 属性

    • balls:一个球的列表,用于管理多个球对象。

    • DEFAULT_WIDTH 和 DEFAULT_HEIGHT:组件的默认大小。

  • 方法

    • add:向组件中添加一个球。

    • paintComponent:重写 paintComponent 方法,用于绘制球。

    • getPreferredSize:获取组件的首选大小。


程序运行流程

  1. 通过 Bounce 类的 main 方法启动程序。

  2. 创建一个 BounceFrame 对象并显示出来。

  3. 用户可以通过窗口中的按钮来添加球。

  4. 球在窗口中移动,并在碰到边界时反弹。

  5. 用户可以观察多个球在窗口中反弹的动画效果。


程序清单

package bounce;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
 
/*
 * A ball that moves and bounces off the edges of a rectangle
 * @version 1.33 2007-05-17
 * @author Cay Horstmann
 */
 class Ball
{
    private static final int XSIZE = 15;
    private static final int YSIZE = 15;
    private double x = 0;
    private double y = 0;
    private double dx = 1;
    private double dy = 1;
 
    /*
     * Moves the ball to the next position, reversing direction if it hits one of the edges
     */
    public void move(Rectangle2D bounds)
    {
        x += dx;
        y += dy;
        if (x < bounds.getMinX())
        {
            x = bounds.getMinX();
            dx = -dx;
        }
        if (x + XSIZE >= bounds.getMaxX())
        {
            x = bounds.getMaxX() - XSIZE;
            dx = -dx;
        }
        if (y < bounds.getMinY())
        {
            y = bounds.getMinY();
            dy = -dy;
        }
        if (y + YSIZE >= bounds.getMaxY())
        {
            y = bounds.getMaxY() - YSIZE;
            dy = -dy;
        }
    }
 
    /*
     * Gets the shape of the ball at its current position.
     */
    public Ellipse2D getShape()
    {
        return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
    }
}
 
 
 
/*
 * Shows an animated bouncing ball.
 * @version 1.34 2015-06-21
 * @author Cay Horstmann
 */
public class Bounce
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(() -> {
            JFrame frame = new BounceFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}
 
/*
 * The frame with ball component and buttons.
 */
class BounceFrame extends JFrame
{
    private BallComponent comp;
    public static final int STEPS = 1000;
    public static final int DELAY = 3;
 
    /*
     * Constructs the frame with the component for showing the bouncing ball and
     * Start and Close buttons
     */
    public BounceFrame()
    {
        setTitle("Bounce");
        comp = new BallComponent();
        add(comp, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        addButton(buttonPanel, "Start", event -> addBall());
        addButton(buttonPanel, "Close", event -> System.exit(0));
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
    }
 
    /*
     * Adds a button to a container.
     * @param c the container
     * @param title the button title
     * @param listener the action listener for the button
     */
    public void addButton(Container c, String title, ActionListener listener)
    {
        JButton button = new JButton(title);
        c.add(button);
        button.addActionListener(listener);
    }
 
    /*
     * Adds a bouncing ball to the panel and makes it bounce 1,000 times.
     */
    public void addBall()
    {
        try
        {
            Ball ball = new Ball();
            comp.add(ball);
            for (int i = 1; i <= STEPS; i++)
            {
                ball.move(comp.getBounds());
                comp.paint(comp.getGraphics());
                Thread.sleep(DELAY);
            }
        }
        catch (InterruptedException e)
        {
        }
    }
}
 
 
 
 
/*
 * The component that draws the balls.
 * @version 1.34 2012-01-26
 * @author Cay Horstmann
 */
 class BallComponent extends JPanel
{
    private static final int DEFAULT_WIDTH = 450;
    private static final int DEFAULT_HEIGHT = 350;
    private java.util.List<Ball> balls = new ArrayList<>();
 
    /*
     * Add a ball to the component.
     * @param b the ball to add
     */
    public void add(Ball b)
    {
        balls.add(b);
    }
 
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for (Ball b : balls)
        {
            g2.fill(b.getShape());
        }
    }
 
    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}

版本2-多线程版

任务拆分

拆分任务,将小球的移动重绘从事件调度线程中拆分处理,放在独立的线程中,使之互不关联。如下:

Runnable r = () -> {
    try
    {
        for (int i = 1; i <= STEPS; i++)
        {
            ball.move(comp.getBounds());
            comp.repaint();
            Thread.sleep(DELAY);
        }
    }
    catch (InterruptedException e)
    {
    }
};
Thread t = new Thread(r);
t.start();

程序清单

package bounceThread;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.*;

/**
 * Shows animated bouncing balls.
 * @version 1.34 2015-06-21
 * @author Cay Horstmann
 */
public class BounceThread
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(() -> {
            JFrame frame = new BounceFrame();
            frame.setTitle("BounceFrame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

/**
 * The frame with panel and buttons.
 */
class BounceFrame extends JFrame
{
    private BallComponent comp;
    public static final int STEPS = 1000;
    public static final int DELAY = 5;

    /**
     * Constructs the frame with the component for showing the bouncing ball and
     * Start and Close buttons
     */
    public BounceFrame()
    {
        comp = new BallComponent();
        add(comp, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        addButton(buttonPanel, "Start", event -> addBall());
        addButton(buttonPanel, "Close", event -> System.exit(0));
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
    }

    /**
     * Adds a button to a container.
     * @param c the container
     * @param title the button title
     * @param listener the action listener for the button
     */
    public void addButton(Container c, String title, ActionListener listener)
    {
        JButton button = new JButton(title);
        c.add(button);
        button.addActionListener(listener);
    }

    /**
     * Adds a bouncing ball to the canvas and starts a thread to make it bounce
     */
    public void addBall()
    {
        Ball ball = new Ball();
        comp.add(ball);
        Runnable r = () -> {
            try
            {
                for (int i = 1; i <= STEPS; i++)
                {
                    ball.move(comp.getBounds());
                    comp.repaint();
                    Thread.sleep(DELAY);
                }
            }
            catch (InterruptedException e)
            {
            }
        };
        Thread t = new Thread(r);
        t.start();
    }
}



/*
 * The component that draws the balls.
 * @version 1.34 2012-01-26
 * @author Cay Horstmann
 */
class BallComponent extends JPanel
{
    private static final int DEFAULT_WIDTH = 450;
    private static final int DEFAULT_HEIGHT = 350;
    private java.util.List<Ball> balls = new ArrayList<>();

    /*
     * Add a ball to the component.
     * @param b the ball to add
     */
    public void add(Ball b)
    {
        balls.add(b);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for (Ball b : balls)
        {
            g2.fill(b.getShape());
        }
    }

    public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}



/*
 * A ball that moves and bounces off the edges of a rectangle
 * @version 1.33 2007-05-17
 * @author Cay Horstmann
 */
class Ball
{
    private static final int XSIZE = 15;
    private static final int YSIZE = 15;
    private double x = 0;
    private double y = 0;
    private double dx = 1;
    private double dy = 1;

    /*
     * Moves the ball to the next position, reversing direction if it hits one of the edges
     */
    public void move(Rectangle2D bounds)
    {
        x += dx;
        y += dy;
        if (x < bounds.getMinX())
        {
            x = bounds.getMinX();
            dx = -dx;
        }
        if (x + XSIZE >= bounds.getMaxX())
        {
            x = bounds.getMaxX() - XSIZE;
            dx = -dx;
        }
        if (y < bounds.getMinY())
        {
            y = bounds.getMinY();
            dy = -dy;
        }
        if (y + YSIZE >= bounds.getMaxY())
        {
            y = bounds.getMaxY() - YSIZE;
            dy = -dy;
        }
    }

    /*
     * Gets the shape of the ball at its current position.
     */
    public Ellipse2D getShape()
    {
        return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值