📄 threadpriority.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ThreadPriority extends JFrame implements ActionListener
{
private PaintPanel paintPanel;
private JPanel buttonPanel;
private JButton startButton, stopButton;
public ThreadPriority()
{
super("线程优先级");
setSize(400,300);
//获取内容面板
Container container = getContentPane();
//创建绘图面板paintPanel
paintPanel = new PaintPanel();
container.add(paintPanel, BorderLayout.CENTER);
//创建存放按钮的面板buttonPanel
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.YELLOW);
startButton = new JButton("开始");
stopButton = new JButton("暂停");
//设置按钮背景颜色
startButton.setBackground(Color.CYAN);
stopButton.setBackground(Color.CYAN);
//设置字体
startButton.setFont(new Font("Serif", Font.PLAIN, 14));
stopButton.setFont(new Font("Serif", Font.PLAIN, 14));
//注册监听器
startButton.addActionListener(this);
stopButton.addActionListener(this);
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
container.add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) //JButton事件处理
{
if(event.getSource() == startButton)
{
paintPanel.start();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
else if(event.getActionCommand().equals("暂停"))
{
paintPanel.suspend();
stopButton.setText("恢复");
}
else if(event.getActionCommand().equals("恢复"))
{
paintPanel.resume();
stopButton.setText("暂停");
}
}
public static void main(String[] args)
{
ThreadPriority application = new ThreadPriority();
}
class PaintPanel extends JPanel implements Runnable
{
private Thread blueBall, greenBall;
private int x1 = 10;
private int x2 = 10;
public PaintPanel()
{
super();
setBackground(Color.WHITE);
//创建两个线程
blueBall = new Thread(this);
greenBall = new Thread(this);
//设置线程的优先级
blueBall.setPriority(Thread.MIN_PRIORITY);
greenBall.setPriority(Thread.MAX_PRIORITY);
}
public void paintComponent(Graphics g)
{
Graphics2D comp2D = (Graphics2D)g;
comp2D.setColor(getBackground());
//清除画板
comp2D.fillRect(0,0,getSize().width,getSize().height);
//绘制蓝色球
comp2D.setColor(Color.BLUE);
comp2D.drawOval(x1, 30, 50, 50);
//绘制绿色球
comp2D.setColor(Color.GREEN);
comp2D.drawOval(x2, 100, 50, 50);
}
public void run()
{
while(true){
//如果当前线程为blueBall则执行以下代码
if(Thread.currentThread() == blueBall)
{
x1 += 5;
if(x1 >= 400)
x1 = 10;
//线程blueBall休眠50毫秒
pause(50);
//如果当前线程为greenBall则执行以下代码
}
else if(Thread.currentThread() == greenBall)
{
x2 += 5;
if(x2 >= 400)
x2 = 10;
//线程greenBall休眠50毫秒
pause(50);
}
repaint();
}
}
public void start() //用于启动两个线程
{
greenBall.start();
blueBall.start();
}
public void resume() //用于将两个挂起的线程重新加入就绪队列
{
blueBall.resume();
greenBall.resume();
}
public void suspend() //用于将两个活动的线程挂起
{
blueBall.suspend();
greenBall.suspend();
}
private void pause(int time) //用于时间延时的方法
{
try
{
Thread.sleep(time);
} catch (InterruptedException e) { }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -