线程学习

import java.io.*;

class Daemon extends Thread {
	private static final int SIZE = 10;//定义一个静态常量
	private Thread[] t = new Thread[SIZE];//定义一个线程数组
	public Daemon() {//构造方法
		setDaemon(true);//设置一个驻留的线程
		start();//运行线程
	}
	public void run() {
		for(int i = 0; i < SIZE; i++) {
			t[i] = new DaemonSpawn(i);//把线程数组进行实例化
		}
		for(int i = 0; i < SIZE; i++) {
			System.out.println("t[" + "].isDaemon()= " 
			+ t[i].isDaemon());//打印出某是否为一个驻留程序
		}
		while(true) {//循环让出控制权给同一个级别的线程
			yield();
		}
	}
}

class DaemonSpawn extends Thread {
	public DaemonSpawn(int i) {//构造方法
		System.out.println("DaemonSpawn" +  i +"started");//打印是哪个开始执行
		start();//让线程开始执行起来
	}
	public void run() {
		while(true) {//一直都在让出它的控制权
			yield();
		}
	}
}

public class Daemons {
	public static void main(String[] args) {
		Thread d = new Daemon();
		System.out.println("d.isDaemon()= "+d.isDaemon());
		BufferedReader stdin =//构造了一个字符输入流
	    new BufferedReader(new InputStreamReader(System.in));
	    System.out.println("Waiting for CR");
	    try {
	    	stdin.readLine();//从键盘中读一行
	    }catch(IOException e) {
	    }
	}
}

 

 

import  java.io.PrintWriter;

class Point2D {
	int x, y;
	
	Point2D() {
		this(0, 0);
	}
	Point2D(int x) {
		this(x, 0);
	}
	Point2D(int x, int y) {
		this.x = x;
		this.y = y;
	}
	double length() {
		return Math.sqrt(x * x + y * y);
	}
}

class MyPoint extends Point2D {
	int x, y;
	
	MyPoint(int x, int y) {
		super(x, y);
		this.x = x;
		this.y = y;
	}
	double length() {
		return Math.sqrt(x * x + y * y);
	}
	double distance() {
		return Math.abs(this.length() - super.length());
	}
}

public class PointTest {
	public static void main(String[] args) {
		PrintWriter out = new PrintWriter(System.out, true);
		MyPoint mp = new MyPoint(4, 3);
		Point2D p = new Point2D(11);
		Point2D q = mp;
		mp.x = 5;
		mp.y = 12;
		out.println(mp.y);
//		out.println(mp.x);
		out.println(p.y);
//		out.println(q.y);
//		out.println(mp.length());
//		out.println(p.length());
//		out.println(q.length());
//		out.println(mp.distance());
	}
}

 

你可能感兴趣的:(thread)