面向对象程序设计实验七 线程Word格式.docx
- 文档编号:15717827
- 上传时间:2022-11-15
- 格式:DOCX
- 页数:13
- 大小:117.08KB
面向对象程序设计实验七 线程Word格式.docx
《面向对象程序设计实验七 线程Word格式.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计实验七 线程Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
for(inti=0;
i<
10;
i++){
System.out.println(i+"
"
+getName());
try{
sleep((int)(Math.random()*1000));
}catch(InterruptedExceptione){}
System.out.println("
DONE!
}
publicclassTwoThreadsTest{
publicstaticvoidmain(String[]args){
newSimpleThread("
GotoBeijing"
).start();
Stayhere!
!
"
问题:
(1)程序的运行结果和功能分别是什么?
(2)如果程序中去掉try-catch语句,程序是否仍能正常运行?
(3)Thread子类是如何实现多线程机制的?
(4)如果希望执行三个线程,再显示“GotoShanghai”,程序如何修改?
比较显示次序,是否相同。
为什么会有这样的现象?
答:
1:
运行结果:
功能:
创建两个线程并打印做检测
2:
去掉后程序能够正常运行但是运行的速度加快,即线程没有了等待时间。
3:
在编写复杂程序时相关的类可能已经继承了某个基类,而Java不支持多继承,在这种情况下,便需要通过实现Runnable接口来生成多线程
用到Thread类的构造方法
publicThread(Runnabletarget);
//构造一个新的线程对象,以一个实现Runnable接口的类的对象为参数。
默认名为Thread-n,n是从0开始递增的整数。
4:
首先在函数TwoThreadsTest添加一个线程即是添加语句newSimpleThread("
GotoQinghai"
和System.out.println(“GotoShanghai”);
即可由于学校的机器是四核的无法完成下面的操作!
2.运行下面的程序,理解用实现Runnable接口的方法实现多线程。
//这是一个时钟Applet,它显示当前时间并逐秒进行更新。
importjava.awt.*;
importjava.applet.*;
importjava.util.*;
publicclassClockextendsAppletimplementsRunnable{
ThreadclockThread;
publicvoidstart(){
if(clockThread==null){
clockThread=newThread(this,"
Clock"
);
clockThread.start();
publicvoidrun(){
while(clockThread!
=null){
repaint();
try{
clockThread.sleep(1000);
}catch(InterruptedExceptione){}
publicvoidpaint(Graphicsg){
Datenow=newDate();
g.drawString(now.getHours()+"
;
+now.getMinutes()+"
+now.getSeconds(),5,10);
publicvoidstop(){
clockThread.stop();
clockThread=null;
(1)在什么情况下一般要通过实现runnable接口实现线程机制?
(2)程序的运行结果和功能分别是什么?
(3)Runnable接口是如何实现多线程机制的?
(4)程序中是通过什么方法如何实现逐秒更新显示时间的?
(1)在编写复杂程序时相关的类可能已经继承了某个基类,而Java不支持多继承,在这种情况下,便需要通过实现Runnable接口来生成多线程
(2)结果:
功能:
该程序是一个时钟Applet,它显示当前时间并逐秒进行更新。
(3)定义一个实现java.lang.Runnable接口的类,并在该类中定义Runnable接口的run()方法,同样,该run()方法是线程执行的起点。
(4)程序通过调用clockThread.sleep()方法通过控改变sleep()里面的内容来实现逐秒更新显示时间。
3.运行下面的程序,理解线程的同步。
//程序使用同步块
classCallme{
voidcall(Stringmsg){
System.out.print("
["
+msg);
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
System.out.println("
Interrupted"
}
System.out.println("
]"
classCallerimplementsRunnable{
Stringmsg;
Callmetarget;
Threadt;
publicCaller(Callmetarg,Strings){
target=targ;
msg=s;
t=newThread(this);
t.start();
//同步调用call()方法
synchronized(target){//同步块
target.call(msg);
classJava2{
publicstaticvoidmain(Stringargs[]){
Callmetarget=newCallme();
Callerob1=newCaller(target,"
Hello"
Callerob2=newCaller(target,"
Synchronized"
Callerob3=newCaller(target,"
World"
//等待线程结束
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedExceptione){
(2)程序中如何实现线程的同步?
(3)去掉程序中的synchronized关键字,运行结果如何?
运行结果
创建三个线程,检测线程同步。
2碰到多个线程对同一资源进行访问的情况,这时,就需要协调:
每个对象都只有一个“锁”与之相连,利用多线程对其的争夺可实现线程间的互斥操作。
当线程A获得了一个对象的锁后,线程B必须等待线程A完成规定的操作、并释放出锁后,才能获得该对象的锁,并执行线程B中的操作。
利用synchronized首先判断对象的锁是否在,如果在就获得锁,然后就可以执行紧随其后的代码段;
如果对象的锁不在(已被其他线程拿走),就进入等待状态,直到获得锁
去掉关键字后无运行结果。
运行结果为空,三个线程均不被执行。
4.运行下面的程序,理解线程间的通信。
importjava.lang.Runnable;
importjava.lang.Thread;
publicclassDemoThread1implementsRunnable{
publicDemoThread1(){
TestThreadtestthread1=newTestThread(this,"
1"
TestThreadtestthread2=newTestThread(this,"
2"
testthread2.start();
testthread1.start();
publicstaticvoidmain(String[]args){
DemoThread1demoThread1=newDemoThread1();
publicvoidrun(){
TestThreadt=(TestThread)Thread.currentThread();
if(!
t.getName().equalsIgnoreCase("
)){
synchronized(this){wait();
while(true){
@thread"
+t.getName()+"
="
+t.increaseTime());
if(t.getTime()%10==0)
{synchronized(this)
{System.out.println("
*********changethread**********"
this.notifyAll();
if(t.getTime()==20)break;
wait();
}
}catch(Exceptione){e.printStackTrace();
classTestThreadextendsThread{
privateinttime=0;
publicTestThread(Runnabler,Stringname){
super(r,name);
publicintgetTime(){
returntime;
publicintincreaseTime(){
return++time;
(2)程序是如何实现线程通信的?
可否通过其他方法进行线程间的通信?
(3)程序中如果去掉notify();
语句,程序运行结果会怎样?
为什么?
(4)synchronized(this);
语句的作用是什么?
如果去掉此语句,能否实现线程间的通信?
为
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序设计实验七 线程 面向 对象 程序设计 实验
![提示](https://static.bdocx.com/images/bang_tan.gif)