Java语言程序设计(郑莉)第八章课后习题答案.docx

上传人:wux****ua 文档编号:9045924 上传时间:2020-04-02 格式:DOCX 页数:16 大小:229.20KB
返回 下载 相关 举报
Java语言程序设计(郑莉)第八章课后习题答案.docx_第1页
第1页 / 共16页
Java语言程序设计(郑莉)第八章课后习题答案.docx_第2页
第2页 / 共16页
Java语言程序设计(郑莉)第八章课后习题答案.docx_第3页
第3页 / 共16页
点击查看更多>>
资源描述
Java语言程序设计第八章课后习题答案1.进程和线程有何区别,Java是如何实现多线程的。答:区别:一个程序至少有一个进程,一个进程至少有一个线程;线程的划分尺度小于进程;进程在执行过程中拥有独立的内存单元,而多个线程共享内存,从而极大地提高了程序的运行效率。Java程序一般是 继承Thread 类 或者实现 Runnable接口,从而实现多线程。2.简述线程的生命周期,重点注意线程阻塞的几种情况,以及如何重回就绪状态。答:线程的声明周期:新建-就绪-(阻塞)-运行-死亡线程阻塞的情况:休眠、进入对象wait池等待、进入对象lock池等待;休眠时间到回到就绪状态;在wait池中获得notify()进入lock池,然后获得锁棋标进入就绪状态。3.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000毫秒以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。(注:两个类,相同一个测试类)/Runnable接口实现的线程runable类public class runnable implements Runnable private String city; public runnable() public runnable(String city) this.city = city; public void run() for (int i = 0; i 10; i+) System.out.println(city); try /休眠1000毫秒。 Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace(); / Thread类实现的线程thread类public class runnable extends Thread private String city; public runnable() public runnable(String city) this.city = city; public void run() for (int i = 0; i 10; i+) System.out.println(city); try /休眠1000毫秒。 Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace(); /test8_3public class test8_3 public static void main(String args) / 将创建一个线程对象,这个对象接受一个实现了Runnable接口。实际上这里也就是使用run()方法runnable r1=new runnable(广州);runnable r2=new runnable(乌鲁木齐);Thread t1 = new Thread(r1);Thread t2 = new Thread(r2);/ 启动线程t1.start();t2.start();运行结果分别为: 4.编写一个多线程程序实现如下功能:线程A和线程B分别在屏幕上显示信息“start”后,调用wait等待;线程C开始后调用sleep休眠一段时间,然后调用notifyall,使线程A和线程B继续运行。线程A和线程B恢复运行后输出信息“end”后结束,线程C在判断线程B和线程A结束后自己结束运行。/test8_4public class test8_4 Thread A = new Thread(A) public void run() Wait(A);Thread B = new Thread(B) public void run() Wait(B);Thread C = new Thread(C) public void run() while (true) if (!A.isAlive() & !B.isAlive()return;try Thread.sleep(2000); catch (InterruptedException e) e.printStackTrace();notifyall();public synchronized void Wait(String name) System.out.println(name + .start);try wait(); catch (InterruptedException e) e.printStackTrace();System.out.println(name + .end);public synchronized void notifyall() notifyAll();public static void main(String args) test8_4 test = new test8_4();/A、B两线程一起输入start和输出end,不过中间有C让线程休眠2秒,没法全部一次性输出,/之后再唤醒,让AB继续输出下半部分endtest.A.start();test.B.start();test.C.start();运行结果:5.实现一个数据单元,包括学号和姓名两部分。编写两个线程,一个线程往数据单元中写,另一个线程往外读。要求没写一次就往外读一次。/Data类import java.util.Scanner;public class DataString studentId;String name;boolean available = false;/ 判断是读是写Scanner in = new Scanner(System.in);/ 定义一个输入对象public synchronized void read()if(available)trywait();catch(Exception e)System.out.printf(请输入学号:);trystudentId=in.next();catch(Exception e)System.out.println(输入学号出错!);System.out.printf(请输入姓名:);tryname=in.next();catch(Exception e)System.out.println(输入姓名出错!);System.out.println();available=true;notify();public synchronized void write()if(!available)trywait();catch(Exception e)System.out.println(输出学生学号:+studentId+ 姓名:+name+n);available=false;notify();/Read类public class Read extends ThreadData d1 = null;public Read(Data d)this.d1=d;public void run()while(true)d1.read();/Write类class Write extends ThreadData d2=null;public Write(Data d)this.d2=d;public void run()while(true)d2.write();/test8_5类public class test8_5 public static void main(String args)Data data=new Data();new Read(data).start();new Write(data).start();运行结果:6.创建两个不同优先级的线程,都从1数到10000,看看哪个数得快。(注:线程的优先级别越高低与执行速度没有绝对关系!)/Count类public class Count extends Thread int which;int n = 10000;public Count(int which)this.which=which;public void run() for (int i = 1; i = n; i+) if (i = n) System.out.println(which+号进程+ 结束!);/test8_6public class test8_6 public static void main(String args) Count count1 = new Count(1);Count count2 = new Count(2);Thread t1 = new Thread(count1);Thread t2 = new Thread(count2);t1.setPriority(2);/1号进程优先级是2t2.setPriority(8);/2号进程优先级是8t1.start();t2.start();运行结果: 都有可能。7.编写一个Java程序,以说明较高优先级的线程通过调用sleep方法,使较低优先级的线程获得运行的机会。(这里可以借鉴课本例813)/TestThread类public class TestThread extends Thread private int tick = 1;private int num;public TestThread(int i) this.num = i;public void run() while (tick 400000) tick+;if (tick % 50000) = 0) System.out.println(Thread # + num + ,tick = + tick);yield();try sleep(1); catch (Exception e) /test8_7public class test8_7 public static void main(String args)TestThread runners = new TestThread2;for(int i=0;i2;i+)runnersi=new TestThread(i);runners0.setPriority(2);runners1.setPriority(5);for(int i=0;i0)result=result*i;i-;*/System.out.println(the new thread of +num+is +result);System.out.println(new thread ends);/test8_8public class test8_8 public static void main(String args)System.out.println(main tread start.);SonThread newThread = new SonThread(10);newThread.start();/;浪费时间的循环int i=1;while(i=100000)i+;newThread.stop();/结束新进程System.out.println(main thread ends);运行结果:(这个结果很难把握,通常都会出现2那样的结果,知道原理就好。) 29.用两个线程模拟存、取货物。一个线程往一对象里放货物(包括品名、价格),另外一个线程取货物。分别模拟“放一个、取一个”和“放若干个、取若干个”两种情况。/Good货物类public class Good String name;int price;public Good()public Good(String name,int price)this.name=name;this.price=price;public Good(Good g)this.name=g.name;this.price=g.price;/WareHouse仓库类import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class WareHouse Good good = null;Scanner in = new Scanner(System.in);List list = new ArrayList();/用来存放商品对象int count;/想存入商品的个数boolean available = true;public WareHouse() public WareHouse(int count) this.count = count;public WareHouse(List list) this.count = count;for (int i = 0; i list.size(); i+) this.list.add(Good) list.get(i);public synchronized void saveGood() if (available=false) try wait(); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();for (int i = 0; i count; i+) String n;int p;System.out.println(请输入 + (i+1) + 号商品的名称:);n = in.next();System.out.println(价格:);p = in.nextInt();good = new Good(n, p);list.add(good);available = false;notify();public synchronized void takeGood() if (available=true) try wait(); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();for (int i = 0; i list.size(); i+) good = (Good) list.get(i);System.out.print(i+1)+ 号商品的名称为: + good.name);System.out.println(t价格为: + good.price);available = true;notify();/Save存货物类public class Save extends ThreadWareHouse wareHouse = null;public Save(WareHouse w)this.wareHouse=w;public void run()wareHouse.saveGood();/Take取货物类public class Take extends ThreadWareHouse wareHouse = null;public Take(WareHouse w)this.wareHouse=w;public void run()wareHouse.takeGood();/test8_9测试类import java.util.Scanner;public class test8_9 public static void main(String args)Scanner in =new Scanner(System.in);System.out.println(请输入仓库的容量:);int i=in.nextInt();WareHouse wareHouse=new WareHouse(i);Thread saveGood=new Save(wareHouse);Thread takeGood=new Take(wareHouse);saveGood.start();takeGood.start();运行结果: 10.用两个线程模拟对话,任何一个线程都可以随时收发信息。(这道题本人搞不清楚,暂且用网上的给大家参考下。听说要用到:Socket.getInputStream()获取输入流用于“接收”Socket.getOutputStream()获取输出流用于“发送”)/test8_10import java.io.*;import java.net.*;import java.util.*;public class test8_10 public static void main(String args) try / establish server socket ServerSocket s = new ServerSocket(8189); / wait for client connection Socket incoming = s.accept(); try InputStream inStream = incoming.getInputStream(); OutputStream outStream = incoming.getOutputStream(); Scanner in = new Scanner(inStream); PrintWriter out = new PrintWriter(outStream, true); out.println(Hello! Enter BYE to exit.); / echo client input boolean done = false; while (!done & in.hasNextLine() String line = in.nextLine(); out.println(Echo: + line); if (line.trim().equals(BYE) done = true; finally incoming.close(); catch (IOException e) e.printStackTrace();
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!