多线程4(龟兔赛跑-休眠线程)
1.题目
在龟兔赛跑中,领先的兔子因为通宵写博客,中途太累睡着了,跑输了乌龟。这个故事说明了兔子是爱学习的同学。咳咳,通宵是不可取的,大家别学。
实现:使用线程休眠模拟龟兔赛跑比赛
2.解题思路
创建一个类:RaceFrame,继承了JFrame。用来界面显示两个文本区域,用来输出乌龟和兔子的比赛记录,比赛开始按钮用来开始比赛。
编写内部类:Rabbit,该类实现了Runnable接口,在run()方法中,让兔子休眠了时间。
Thread是程序中的执行线程。JAVA虚拟机允许应用程序并发地运行多个执行线程。
sleep()方法是Thread类的一个静态方法。
3.代码详解
package com.xiaoxuzhu;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import java.awt.Font;
public class RaceFrame extends JFrame {
private static final long serialVersionUID = 4941729012450153307L;
private JPanel contentPane;
private JTextArea rabbitTextArea;
private JTextArea tortoiseTextArea;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RaceFrame frame = new RaceFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public RaceFrame() {
setTitle("休眠当前线程");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel buttonPanel = new JPanel();
contentPane.add(buttonPanel, BorderLayout.SOUTH);
JButton button = new JButton("比赛开始");
button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
do_button_actionPerformed(arg0);
}
});
buttonPanel.add(button);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(1, 2, 5, 5));
JPanel rabbitPanel = new JPanel();
panel.add(rabbitPanel);
rabbitPanel.setLayout(new BorderLayout(0, 0));
JLabel rabbitLabel = new JLabel("兔子的比赛记录");
rabbitLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
rabbitLabel.setHorizontalAlignment(SwingConstants.CENTER);
rabbitPanel.add(rabbitLabel, BorderLayout.NORTH);
JScrollPane rabbitScrollPane = new JScrollPane();
rabbitPanel.add(rabbitScrollPane, BorderLayout.CENTER);
rabbitTextArea = new JTextArea();
rabbitTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
rabbitScrollPane.setViewportView(rabbitTextArea);
JPanel tortoisePanel = new JPanel();
panel.add(tortoisePanel);
tortoisePanel.setLayout(new BorderLayout(0, 0));
JLabel tortoiseLabel = new JLabel("乌龟的比赛记录");
tortoiseLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
tortoiseLabel.setHorizontalAlignment(SwingConstants.CENTER);
tortoisePanel.add(tortoiseLabel, BorderLayout.NORTH);
JScrollPane tortoiseScrollPane = new JScrollPane();
tortoisePanel.add(tortoiseScrollPane, BorderLayout.CENTER);
tortoiseTextArea = new JTextArea();
tortoiseTextArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
tortoiseScrollPane.setViewportView(tortoiseTextArea);
}
protected void do_button_actionPerformed(ActionEvent arg0) {
Runnable run1 = new Rabbit();
Runnable run2 = new Tortoise();
Thread rabbit = new Thread(run1);
Thread tortoise = new Thread(run2);
rabbit.start();
tortoise.start();
}
private class Rabbit implements Runnable {
@Override
public void run() {
for (int i = 1; i < 11; i++) {// 循环10次模拟赛跑的过程
String text = rabbitTextArea.getText();// 获得文本域中的信息
try {
Thread.sleep(1);// 线程休眠0.001秒,模拟兔子在跑步
} catch (InterruptedException e) {
e.printStackTrace();
}
rabbitTextArea.setText(text + "兔子跑了" + i + "0米\n");// 显示兔子的跑步距离
if (i == 9) {
rabbitTextArea.setText(text + "兔子在睡觉\n");// 当跑了90米时开始睡觉
try {
Thread.sleep(10000);// 线程休眠10秒,模拟兔子在睡觉
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (i == 10) {
try {
Thread.sleep(1);// 线程休眠0.001秒,模拟兔子在跑步
} catch (InterruptedException e) {
e.printStackTrace();
}
rabbitTextArea.setText(text + "兔子到达终点\n");// 显示兔子到达了终点
}
}
}
}
private class Tortoise implements Runnable {
@Override
public void run() {
for (int i = 1; i < 11; i++) {
String text = tortoiseTextArea.getText();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
tortoiseTextArea.setText(text + "乌龟跑了" + i + "0米\n");
if (i == 10) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
tortoiseTextArea.setText(text + "乌龟到达终点\n");
}
}
}
}
}
到此这篇关于Java多线程解决龟兔赛跑问题详解的文章就介绍到这了,更多相关Java多线程内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!