线程学习

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值