Java多线程实验报告汇总.docx
《Java多线程实验报告汇总.docx》由会员分享,可在线阅读,更多相关《Java多线程实验报告汇总.docx(14页珍藏版)》请在冰豆网上搜索。
Java多线程实验报告汇总
西安邮电大学
(计算机学院)
课内实验报告
实验名称:
多线程
专业名称:
计算机科学与技术
班级:
计科1405班
学生姓名:
高宏伟
学号:
04141152
指导教师:
刘霞林
实验日期:
2016.12.08
一、实验目的
1.线程的概念、线程的生命周期。
2.多线程的编程:
继承Thread类与使用Runnable接口。
3.使用多线程机制实现动画。
二、实验要求
1.掌握利用JAVA语言编写多线程程序的方法。
2.掌握线程的调度方法。
3.掌握多线程环境中GUI程序的编写方法。
三、实验内容
(一)汉字打字练习。
✧实验要求:
编写一个Java应用程序,在主线程中再创建一个Frame类型的窗口,在该窗口中再创建1个线程giveWord。
线程giveWord每隔2秒钟给出一个汉字,用户使用一种汉字输入法将该汉字输入到文本框中。
✧程序模板:
WordThread.java
importjava.awt.*;
publicclassWordThreadextendsThread
{charword;
intk=19968;
Labelcom;
WordThread(Labelcom)
{=com;
}
publicvoidrun()
{k=19968;
while(true)
{
word=(char)k;
com.setText(""+word);
try{【代码1】//调用sleep方法使得线程中断6000豪秒
}
catch(InterruptedExceptione){}
k++;
if(k>=29968)k=19968;
}
}
}
ThreadFrame.java
importjava.awt.*;
importjava.awt.event.*;
publicclassThreadFrameextendsFrameimplementsActionListener
{
LabelwordLabel;
Buttonbutton;
TextFieldinputText,scoreText;
【代码2】//用WordThread声明一个giveWord对象
intscore=0;
ThreadFrame()
{wordLabel=newLabel("",Label.CENTER);
wordLabel.setFont(newFont("",Font.BOLD,72));
button=newButton("开始");
inputText=newTextField(3);
scoreText=newTextField(5);
scoreText.setEditable(false);
【代码3】//创建giveWord,将wordLabel传递给WordThread构造方法的参数
button.addActionListener(this);
inputText.addActionListener(this);
add(button,BorderLayout.NORTH);
add(wordLabel,BorderLayout.CENTER);
PanelsouthP=newPanel();
southP.add(newLabel("输入标签所显示的汉字后回车:
"));
southP.add(inputText);
southP.add(scoreText);
add(southP,BorderLayout.SOUTH);
setBounds(100,100,350,180);
setVisible(true);
validate();
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);
}
}
);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==button)
{if(!
(【代码4】))//giveWord调用方法isAlive()
{giveWord=newWordThread(wordLabel);
}
try
{【代码5】//giveWord调用方法start()
}
catch(Exceptionexe){}
}
elseif(e.getSource()==inputText)
{if(inputText.getText().equals(wordLabel.getText()))
{score++;
}
scoreText.setText("得分:
"+score);
inputText.setText(null);
}
}
}
WordThread.java
publicclassThreadWordMainClass
{publicstaticvoidmain(Stringargs[])
{newThreadFrame();
}
}
✧实验后的练习:
1.在WordThread类中增加int型的成员time,其初值为6000,将其中的【代码1】更改为线程中断time毫秒。
在WordThread类增加publicvoidsetTime(intn)方法,使得WordThread线程对象可以调用该方法修改time的值。
(二)旋转的行星。
✧实验要求:
编写一个应用程序,模拟月亮围绕地球旋转、地球围绕太阳旋转。
✧程序模板:
Mycanvas.java
importjava.awt.*;
publicclassMycanvasextendsCanvas
{intr;
Colorc;
publicvoidsetColor(Colorc)
{this.c=c;
}
publicvoidsetR(intr)
{this.r=r;
}
publicvoidpaint(Graphicsg)
{g.setColor(c);
g.fillOval(0,0,2*r,2*r);
}
publicintgetR()
{returnr;
}
}
Planet.java
importjava.awt.*;
publicclassPlanetextendsPanelimplementsRunnable
{【代码1】//用Thread类声明一个moon对象
MycanvasyellowBall;
doublepointX[]=newdouble[360],
pointY[]=newdouble[360];//用来表示画布左上角端点坐标的数组
intw=100,h=100;
intradius=30;
Planet()
{setSize(w,h);
setLayout(null);
yellowBall=newMycanvas();
yellowBall.setColor(Color.yellow);
add(yellowBall);
yellowBall.setSize(12,12);
yellowBall.setR(12/2);
pointX[0]=0;
pointY[0]=-radius;
doubleangle=1*Math.PI/180;//刻度为1度
for(inti=0;i<359;i++)//计算出数组中各个元素的值
{pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];
pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);
}
for(inti=0;i<360;i++)
{pointX[i]=pointX[i]+w/2;//坐标平移
pointY[i]=pointY[i]+h/2;
}
yellowBall.setLocation((int)pointX[0]-yellowBall.getR(),
(int)pointY[0]-yellowBall.getR());
【代码2】//创建moon线程,当前面板做为该线程的目标对象
}
publicvoidstart()
{try{moon.start();
}
catch(Exceptionexe){}
}
publicvoidpaint(Graphicsg)
{g.setColor(Color.blue);
g.fillOval(w/2-9,h/2-9,18,18);
}
publicvoidrun()
{inti=0;
while(true)
{i=(i+1)%360;
yellowBall.setLocation((int)pointX[i]-yellowBall.getR(),
(int)pointY[i]-yellowBall.getR());
try{【代码3】//Thread类调用类方法sleep使得线程中断10豪秒
}
catch(InterruptedExceptione){}
}
}
}
HaveThreadFrame.java
importjava.awt.*;
importjava.awt.event.*;
publicclassHaveThreadFrameextendsFrameimplementsRunnable
{【代码4】//用Thread类声明一个rotate对象
Planetearth;
doublepointX[]=newdouble[360],
pointY[]=newdouble[360];
intwidth,height;
intradius=120;
HaveThreadFrame()
{rotate=newThread(this);
earth=newPlanet();
setBounds(0,0,360,400);
width=getBounds().width;
height=getBounds().height;
pointX[0]=0;
pointY[0]=-radius;
doubleangle=1*Math.PI/180;
for(inti=0;i<359;i++)
{pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];
pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);
}
for(inti=0;i<360;i++)
{pointX[i]=pointX[i]+width/2;
pointY[i]=pointY[i]+height/2;
}
setLayout(null);
setVisible(true);
validate();
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);
}
}
);
add(earth);
earth.setLocation((int)pointX[0]-earth.getSize().width/2,
(int)pointY[0]-earth.getSize().height/2);
earth.start();
【代码5】//用rotate调用start方法
}
publicvoidrun()
{inti=0;
while(true)
{i=(i+1)%360;
earth.setLocation((int)pointX[i]-earth.getSize().width/2,
(int)pointY[i]-earth.getSize().height/2);
try{Thread.sleep(100);
}
catch(InterruptedExceptione){}
}
}
publicvoidpaint(Graphicsg)
{g.setColor(Color.red);
g.fillOval(width/2-15,height/2-15,30,30);
}
}
HaveThreadFrame.java
publicclassThreadRotateMainClass
{publicstaticvoidmain(Stringargs[])
{newHaveThreadFrame();
}
}
✧实验后的练习:
1.在Planet类中再增加一个Mycanvas对象greenBall和一个Thread对象Satellite,线程Satellite占有CPU资源期间可以让greenBall画布旋转。
(三)双线程接力。
✧实验要求:
编写一个应用程序,除了主线程外,还有两个线程:
first和second。
first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)。
1.在MoveButton类中再增加一个蓝色的按钮和一个third线程,third线程负责将这个蓝色的按钮从(200,60)运动到(300,60)。
1.在MoveButton类中再增加一个蓝色的按钮和一个third线程,third线程负责将这个蓝色的按钮从(200,60)运动到(300,60)。
✧程序模板:
MoveButton.java
importjava.awt.*;
importjava.awt.event.*;
publicclassMoveButtonextendsFrameimplementsRunnable,ActionListener
{【代码1】//用Thread类声明first,second两个线程对象
ButtonredButton,greenButton,startButton;
intdistance=10;
MoveButton()
{【代码2】//创建first线程,当前窗口做为该线程的目标对象
【代码3】//创建first线程,当前窗口做为该线程的目标对象
redButton=newButton();
greenButton=newButton();
redButton.setBackground(Color.red);
greenButton.setBackground(Color.green);
startButton=newButton("start");
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(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);
}
}
);
}
publicvoidactionPerformed(ActionEvente)
{try{first.start();
second.start();
}
catch(Exceptionexp){}
}
publicvoidrun()
{while(true)
{if(【代码4】)//判断当前占有CPU资源的线程是否是first
{moveComponent(redButton);
try{Thread.sleep(20);
}
catch(Exceptionexp){}
}
if(【代码5】)//判断当前占有CPU资源的线程是否是second
{moveComponent(greenButton);
try{Thread.sleep(10);
}
catch(Exceptionexp){}
}
}
}
publicsynchronizedvoidmoveComponent(Componentb)
{
if(Thread.currentThread()==first)
{while(distance>100&&distance<=200)
try{wait();
}
catch(Exceptionexp){}
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(Exceptionexp){}
distance=distance+1;
b.setLocation(distance,60);
if(distance>200)
{distance=10;
b.setLocation(100,60);
notifyAll();
}
}
}
}
MoveButtonMainClass.java
publicclassMoveButtonMainClass
{publicstaticvoidmain(Stringargs[])
{newMoveButton();
}
✧实验后的练习:
1.在MoveButton类中再增加一个蓝色的按钮和一个third线程,third线程负责将这个蓝色的按钮从(200,60)运动到(300,60)。
四、实验结果
(一)汉字打字练习。
(二)旋转的行星。
(三)双线程接力。
五、总结
通过这次实验掌握了利用JAVA语言编写多线程程序的方法,掌握了线程的调度方法,掌握了多线程环境中GUI程序的编写方法。
了解了线程的概念、线程的生命周期。
以及多线程的编程方法:
继承Thread类与使用Runnable接口,学会了使用多线程机制实现动画。