Thinking in java 21 章练习题

// concurrency/Ex1.java
// TIJ4 Chapter Concurrency, Exercise 1, page 1120
/** Implement a Runnable. Inside run(), print a message, and then call yield().
* Repeat this three times, and then return from run(). Put a startup message in
* the constructor and a shutdown message when the task terminates. Create a 
* number of these tasks and drive them using threads.
**/


class Ex1RunnerA implements Runnable {
	public Ex1RunnerA() {
		System.out.println("Constructing Ex1RunnerA");
	}
	public void run() {
		for(int i = 0; i < 3; i++) {
			System.out.println("Hi from Ex1RunnerA");		
			Thread.yield();
		}
		System.out.println("Ex1RunnerA task complete.");
		return;				
	}
}

class Ex1RunnerB implements Runnable {
	public Ex1RunnerB() {
		System.out.println("Constructing Ex1RunnerB");
	}
	public void run() {
		for(int i = 0; i < 3; i++) {
			System.out.println("Hi from Ex1RunnerB");		
			Thread.yield();
		}
		System.out.println("Ex1RunnerB task complete.");
		return;
	}
}

class Ex1RunnerC implements Runnable {
	public Ex1RunnerC() {
		System.out.println("Constructing Ex1RunnerC");
	}
	public void run() {
		for(int i = 0; i < 3; i++) {
			System.out.println("Hi from Ex1RunnerC");		
			Thread.yield();
		}
		System.out.println("Ex1RunnerC task complete.");
		return;	
	}
}

public class Ex1 {
	public static void main(String[] args) {
		Thread ta = new Thread(new Ex1RunnerA());		
		Thread tb = new Thread(new Ex1RunnerB());		
		Thread tc = new Thread(new Ex1RunnerC());
		ta.start();
		tb.start();
		tc.start();
	}
}	

 

// concurrency/Ex2.java
// TIJ4 Chapter Concurrency, Exercise 2, page 1120
/** Following the form of generics/Fibonacci.java, create a task that produces
* a sequence of n Fibonacci numbers, where n is provided to the constructor
* of the task. Create a number of these tasks and drive them using threads.
**/


import static org.greggordon.tools.Print.*;

class Ex2FibonacciA implements Runnable {
	private int n = 0;
	public Ex2FibonacciA(int n) {
		this.n = n;
	}
	private int fib(int x) {
		if(x < 2) return 1;
		return fib(x - 2) + fib(x - 1);
	}
	public void run() {
		for(int i = 0; i < n; i++)
			print(fib(i) + " ");
			println();		
	}
}

class Ex2FibonacciB implements Runnable {
	private int n = 0;
	public Ex2FibonacciB(int n) {
		this.n = n;
	}
	private int fib(int x) {
		if(x < 2) return 1;
		return fib(x - 2) + fib(x - 1);
	}
	public void run() {
		for(int i = 0; i < n; i++)
			print(fib(i) + " ");
			println();		
	}
}

class Ex2FibonacciC implements Runnable {
	private int n = 0;
	public Ex2FibonacciC(int n) {
		this.n = n;
	}
	private int fib(int x) {
		if(x < 2) return 1;
		return fib(x - 2) + fib(x - 1);
	}
	public void run() {
		for(int i = 0; i < n; i++)
			print(fib(i) + " ");	
			println();	
	}
}

class Ex2FibonacciD implements Runnable {
	private int n = 0;
	public Ex2FibonacciD(int n) {
		this.n = n;
	}
	private int fib(int x) {
		if(x < 2) return 1;
		return fib(x - 2) + fib(x - 1);
	}
	public void run() {
		for(int i = 0; i < n; i++)
			print(fib(i) + " ");	
			println();	
	}
}

public class Ex2 {
	public static void main(String[] args) {
		Thread f1 = new Thread(new Ex2FibonacciA(15));		
		Thread f2 = new Thread(new Ex2FibonacciB(15));		
		Thread f3 = new Thread(new Ex2FibonacciC(15));
		Thread f4 = new Thread(new Ex2FibonacciD(15));
		f1.start();
		f2.start();
		f3.start();
		f4.start();
	}
}	

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值