实验12 java多线程(含答案)(4)

2018-12-22 22:12

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(【代码4】) //判断当前占有CPU资源的线程是否是first

{ moveComponent(redButton); try{ Thread.sleep(20); }

catch(Exception exp){} }

if(【代码5】) //判断当前占有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(); }

? 实验后的练习:

1. 在MoveButton类中再增加一个蓝色的按钮和一个third线程,third线程负责将这个蓝色的按钮从(200,60)运动到(300,60)。


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

下一篇:湖北省融资租赁公司名录2018版118家

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

马上注册会员

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