`

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();
	}
}	

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics