当前位置:文档之家› Java语言程序设计郑莉第八章课后习地的题目详解

Java语言程序设计郑莉第八章课后习地的题目详解

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 Data{String studentId;String name;boolean available = false;// 判断是读是写Scanner in = new Scanner(System.in);// 定义一个输入对象public synchronized void read(){if(available)try{wait();}catch(Exception e){}System.out.printf("请输入学号:");try{studentId=in.next();}catch(Exception e){System.out.println("输入学号出错!"); }System.out.printf("请输入姓名:");try{name=in.next();}catch(Exception e){System.out.println("输入姓名出错!");}System.out.println();available=true;notify();}public synchronized void write(){if(!available)try{wait();}catch(Exception e){}System.out.println("输出学生学号:"+studentId+" 姓名:"+name+"\n");available=false;notify();}}//Read类public class Read extends Thread{ Data d1 = null;public Read(Data d){this.d1=d;}public void run(){while(true){d1.read();}}}//Write类class Write extends Thread{Data 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方法,使较低优先级的线程获得运行的机会。

(这里可以借鉴课本例8—13)//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 TestThread[2];for(int i=0;i<2;i++){runners[i]=new TestThread(i);}runners[0].setPriority(2);runners[1].setPriority(5);for(int i=0;i<2;i++){runners[i].start();}}}运行结果:8.主线程控制新线程的生命,当主线程运行一段时间后,控制新线程死亡,主线程继续运行一段时间后结束。

相关主题