java第八章答案.doc_第1页
java第八章答案.doc_第2页
java第八章答案.doc_第3页
java第八章答案.doc_第4页
java第八章答案.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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();运行结果:4.lass test implements Runnablepublic test() public void run()if(Thread.currentThread().getName().equals(A)System.out.println(A.start);synchronized(this)trywait();System.out.println(A.end);notify();catch(Exception e) else if(Thread.currentThread().getName().equals(B)System.out.println(B.start);synchronized(this)trywait();System.out.println(B.end);catch(Exception e)else if(Thread.currentThread().getName().equals(C)tryThread.sleep(10);synchronized(this)notify();catch(Exception e)public class d public static void main(String args) test show=new test();Thread A=new Thread(show,A);Thread B=new Thread(show,B);Thread C=new Thread(show,C);A.start();B.start();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那样的结果,知道原理就好。)2 9.用两个线程模拟存、取货物。一个线程往一对象里放货物(包括品名、价格),另外一个线程取货物。分别模拟“放一个、取一个”和“放若干个、取若干个”两种情况。/Good货物类public class Good String name;int price;public Good()public Good(String name,int price)=name;this.price=price;public Good(Good g)=;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)+ 号商品的名称为: + );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.用两个线程模拟对话,任何一个线程都可以随时收发信息。(这道题本人

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论