实验五 多线程程序设计(汽院含答案)(2)

2020-04-14 01:03

} inputText.addActionListener(this); add(button, BorderLayout.NORTH); add(wordLabel, BorderLayout.CENTER); Panel southP = new Panel(); southP.add(new Label(\输入标签所显示的汉字后回车:\)); southP.add(inputText); southP.add(scoreText); add(southP, BorderLayout.SOUTH); setBounds(100, 100, 350, 180); setVisible(true); validate(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { } if (!(giveWord.isAlive())) // giveWord调用方法isAlive() { giveWord = new WordThread(wordLabel); } try { giveWord.start();//【补充代码】// giveWord调用方法start() } catch (Exception exe) { } } else if (e.getSource() == inputText) { if (inputText.getText().equals(wordLabel.getText())) { score++; } scoreText.setText(\得分:\ + score); inputText.setText(null); } WordThread.java public class ThreadWordMainClass { public static void main(String args[]) { new ThreadFrame(); } }

2. 双线程接力

编写一个应用程序,除了主线程外,还有两个线程:first和second。first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)。

阅读并分析以下程序,将程序中的代码补充完整,编译并运行程序,查看结果。

MoveButton.java

import java.awt.*;

import java.awt.event.*;

public class MoveButton extends Frame implements Runnable,ActionListener { 【补充代码】 //用Thread类声明first,second两个线程对象 Button redButton,greenButton,startButton; int distance=10; MoveButton()

{ 【补充代码】 //创建first线程,当前窗口做为该线程的目标对象 【补充代码】 //创建first线程,当前窗口做为该线程的目标对象 redButton=new Button(); greenButton=new Button();

redButton.setBackground(Color.red);

greenButton.setBackground(Color.green); startButton=new Button(\);

startButton.addActionListener(this); setLayout(null); add(redButton);

redButton.setBounds(10,60,15,15); add(greenButton);

greenButton.setBounds(100,60,15,15); add(startButton);

startButton.setBounds(10,100,30,30); setBounds(0,0,300,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();

}

catch(Exception exp){} }

public void run() { while(true)

{ if(【补充代码】) //判断当前占有CPU资源的线程是否是first { moveComponent(redButton); try{ Thread.sleep(20); }

catch(Exception exp){} }

if(【补充代码】) //判断当前占有CPU资源的线程是否是second { moveComponent(greenButton); try{ Thread.sleep(10); }

catch(Exception exp){} } } }

public synchronized void moveComponent(Component b) {

if(Thread.currentThread()==first)

{ while(distance>100&&distance<=200) try{ wait(); }

catch(Exception exp){} distance=distance+1;

b.setLocation(distance,60); if(distance>=100)

{ b.setLocation(10,60); notifyAll(); } }

if(Thread.currentThread()==second)

{ while(distance>=10&&distance<100) try{ wait(); }

catch(Exception exp){} distance=distance+1;

b.setLocation(distance,60); if(distance>200) { distance=10;

b.setLocation(100,60); notifyAll();

} } } }

MoveButtonMainClass.java

public class MoveButtonMainClass

{ public static void main(String args[]) { new MoveButton(); } } 答案: MoveButton.java import java.awt.*; import java.awt.event.*; public class MoveButton extends Frame implements Runnable,ActionListener { /** * */ private static final long serialVersionUID = 1L; Thread first,second; //用Thread类声明first,second两个线程对象 Button redButton,greenButton,startButton; int distance=10; MoveButton() { first=new Thread(this); //创建first线程,当前窗口做为该线程的目标对象 second=new Thread(this); //创建first线程,当前窗口做为该线程的目标对象 redButton=new Button(); greenButton=new Button(); redButton.setBackground(Color.red); greenButton.setBackground(Color.green); startButton=new Button(\); startButton.addActionListener(this); setLayout(null); add(redButton); redButton.setBounds(10,60,15,15); add(greenButton); greenButton.setBounds(100,60,15,15); add(startButton); startButton.setBounds(10,100,30,30); setBounds(0,0,300,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(); } catch(Exception exp){} } public void run() { while(true) { if(Thread.currentThread()==first) //判断当前占有CPU资源的线程是否是first { moveComponent(redButton); try{ Thread.sleep(20); } catch(Exception exp){} } if(Thread.currentThread()==second) //判断当前占有CPU资源的线程是否是second { moveComponent(greenButton); try{ Thread.sleep(10); } catch(Exception exp){} } } } public synchronized void moveComponent(Component b) { if(Thread.currentThread()==first) { while(distance>100&&distance<=200) try{ wait(); } catch(Exception exp){} distance=distance+1; b.setLocation(distance,60); if(distance>=100) { b.setLocation(10,60); notifyAll(); } } if(Thread.currentThread()==second)


实验五 多线程程序设计(汽院含答案)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:stm32移植rt-thread

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: