实验目的
1. 掌握Runnable接口实现多线程的方法
2. 掌握Thread类实现多线程的用法
3. 掌握Java语言中多线程编程的基本方法
- 线程接力(45分)
要求:编写一个应用程序,除了主线程外,还有三个线程:first、second和third。
first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);
second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60);
third线程负责模拟一个蓝色的按钮从坐标(200,60)运动到(300,60)。
程序运行界面如下图所示:
请参看实验指导书,阅读分析程序,将程序中的代码补充完整,编译并运行程序,查看结果。
实现代码
package T7;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Relay extends Frame implements Runnable,ActionListener
{
Thread first,second,third;
Button redButton,greenButton,blueButton,startButton;
int distance=10;
Relay()
{
setTitle("线程接力");
JLabel jl=new JLabel("计Z2004 202062157 卜袁杰");
jl.setBounds(60,90,200,50);
add(jl);
first=new Thread(this);
second=new Thread(this);
third=new Thread(this);
redButton=new Button();
greenButton=new Button();
blueButton=new Button();
redButton.setBackground(Color.red);
greenButton.setBackground(Color.green);
blueButton.setBackground(Color.blue);
startButton=new Button("start");
startButton.addActionListener(this);
setLayout(null);
add(redButton);
redButton.setBounds(10,60,15,15);
add(greenButton);
greenButton.setBounds(100,60,15,15);
add(blueButton);
blueButton.setBounds(200,60,15,15);
add(startButton);
startButton.setBounds(10,100,30,30);
setBounds(0,0,350,200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
try{
first.start();
second.start();
third.start();
}
catch(Exception exp){}
}
public void run()
{ while(true)
{ if(Thread.currentThread()==first)
{ moveComponent(redButton);
try{ Thread.sleep(10);
}
catch(Exception exp){}
}
if(Thread.currentThread()==second)
{ moveComponent(greenButton);
try{ Thread.sleep(10);
}
catch(Exception exp){}
}
if(Thread.currentThread()==third)
{ moveComponent(blueButton);
try{ Thread.sleep(10);
}
catch(Exception exp){}
}
}
}
public synchronized void moveComponent(Component b)
{
if(Thread.currentThread()==first)
{ while(distance>100 && distance<=300)
try{ wait();
}
catch(Exception exp){}
distance+=1;
b.setLocation(distance,60);
if(distance>=100)
{ b.setLocation(10,60);
notifyAll();
}
}
if(Thread.currentThread()==second)
{
while(distance<100 || distance>200)
try{ wait();
}
catch(Exception exp){}
distance+=1;
b.setLocation(distance,60);
if(distance>200)
{
b.setLocation(100,60);
notifyAll();
}
}
if(Thread.currentThread()==third)
{
while(distance<200)
try{ wait();
}
catch(Exception exp){}
distance+=1;
b.setLocation(distance,60);
if(distance>300)
{
distance=10;
b.setLocation(200,60);
notifyAll();
}
}
}
public static void main(String args[]) {
new Relay();
}
}
- 线程的控制(45分)
要求:编写一个程序,动画显示文本域中的字符串。在窗体的南面添加三个按钮,为程序添加线程控制功能。
点击开始按钮(startBtn),线程开始启动,文字逐个显示,并且将按钮状态改变为禁用(因为线程不能重复启动)
点击暂停按钮(pauseBtn),线程暂停,文字显示停止
点击恢复按钮(resumeBtn),线程恢复运行,文字继续显示
当线程执行完毕后,恢复开始按钮的状态为可用。
程序运行界面如下图所示:
实现代码
package T7;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Control extends Thread{
static String text=null;
static JTextArea jTextArea;
static JButton jButton1;
public static void main(String[] args) {
text="如果你想博学些,就请勤奋些;如果你想尊严些,就请自强些;如果你想荣耀些," +
"就请努力些;如果你想健康些,就请环保些;如果你想长寿些,就请宽心些。";
JFrame jFrame=new JFrame("线程的控制");
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setBounds(100,100,400,260);
JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel jLabel=new JLabel("多线程简介:计Z2004 202062157 卜袁杰");
jPanel.add(jLabel);
jFrame.add(jPanel);
JPanel jPanel2=new JPanel();
jTextArea=new JTextArea(10,35);
jTextArea.setLineWrap(true);
jPanel2.add(jTextArea);
JPanel jPanel3=new JPanel();
jButton1=new JButton("开始");
Control control=new Control();
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jButton1.setEnabled(false);
control.start();
}
});
JButton jButton2=new JButton("暂停");
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
control.suspend();
}
});
JButton jButton3=new JButton("恢复");
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
control.resume();
}
});
jPanel3.add(jButton1);jPanel3.add(jButton2);jPanel3.add(jButton3);
jFrame.add(jPanel,BorderLayout.NORTH);
jFrame.add(jPanel2,BorderLayout.CENTER);
jFrame.add(jPanel3,BorderLayout.SOUTH);
}
@Override
public void run() {
for (int i=0;i<text.length();i++){
jTextArea.append(text.substring(i,i+1));
if (i+1==text.length()){
jButton1.setEnabled(true);
}
try {
sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}