Java语言程序设计试题及答案5.docx

上传人:b****4 文档编号:4160072 上传时间:2022-11-28 格式:DOCX 页数:34 大小:101.64KB
下载 相关 举报
Java语言程序设计试题及答案5.docx_第1页
第1页 / 共34页
Java语言程序设计试题及答案5.docx_第2页
第2页 / 共34页
Java语言程序设计试题及答案5.docx_第3页
第3页 / 共34页
Java语言程序设计试题及答案5.docx_第4页
第4页 / 共34页
Java语言程序设计试题及答案5.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

Java语言程序设计试题及答案5.docx

《Java语言程序设计试题及答案5.docx》由会员分享,可在线阅读,更多相关《Java语言程序设计试题及答案5.docx(34页珍藏版)》请在冰豆网上搜索。

Java语言程序设计试题及答案5.docx

Java语言程序设计试题及答案5

Java语言程序设计试题及答案

第八章多线程

8.1建立线程有哪两种方法?

答:

一是继承Thread类声明Thread子类,用Thread子类创建线程对象。

二是在类中实现Runnable接口,在类中提供Runnable接口的run()方法。

无论用哪种方法,都需要java基础类库中的Thread类及其方法的支持。

程序员能控制的关键性工作有两个方面:

一是编写线程的run()方法;二是建立线程实例。

8.2怎样设置线程的优先级?

答:

setPriority(intp),设定线程的优先级为p(1~10)。

线程创建时,子线程继承父线程的优先级。

优先级的数值越大优先级越高(缺省为5)。

常用以下3个优先级:

Thread.MIN_PRIORITY(最低)

Thread.MAX_PRIORITY(最高)

Thread.NORMAL_PRIORITY(标准)

8.3编写程序,一个画圆,一个画椭圆。

程序运行结果:

Applet的源文件:

Work8_3.java

importjava.applet.Applet;

importjava.awt.Color;

importjava.awt.Graphics;

/**

*8.3用一个红色笔画圆,同时用一个蓝色笔画椭圆

*/

publicclassWork8_3extendsAppletimplementsRunnable

{

privatestaticfinallongserialVersionUID=1L;

/**两个线程,红笔线程,和蓝笔线程*/

privateThreadred_thread,blue_thread;

/**红色画笔、蓝色画笔*/

privateGraphicsredPen,bluePen;

/**蓝、红笔画时需要旋转角度*/

privateintblue_angle=0,red_angle=0;

/**红色画的图案的中心坐标*/

privateinta_red=100,b_red=100;

/**蓝色画的图案的中心坐标*/

privateinta_blue=300,b_blue=100;

/**圆形半径*/

privateintradius_red=80;

/**椭圆的两个半径*/

privateintradius1_blue=150,radius2_blue=100;

publicvoidinit()

{

red_thread=newThread(this);

blue_thread=newThread(this);

redPen=getGraphics();

bluePen=getGraphics();

setBackground(Color.white);

setSize(470,240);

}

publicvoidstart()

{

red_thread.start();//两个线程开始

blue_thread.start();

}

publicvoidrun()

{

while(true)

{

if(Thread.currentThread()==red_thread)

{

redPen.drawOval(25,25,160,160);//绘制圆的边框。

x,y,width,height

intx=getX(radius_red,red_angle,a_red);

inty=getY(radius_red,red_angle,b_red);

redPen.setColor(Color.WHITE);

redPen.fillOval(x,y,10,10);//用白色画一次,可以擦出痕迹

red_angle+=3;

if(red_angle>=360)

red_angle=0;

x=getX(80,red_angle,100);

y=getY(80,red_angle,100);

redPen.setColor(Color.RED);

redPen.fillOval(x,y,10,10);//使用当前颜色填充外接指定矩形框的椭圆

try

{

Thread.sleep(10);

}

catch(InterruptedExceptione)

{

}

}

elseif(Thread.currentThread()==blue_thread)

{

bluePen.drawOval(a_blue/2+5,b_blue/2-45,radius1_blue*2,

radius2_blue*2);//绘制椭圆的边框。

x,y,width,height

intx=getX(radius1_blue,blue_angle,a_blue);

inty=getY(radius2_blue,blue_angle,b_blue);

bluePen.setColor(Color.WHITE);

bluePen.fillOval(x,y,10,10);//擦除痕迹

blue_angle+=3;

if(blue_angle>=360)

blue_angle=0;

x=getX(radius1_blue,blue_angle,300);

y=getY(radius2_blue,blue_angle,100);

bluePen.setColor(Color.BLUE);

bluePen.fillOval(x,y,10,10);//擦除痕迹

try

{

Thread.sleep(20);

}

catch(InterruptedExceptione)

{

}

}

}

}

/**

*用参数方程方法,计算坐标的方法

*@paramr圆的半径,椭圆的两个半轴

*@paramlocx坐标的相对偏移量

*@paramangle旋转的角度,单位为角度

*@return计算后的坐标值

*/

publicintgetX(intr,intangle,intlocX)

{

intx=locX+(int)(r*Math.cos(Math.PI/180.0*angle));

returnx;

}

/**

*用参数方程方法,计算坐标的方法

*@paramr圆的半径,椭圆的两个半轴

*@paramlocY坐标的相对偏移量

*@paramangle旋转的角度,单位为角度

*@return计算后的坐标值

*/

publicintgetY(intr,intangle,intlocY)

{

inty=locY+(int)(r*Math.sin(Math.PI/180.0*angle));

returny;

}

}

8.4在多线程程序中,要考虑互斥的原因是什么?

在Java中如何解决?

答:

多线程之间要共享资源,为了保护资源在使用时,不被其他线程使用。

在Java语言中,使用关键字synchronized定义临界段,能对共享对象的操作上锁。

8.5在多线程程序中,要考虑同步的原因是什么?

在Java中如何解决?

答:

在临界段中使用wait()方法,使执行该方法的线程等待,并允许其他线程使用这个临界段。

wait()方法常用以下两种格式:

wait(),让线程一直等待,直到被notify()或notifyAll()方法唤醒。

wait(longtimeout),让线程等待到被唤醒,或经过指定时间后结束等待。

当线程使用完临界段后,用notify()方法通知由于想使用这个临界段而处于等待的线程结束等待。

notify()方法只是通知第一个处于等待的线程。

如果某个线程在使用完临界段方法后,其他早先等待的线程都可结束等待,重新竞争CPU,则可以notifyAll()方法。

8.6模拟排队买票,球票5元,购票者持有5,10,20,50元的,售票员手里开始没有零钱。

程序运行结果:

主窗体源文件:

BuyTicketFrame.java

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

/**

*8.6线程实现,购票规则,由于我想的算法太过复杂,只写到50元面值的

*@author段智敏

*/

publicclassBuyTicketFrameextendsJFrameimplementsActionListener,Runnable

{

privatestaticfinallongserialVersionUID=1L;

privateConductorlady;

privateintarray[]={10,10,5,20,50,5,5,5,5,5,5,10,20,10,5,10,20,5};

privateintnumber=array.length;

privateThreadthread[]=newThread[number];

staticJTextAreatext;

privateJButtonbegin_button,replay_button;

privateJPanelpanel;

publicBuyTicketFrame()

{

super("第八章,第六题,线程模拟购票");

lady=newConductor(15);

text=newJTextArea();

panel=newJPanel();

begin_button=newJButton("开始买票");

replay_button=newJButton("重新开始");

for(inti=0;i

thread[i]=newThread(this);

begin_button.addActionListener(this);

replay_button.addActionListener(this);

panel.setLayout(newFlowLayout());

panel.add(begin_button);

panel.add(replay_button);

this.add(panel,BorderLayout.NORTH);

this.add(newJScrollPane(text),BorderLayout.CENTER);

this.setSize(1000,700);

this.setVisible(true);

this.validate();

this.addWindowListener(newWindowAdapter()//窗口监视器

{

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

});

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==begin_button)//线程开始

{

text.setText("********"+number+"个人排队买票,共"+lady.getTicketAmount()+"张票,开始卖票:

\n");

try

{

for(inti=0;i

{

thread[i].start();

if(thread[i].isAlive())

text.append(""+(i+1)+"线程开始\n");

}

begin_button.setForeground(Color.RED);

}

catch(IllegalThreadStateExceptionee)

{

text.append("error:

重复启动线程"+ee.toString());

}

//检查线程是否结束

while(true)

{

intn=0;

for(inti=0;i

{

if(thread[i].isAlive())

break;

else

n++;

}

if(n==array.length)

break;

}

begin_button.setForeground(Color.BLUE);//全部线程结束

text.append("\n全部线程结束,已经没有排队等待买票的了");

}

if(e.getSource()==replay_button)

{

this.removeAll();

newBuyTicketFrame();

}

}

publicvoidrun()

{

for(inti=0;i

{

if(Thread.currentThread()==thread[i])

lady.rule(i+1,array[i]);

}

}

publicstaticvoidmain(Stringargs[])

{

newBuyTicketFrame();

}

}

售票员类源文件:

Conductor.java

/**

*售票员

*@author段智敏

*/

classConductor

{

/**售票员开始持有的各种钞票的数量*/

privateintfive=0,ten=0,twenty=0,fifty=0;

/**总的票数*/

privateintticket_amount;

/**

*构造方法

*@paramnumber-票的初始数

*/

publicConductor(intnumber)

{

this.ticket_amount=number;

}

publicintgetTicketAmount()

{

returnticket_amount;

}

/**

*买票规则方法

*/

publicsynchronizedvoidrule(intindex,intmoney)

{

if(ticket_amount<=0)

{

BuyTicketFrame.text.append(index+"************售票结束**********\n");

return;

}

if(money==5)

fiveGive(index);

if(money==10)

tenGive(index);

if(money==20)

twentyGive(index);

if(money==50)

fiftyGive(index);

BuyTicketFrame.text

.append("-------------------------------------------------------------------------------");

BuyTicketFrame.text.append("剩余票数:

"+ticket_amount+"张:

¥:

5元:

"+five+"张;10元:

"+ten

+"张;20元:

"+twenty+"张;50元:

"+fifty+"张\n");

notifyAll();//唤醒所以线程

}

publicvoidfiveGive(intindex)

{

if(ticket_amount<=0)

{

BuyTicketFrame.text.append(index+"************售票结束**********\n");

return;

}

else

{

five++;

ticket_amount--;

BuyTicketFrame.text.append("第"+index+"个,5快的,钱正好,给你票\n");

}

}

publicvoidtenGive(intindex)//10元找钱规则

{

while(true)

{

if(five>=1)

break;

else

{

BuyTicketFrame.text.append("第"+index+"个,10元,找不开,发...生...等...待...☆☆☆☆☆\n");

try

{

wait();

}

catch(InterruptedExceptione)

{

}

}

}

if(ticket_amount<=0)

{

BuyTicketFrame.text.append(index+"************售票结束**********\n");

return;

}

else

{

ticket_amount--;

five--;

ten++;

BuyTicketFrame.text.append("第"+index+"个,10元的,找你5元的,给你票\n");

}

}

publicvoidtwentyGive(intindex)//20元找钱规则

{

while(true)

{

if(((five>=1&&ten>=1)||five>=3))//20元的两种找钱规则:

3*5,10+5

break;//跳出while循环,意味着有符合的规则

else

{

BuyTicketFrame.text.append("第"+index+"个,20元,找不开,发...生...等...待...☆☆☆☆☆\n");

try

{

wait();

}

catch(InterruptedExceptione)

{

}

}

}

if(ticket_amount<=0)

{

BuyTicketFrame.text.append(index+"************售票结束**********\n");

return;

}

elseif(ten>=1&&five>=1)//20元找钱规则:

10+5

{

ticket_amount--;

five--;

ten--;

twenty++;

BuyTicketFrame.text.append("第"+index+"个,20元的,找你1个5元,1个10块的,给你票\n");

return;

}

elseif(five>=3)//20元找钱规则:

5*3

{

ticket_amount--;

five-=3;

twenty++;

BuyTicketFrame.text.append("第"+index+"个,20元的,找你3个5元的,给你票\n");

}

}

publicvoidfiftyGive(intindex)//50元,找钱规则

{

while(true)

{

if((twenty>=2&&five>=1)||(twenty>=1&&ten>=2&&five>=1)

||(twenty>=1&&ten>=1&&five>=3)||(ten>=4&&five>=1)

||(ten>=3&&five>=3)||(ten>=2&&five>=5)

||(ten>=1&&five>=7)||(five>=9))

break;//50元的8种找钱方法

else

{

BuyTicketFrame.text.append("第"+index+"个,50元,找不开,发...生...等...待...☆☆☆☆☆\n");

try

{

wait();

}

catch(InterruptedExceptione)

{

}

}

}

//跳出while循环后,符合照片规则

if(ticket_amount<=0)

{

BuyTicketFrame.text.append(index+"************售票结束**********\n");

return;

}

//先可20的先找,然后在10元的

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1