Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx

上传人:b****6 文档编号:16215744 上传时间:2022-11-21 格式:DOCX 页数:20 大小:454.67KB
下载 相关 举报
Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx_第1页
第1页 / 共20页
Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx_第2页
第2页 / 共20页
Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx_第3页
第3页 / 共20页
Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx_第4页
第4页 / 共20页
Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx

《Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx》由会员分享,可在线阅读,更多相关《Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx(20页珍藏版)》请在冰豆网上搜索。

Java语言程序设计郑莉第八章课后习地的题目详解文档格式.docx

e.printStackTrace();

}

//Thread类实现的线程thread类

publicclassrunnableextendsThread{

 

//test8_3

publicclasstest8_3{

publicstaticvoidmain(String[]args){

//将创建一个线程对象,这个对象接受一个实现了Runnable接口。

实际上这里也就是使用run()方法

runnabler1=newrunnable("

广州"

);

runnabler2=newrunnable("

乌鲁木齐"

Threadt1=newThread(r1);

Threadt2=newThread(r2);

//启动线程

t1.start();

t2.start();

}

运行结果分别为:

4.编写一个多线程程序实现如下功能:

线程A和线程B分别在屏幕上显示信息“…start”后,调用wait等待;

线程C开始后调用sleep休眠一段时间,然后调用notifyall,使线程A和线程B继续运行。

线程A和线程B恢复运行后输出信息“…end”后结束,线程C在判断线程B和线程A结束后自己结束运行。

//test8_4

publicclasstest8_4{

ThreadA=newThread("

A"

){

publicvoidrun(){

Wait("

}

};

ThreadB=newThread("

B"

ThreadC=newThread("

C"

while(true){

if(!

A.isAlive()&

&

!

B.isAlive())

return;

try{

Thread.sleep(2000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

notifyall();

}

publicsynchronizedvoidWait(Stringname){

System.out.println(name+"

..start"

try{

wait();

}catch(InterruptedExceptione){

e.printStackTrace();

..end"

publicsynchronizedvoidnotifyall(){

notifyAll();

publicstaticvoidmain(Stringargs[]){

test8_4test=newtest8_4();

//A、B两线程一起输入start和输出end,不过中间有C让线程休眠2秒,没法全部一次性输出,

//之后再唤醒,让AB继续输出下半部分end

test.A.start();

test.B.start();

test.C.start();

运行结果:

5.实现一个数据单元,包括学号和姓名两部分。

编写两个线程,一个线程往数据单元中写,另一个线程往外读。

要求没写一次就往外读一次。

//Data类

importjava.util.Scanner;

publicclassData{

StringstudentId;

Stringname;

booleanavailable=false;

//判断是读是写

Scannerin=newScanner(System.in);

//定义一个输入对象

publicsynchronizedvoidread()

{

if(available)

try

{

wait();

catch(Exceptione)

System.out.printf("

请输入学号:

"

try

{

studentId=in.next();

catch(Exceptione)

System.out.println("

输入学号出错!

请输入姓名:

name=in.next();

输入姓名出错!

System.out.println();

available=true;

notify();

publicsynchronizedvoidwrite()

if(!

available)

{

System.out.println("

输出学生学号:

+studentId+"

姓名:

+name+"

\n"

available=false;

//Read类

publicclassReadextendsThread{

Datad1=null;

publicRead(Datad){

this.d1=d;

publicvoidrun(){

while(true)

d1.read();

//Write类

classWriteextendsThread{

Datad2=null;

publicWrite(Datad)

this.d2=d;

publicvoidrun()

d2.write();

//test8_5类

publicclasstest8_5{

publicstaticvoidmain(String[]args){

Datadata=newData();

newRead(data).start();

newWrite(data).start();

6.创建两个不同优先级的线程,都从1数到10000,看看哪个数得快。

线程的优先级别越高低与执行速度没有绝对关系!

//Count类

publicclassCountextendsThread{

intwhich;

intn=10000;

publicCount(intwhich){

this.which=which;

for(inti=1;

=n;

i++){

if(i==n){

System.out.println(which+"

号进程"

+"

结束!

//test8_6

publicclasstest8_6{

Countcount1=newCount

(1);

Countcount2=newCount

(2);

Threadt1=newThread(count1);

Threadt2=newThread(count2);

t1.setPriority

(2);

//1号进程优先级是2

t2.setPriority(8);

//2号进程优先级是8

都有可能。

7.编写一个Java程序,以说明较高优先级的线程通过调用sleep方法,使较低优先级的线程获得运行的机会。

(这里可以借鉴课本例8—13)

//TestThread类

publicclassTestThreadextendsThread{

privateinttick=1;

privateintnum;

publicTestThread(inti){

this.num=i;

while(tick<

400000){

tick++;

if((tick%50000)==0){

System.out.println("

Thread#"

+num+"

,tick="

+tick);

yield();

sleep

(1);

}catch(Exceptione){

//test8_7

publicclasstest8_7{

TestThread[]runners=newTestThread[2];

for(inti=0;

i<

2;

i++){

runners[i]=newTestThread(i);

runners[0].setPriority

(2);

runners[1].setPriority(5);

runners[i].start();

8.主线程控制新线程的生命,当主线程运行一段时间后,控制新线程死亡,主线程继续运行一段时间后结束。

main函数就是主线程,程序里其他的Thread都属于子线程。

可以参考课本的例8—1)

//SonThread类

publicclassSonThreadextendsThread{

publicSonThread(intnum){

this.num=num;

inti=num;

intresult=1;

newthreadstart."

/*while(i>

0){

result=result*i;

i--;

}*/

thenewthreadof"

+num+"

is"

+result);

newthreadends"

//test8_8

publicclasstest8_8{

maintreadstart."

SonThreadnewThread=newSonThread(10);

newThread.start();

//;

浪费时间的循环

inti=1;

while(i<

=100000){

i++;

newThread.stop();

//结束新进程

mainthreadends"

(这个结果很难把握,通常都会出现2那样的结果,知道原理就好。

2

9.用两个线程模拟存、取货物。

一个线程往一对象里放货物(包括品名、价格),另外一个线程取货物。

分别模拟“放一个、取一个”和“放若干个、取若干个”两种情况。

//Good货物类

publicclassGood{

intprice;

publicGood(){

publicGood(Stringname,intprice){

this.name=name;

this.price=price;

publicGood(Goodg){

this.name=g.name;

this.price=g.price;

//WareHouse仓库类

importjava.util.ArrayList;

importjava.util.List;

publicclassWareHouse{

Goodgood=null;

Listlist=newArrayList();

//用来存放商品对象

intcount;

//想存入商品的个数

booleanavailable=true;

publicWareHouse(){

publicWareHouse(intcount){

this.count=count;

publicWareHouse(Listlist){

for(inti=0;

list.size();

this.list.add((Good)list.get(i));

publicsynchronizedvoidsaveGood(){

if(available==false){

try{

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

count;

Stringn;

intp;

请输入"

+(i+1)+"

号商品的名称:

n=in.next();

价格:

p=in.nextInt();

good=newGood(n,p);

list.add(good);

available=false;

publicsynchronizedvoidtakeGood(){

if(available==true){

good=(Good)list.get(i);

System.out.print((i+1)+"

号商品的名称为:

+good.name);

\t价格为:

+good.price);

available=true;

//Save存货物类

publicclassSaveextendsThread{

WareHousewareHouse=null;

publicSave(WareHousew){

this.wareHouse=w;

wareHouse.saveGood();

//Take取货物类

publicclassTakeextendsThread{

publicTake(WareHousew){

publicvoidrun(){

wareHouse.takeGood();

//test8_9测试类

publicclasstest8_9{

publicstaticvoidmain(String[]args){

Scannerin=newScanner(System.in);

请输入仓库的容量:

inti=in.nextInt();

WareHousewareHouse=newWareHouse(i);

ThreadsaveGood=newSave(wareHouse);

ThreadtakeGood=newTake(wareHouse);

saveGood.start();

takeGood.start();

10.用两个线程模拟对话,任何一个线程都可以随时收发信息。

(这道题本人搞不清楚,暂且用网上的给大家参考下。

听说要用到:

Socket.getInputStream()获取输入流用于“接收”

Socket.getOutputStream()获取输出流用于“发送”

//test8_10

importjava.io.*;

import.*;

importjava.util.*;

publicclasstest8_10{

//establishserversocket

ServerSockets=newServerSocket(8189);

//waitforclientconnection

Socketincoming=s.accept();

InputStreaminStream=incoming.getInputStream();

OutputStreamoutStream=incoming.getOutputStream();

Scannerin=newScanner(inStream);

PrintWriterout=newPrintWriter(outStream,true);

out.println("

Hello!

EnterBYEtoexit."

//echoclientinput

booleandone=false;

while(!

done&

in.hasNextLine()){

Stringline=in.nextLine();

Echo:

"

+line);

if(line.trim().equals("

BYE"

))

done=true;

}finally{

incoming.close();

}catch(IOExceptione){

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

当前位置:首页 > 高等教育 > 文学

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

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