Java多线程实验报告详解.docx
- 文档编号:1801303
- 上传时间:2022-10-24
- 格式:DOCX
- 页数:14
- 大小:92.64KB
Java多线程实验报告详解.docx
《Java多线程实验报告详解.docx》由会员分享,可在线阅读,更多相关《Java多线程实验报告详解.docx(14页珍藏版)》请在冰豆网上搜索。
Java多线程实验报告详解
西安邮电大学
(计算机学院)
课内实验报告
实验名称:
多线程
专业名称:
计算机科学与技术
班级:
计科1405班
*******
学号:
********
*******
实验日期:
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;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 多线程 实验 报告 详解