辅以图示简述Java线程的生命周期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了辅以图示简述Java线程的生命周期相关的知识,希望对你有一定的参考价值。

//这是线程被中断,同生命周期的代码,希望帮到你啦!
class MyThread implements Runnable
@Override
public void run()
System.out.println("1、进入run()方法休眠");
try
System.out.println("2、线程休眠20秒");
Thread.sleep(20000);//这里休眠20秒
System.out.println("3、线程正常休眠完毕");
catch (InterruptedException e)
System.out.println("4、线程发生异常休眠被中断");
return;//返回方法调用处

System.out.println("5、线程正常结束run()方法体");


public class InterruptDemo

public static void main(String[] args)
MyThread mt = new MyThread();
Thread t = new Thread(mt,"线程A");
t.start();//启动线程
//========================================================
try
Thread.sleep(2000); //保证线程至少执行2秒
catch (InterruptedException e)
e.printStackTrace();

//========================================================
t.interrupt();//中断线程

参考技术A 不知道你的怪物长什么样子,用个点来模仿一下。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class BounceThread
public static void main(String[] args)
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);



class BallRunnable implements Runnable
public BallRunnable(Ball aBall, Component aComponent)
ball = aBall;
component = aComponent;


public void run()
try
for(int i=1;i<=STEPS;i++)
ball.move(component.getBounds());
component.repaint();
Thread.sleep(DELAY);

catch(InterruptedException e)
e.printStackTrace();



private Ball ball;
private Component component;
public static final int STEPS = 1000;
public static final int DELAY = 5;


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.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;



public Ellipse2D getShape()
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);




class BallPanel extends JPanel
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());


private ArrayList<Ball>balls = new ArrayList<Ball>();


class BounceFrame extends JFrame

private BallPanel panel;
public static final int STEPS = 1000;
public static final int DELAY = 3;

public BounceFrame()
setSize(450,350);
setTitle("Bounce");

panel = new BallPanel();
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", new ActionListener()
public void actionPerformed(ActionEvent event)
addBall();

);

addButton(buttonPanel, "Close", new ActionListener()
public void actionPerformed(ActionEvent event)
System.exit(0);

);
add(buttonPanel, BorderLayout.SOUTH);


public void addButton(Container c, String title, ActionListener listener)
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);


public void addBall()
Ball ball = new Ball();
panel.add(ball);

Runnable r = new BallRunnable(ball, panel);
Thread t = new Thread(r);
t.start();


以上是关于辅以图示简述Java线程的生命周期的主要内容,如果未能解决你的问题,请参考以下文章

android 怎么打印service生命周期

Java并发基础Java线程的生命周期

vue3生命周期图示

Java线程的生命周期

Vue生命周期简述

Vue生命周期简述