java多线程

来源:互联网 发布:win8数据恢复软件 编辑:程序博客网 时间:2024/05/26 05:52
  1. 线程与进程最主要的区别是每个进程都拥有自己的一整套变量,而线程则是共享数据的。
  2. interrupted方法是检测当前的线程是否被中断,并且调用该方法会清除该线程的中断状态。isInterrupted用来检验是否有线程被中断。调用这个方法不会改变中断状态。
  3. 3.
import java.awt.geom.*;public 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;    public void move(Rectangle2D bounds) {        x += dx;        y += dy;        if (x < bounds.getMinX()) {            x = bounds.getMaxX();            dx = -dx;        }        if (x + XSIZE >= bounds.getMaxX()) {            x = bounds.getMaxX() - XSIZE;            dx = -dx;        }        if (y < bounds.getMinY()) {            y = bounds.getMaxY();            dy = -dy;        }        if (y + YSIZE >= bounds.getMaxY()) {            y = bounds.getMaxY() - YSIZE;            dy = -dy;        }    }    public Ellipse2D getShape() {        return new Ellipse2D.Double(x, y, XSIZE, YSIZE); //new Ellipse2D.Double(x, y, XSIZE, YSIZE);    }}
import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.util.ArrayList;import java.util.List;import javax.swing.JPanel;public class BallComponent extends JPanel {    /**     *      */    private static final long serialVersionUID = 2629028133593727544L;    private static final int DEFAULT_WIDTH = 450;    private static final int DEFAULT_HIGHT = 350;    private List<Ball> balls = new ArrayList<>();    public BallComponent() {        this.setPreferredSize(getPreferredSize());    }    public void add(Ball b) {        balls.add(b);    }    @Override    protected void paintComponent(Graphics g) {        // TODO Auto-generated method stub        super.paintComponent(g);        Graphics2D g2 = (Graphics2D) g;        for (Ball b : balls) {            g2.fill(b.getShape());        }    }    public Dimension getPreferredSize() {        return new Dimension(DEFAULT_WIDTH, DEFAULT_HIGHT);    }}

实现Runnable方法。

import java.awt.Component;public class BallRundable implements Runnable {    private Ball ball;    private Component component;    public static final int STEPS = 1000;    public static final int DELAY =3;    public BallRundable(Ball ball, Component component) {        this.ball = ball;        this.component = component;    }    @Override    public void run() {        try {            for (int i = 1; i <= STEPS; i++) {                ball.move(component.getBounds());                component.repaint();                Thread.sleep(DELAY);            }        } catch (Exception e) {            e.printStackTrace();        }    }}
import java.awt.EventQueue;import javax.swing.JFrame;public class BonuceThread {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable(){            @Override            public void run() {                JFrame frame = new BounceFrame();                frame.setTitle("BonuceThread");                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                frame.setVisible(true);            }        });    }}
import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class BounceFrame extends JFrame {    /**     *      */    private static final long serialVersionUID = -1202234496448237858L;    private BallComponent comp;    public BounceFrame() {        this.setTitle("Bounce");        comp = new BallComponent();        add(comp, BorderLayout.CENTER);        JPanel buttonPanel = new JPanel();        addButton(buttonPanel, "start", new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                addBall();            }        });        addButton(buttonPanel, "Close", new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                System.exit(0);            }        });        add(buttonPanel, BorderLayout.SOUTH);        pack();    }    public void addButton(Container c, String title, ActionListener listener) {        JButton button = new JButton(title);        c.add(button);        button.addActionListener(listener);    }    public void addBall() {        try {            Ball ball = new Ball();            comp.add(ball);            //多线程调用。            Runnable r = new BallRundable(ball, comp);            Thread t = new Thread(r);            t.start();        } catch (Exception e) {            e.printStackTrace();        }    }}
0 0
原创粉丝点击