Java学习笔记之三_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java学习笔记之三

Java学习笔记之三

 2011/12/8 8:10:52  独爱Java  http://pinefantasy.iteye.com  我要评论(0)
  • 摘要:Java学习笔记之三一、Java异常处理机制/***@authorAdministrator**@description异常学习测试类*@history*/publicclassExceptionDemo{/***@description*@paramargs*/publicstaticvoidmain(String[]args){//Throable类是所有错误和异常的根基类//Throable类下两个重要的子类Exception和Error//1、编写一个常见的异常例子try{inti=1
  • 标签:笔记 学习 Java 学习笔记
Java学习笔记之三
一、Java异常处理机制
/**
 * @author Administrator
 * 
 * @description 异常学习测试类
 * @history
 */
public class ExceptionDemo {
	/**
	 *@description 
	 *@param args
	 */
	public static void main(String[] args) {
		// Throable类是所有错误和异常的根基类
		// Throable类下两个重要的子类Exception和Error
		
		// 1、编写一个常见的异常例子
		try {
			int i = 1;
			int j = 0;
			int r = i / j;
		} catch (ArithmeticException ae) {
			ae.printStackTrace();
			// Exception in thread "main" java.lang.ArithmeticException: / by zero
		}
		
		// 2、catch多个种类的异常例子
		String s1 = "1";
		String s2 = "0"; // String s2 = "eclipse";
		try{
			int i1 = Integer.parseInt(s1); // 字符串解析成数字
			int i2 = Integer.parseInt(s2);
			int temp = i1/i2;
		} catch(ArithmeticException ae){
			ae.printStackTrace(); // 分母为0异常捕获
		} catch(NumberFormatException nfe){
			nfe.printStackTrace(); // 数字格式转换异常捕获
		}
		
		// 3、异常可以往上抛出去例子
		int[] array = new int[5];
		try{
			int i3 = array[5]; // 数组越界异常
		} catch(ArrayIndexOutOfBoundsException aiobe){ 
			// 捕获异常后往外抛出
			// 在某一层统一处理掉这些异常,比如可以采用拦截器
			throw aiobe;
		}
		
		// 4、自定义异常类,继承自Exception类
		try{
			// 实例化内部类对象比较特殊点
			ExceptionDemo out = new ExceptionDemo(); // 先实例化外部类对象
			throw out.new MyException("hello-exception"); // 再实例化内部类对象
		} catch(MyException me){
			// hello-exception
			System.out.println(me.getLocalizedMessage());
		}
	}
	// 自定义异常类
	class MyException extends Exception{
		public MyException(String msg){
			super(msg);
		}
	}

}

二、 Java多线程编程
/**
 * @author Administrator
 * 
 * @description Java多线程编程入门测试类
 * @history
 */
// 方法一、继承线程类Thread
class MyThread extends Thread{
	public MyThread(String threadName){ // 设置当前线程的名称
		currentThread().setName(threadName);
	}
	public void run(){
		System.out.println(Thread.currentThread().getName());
	}
}
// 方法二、实现Runnable接口
class MyThread2 implements Runnable{
	public void run() {
		System.out.println(Thread.currentThread().getName());
	}
}
public class SynTestDemo {
	/**
	 *@description 
	 *@param args
	 */
	public static void main(String[] args) {
		// 1、线程的定义、线程和进程的区别、为什么要引入线程等
		// 2、Java实现多线程的方法主要有两种:继承Thread类和实现Runnable接口
		// 3、多个线程并发同时访问公共的临界资源的时候需要进行同步处理,过多的同步不当会造成死锁问题
		
		MyThread t1 = new MyThread("hello-thread");
		t1.start(); // 启动线程1
		
		MyThread2 t2 = new MyThread2();
		new Thread(t2).start(); // 启动线程2
	}
}

/**
 * @author Administrator
 * 
 * @description 多线程编程演练例子
 * @history
 */
public class SynABCTest {
	/**
	 * @description
	 * @param args
	 */
	public static void main(String[] args) {
		// 通过具体的例子来加深对多线程的认识
		// 问题为:循环打印10遍ABCABC...ABC
		PrintThread p1 = new PrintThread(0, "A"); // 参数为线程标志和打印的内容
		PrintThread p2 = new PrintThread(1, "B");
		PrintThread p3 = new PrintThread(2, "C");
		// 启动线程A B C
		new Thread(p1).start();
		new Thread(p2).start();
		new Thread(p3).start();
	}
}
// 采用实现接口的方式定义线程类
class PrintThread implements Runnable {
	// 标记执行当前应该执行的线程0、1、2依次表示A B C
	// 定义成静态变量,因为线程各自使用独立的栈
	private static int index = 0; 
	private static Object lock = new Object();
	private int key = 0; // 线程标志
	private int print = 0; // 打印的次数
	private String name; // 打印的内容
	
	public PrintThread(int key, String name) {
		this.key = key;
		this.name = name;
	}
	
	public void run() {
		while (this.print < 10) { // 打印的次数
			synchronized (lock) {
				while (!(this.key == index % 3)) { // 从0开始执行
					try {
						lock.wait(); // 阻塞掉
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.print(this.name); // 打印出内容
				this.print++; // 当前线程打印次数++
				index++; // 线程切换下一个
				lock.notifyAll(); // 唤醒其他等待的线程
			}
		}
	}
}

/**
 * @author Administrator
 * 
 * @description 死锁模拟测试类
 * @history
 */
public class DeadLockTest {
	/**
	 *@description 
	 *@param args
	 */
	public static void main(String[] args) {
		// 过多的同步操作可能会造成死锁问题,死锁产生的原因是形成了环路等待
		// 通过两个线程对象进行模拟,线程A完成一个操作需要资源1和资源2,线程B也是一样
		// 在资源分配的过程中,线程A占用了资源1,等待资源2,此时此刻线程B占用了资源2,等待资源1
		DeadThread dt1 = new DeadThread("1", true);
		DeadThread dt2 = new DeadThread("2", false);
		new Thread(dt1).start(); // 启动线程1
		new Thread(dt2).start(); // 启动线程2
	}
	// 定义静态内部类、类似外部类了
	static class DeadThread implements Runnable{
		/**
		 * 定义资源1和资源2 lock1和lock2
		 */
		private static Object lock1 = new Object();
		private static Object lock2 = new Object();
		private String name; // 线程名称
		private boolean run; // 执行顺序标记
		public DeadThread(String name,boolean run){
			this.name = name;
			this.run = run;
		}
		@Override
		public void run() {
			if(this.run){
				// 线程1先占用资源1
				synchronized(lock1){
					try {
						System.out.println("thread1 used lock1");
						Thread.sleep(3000); // 暂时休眠3秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					// 线程1再去申请资源2,此时资源2已经被线程2占用着不放了
					synchronized(lock2){
						System.out.println("hello dead-lock");
					}
				}
			}else{
				// 线程2先占用资源2
				synchronized(lock2){
					try {
						System.out.println("thread2 used lock2");
						Thread.sleep(3000); // 线程2暂时休眠3秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					} 
					// 线程2再去申请资源1,此时资源1已经被线程1占用着不放了
					synchronized(lock1){
						System.out.println("hello dead-lock");
					}
				}
			}
		}
	}

}

class MyThread1 implements Runnable {
	private boolean flag = true; // 定义标志位
	public void run() {
		int i = 0;
		while (this.flag) {
			System.out.println(Thread.currentThread().getName() + "运行,i = "
					+ (i++));
		}
	}
	public void stop() {
		this.flag = false; // 修改标志位
	}
}

/**
 * @author Administrator
 * 
 * @description 通过修改标记位停止线程
 * @history
 */
public class ThreadStopDemo {
	public static void main(String[] args) {
		MyThread1 my = new MyThread1();
		Thread t = new Thread(my, "线程"); // 建立线程对象
		t.start(); // 启动线程
		try {
			Thread.sleep(50); // 适当地延迟下
		} catch (Exception e) {
			e.printStackTrace();
		}
		my.stop(); // 修改标志位,停止运行
	}
}

三、 Java常用类学习代码
/**
 * @author Administrator
 * 
 * @description Java常用类-StringBuffer学习
 * @history
 */
public class StringBufferTest {
	/**
	 *@description 
	 *@param args
	 */
	public static void main(String[] args) {
		// StringBuffer类在处理字符串时候比较常用
		// 1、StringBuffer类的append方法
		// 具体的方法参数个数和类型,请参看JDK的API即可
		StringBuffer sb = new StringBuffer();
		sb.append("helloworld"); // string类型
		sb.append("\n"); // 特殊符号,换行符
		sb.append(false); // boolean类型
		sb.append('j'); // char类型
		sb.append(1.50d); // double类型
		// ... 等等
		sb.insert(0, "eclipse"); // 在index出插入值
		sb.reverse(); // 反转操作
		sb.replace(1, 3, "helloeclipse"); // 替换操作
		sb.substring(1, 5); // 字符串截取操作
		sb.delete(0, 1); // caozuo.html" target="_blank">删除操作
		sb.indexOf("hello"); // index出现的位置
		
		// 2、StringBuffer类的引用传递
		StringBuffer sb1 = new StringBuffer();
		sb1.append("hello");
		fun(sb1);
		System.out.println(sb1.toString()); // helloworld
		
		// 3、StringBuffer类和String类的区别
		// 一个可变、一个不可变,具体选择根据具体的场景
	}
	private static void fun(StringBuffer sb1) {
		sb1.append("world"); // 改变对应的值
	}
}

import java.io.IOException;

/**
 * @author Administrator
 * 
 * @description Runtime类学习测试
 * @history
 */
public class RuntimeTest {
	/**
	 * @description
	 * @param args
	 */
	public static void main(String[] args) {
		// Runtime类是一个封装了JVM进程的类,每一个java应用程序都有一个JVM实例来支持
		// 因此每个JVM进程对应一个Runtime类的实例对象,由java虚拟机来实例化对象
		// 查看源代码发现,其构造方法被私有化了,类似单例模式
		/*
		 * public class Runtime { private static Runtime currentRuntime = new
		 * Runtime(); public static Runtime getRuntime() { return
		 * currentRuntime; } private Runtime() {} }
		 */
		Runtime run = Runtime.getRuntime(); // 通过静态方法获取实例对象
		// 1、查看一些JVM内存级别的参数
		System.out.println(run.maxMemory()); // 最大内存空间
		System.out.println(run.freeMemory()); // 空间的内存空间
		String str = "";
		for (int i = 0; i < 10000; i++) {
			str += i;
		}
		System.out.println(run.freeMemory()); // 空间的内存空间
		run.gc(); // 进行垃圾回收处理
		System.out.println(run.freeMemory());

		// 2、Runtime类一般和Process类一起使用,可以打开本机的一些进程
		Process p = null; // 定义进程变量
		try {
			p = run.exec("notepad.exe"); // 打开记事本程序
		    /*public Process exec(String command) throws IOException {
		    	return exec(command, null, null);
		    }*/
			// 底层有个ProcessBuilder类处理执行这些命令
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			Thread.sleep(5000); // 让记事本程序执行5秒后关闭掉
		} catch (Exception e) {
		}
		p.destroy(); // 结束此进程
	}

}

import java.io.PrintStream;

class Demo{
	public Demo(){}
	// 覆写该方法,测试System调用gc方法的过程
	protected void finalize() throws Throwable {
		System.out.println("hello-finalize");
	}
}
public class SystemTest {
	/**
	 *@description 
	 *@param args
	 */
	public static void main(String[] args) {
		// System类和上面说到的Runtime类一样也是比较靠近虚拟机实例的类
		// 1、我们经常使用的方式是打印输出操作System.out.println("hello world");
		// PrintStream extends FilterOutputStream extends OutputStream
		PrintStream out = System.out; // 获取打印流
		out.println("helloworld"); // 打印输出
		
		// 2、通过该类获取系统时间操作
		// public static native long currentTimeMillis();
		long current = System.currentTimeMillis();
		System.out.println(current); // 毫秒级别
		
		// 3、查看系统的属性值情况
		System.getProperties().list(out);
		// java.runtime.name=Java(TM) SE Runtime Environment
		// sun.boot.library.path=C:\Program Files\Java\jre6\bin
		// java.vm.version=20.1-b02
		// java.vm.vendor=Sun Microsystems Inc.
		// java.vendor.url=http://java.sun.com/
		// ...等等系统值
		
		// Properties extends Hashtable<Object,Object>
		// Key-Value的键值对形式,实现了Map接口的类
		System.out.println(System.getProperty("os.name")); // Windows 7
	    /*public static String getProperty(String key) {
	    	checkKey(key);
	    	SecurityManager sm = getSecurityManager();
	            if (sm != null) {
	    	    sm.checkPropertyAccess(key);
	    	}
	    	return props.getProperty(key);
	    }*/
		
		// 4、释放内存空间方法调用
		// 在之前的笔记中说到了Object类,其中有个finalize方法
		// protected void finalize() throws Throwable { }
		Demo demo = new Demo();
		demo = null; // 断开引用设置为null
		System.gc(); // 强制性/显示释放内存空间,打印输出hello-finalize
		// 调用的是Runtime类的释放方法
	    /*public static void gc() {
	    	Runtime.getRuntime().gc();
	    }*/
	}
}

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 * 
 * @description 时间帮助工具类
 * @history
 */
public class DateHelperUtil {
	/**
	 * @description 获取当前时间的字符串格式
	 * @return
	 */
	public static String getNowDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdf.format(new Date());
	}

	/**
	 *@description 对传入的date类型时间转换成字符串格式的时间
	 *@param date
	 *@return 返回字符串格式时间
	 */
	public static String formatDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdf.format(date);
	}
	
	/**
	 *@description 对传入的date类型时间转换成字符串格式的时间
	 *@param date
	 *@param formatStr 格式模板
	 *@return 返回字符串格式时间
	 */
	public static String formatDate(Date date,String formatStr){
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(date);
	}
	
	/**
	 *@description 对传入的字符串格式的时间进行解析处理成date类型
	 *@param dateStr
	 *@return
	 * @throws ParseException 解析错误异常
	 */
	public static Date parseDate(String dateStr) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return sdf.parse(dateStr);
	}
	
	/**
	 *@description 对传入的字符串格式的时间进行解析处理成date类型
	 *@param dateStr
	 *@param formatStr 解析字符串的时间模板
	 *@return
	 *@throws ParseException 解析错误异常
	 */
	public static Date parseDate(String dateStr, String formatStr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.parse(dateStr);
	}
	
	/**
	 *@description 获取当前时间的时间戳
	 *@return
	 */
	public static String getTimeStamp() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(new Date());
	}
	
}

import java.math.BigInteger;

/**
 * @author Administrator
 * 
 * @description BigInteger大数学习测试类
 * @history
 */
public class BigIntegerDemo {
	/**
	 *@description 
	 *@param args
	 */
	public static void main(String args[]) {
		BigInteger bi1 = new BigInteger("123456789"); // 声明BigInteger对象
		BigInteger bi2 = new BigInteger("987654321"); // 声明BigInteger对象
		System.out.println("加法操作:" + bi2.add(bi1)); // 加法操作
		System.out.println("减法操作:" + bi2.subtract(bi1)); // 减法操作
		System.out.println("乘法操作:" + bi2.multiply(bi1)); // 乘法操作
		System.out.println("除法操作:" + bi2.divide(bi1)); // 除法操作
		System.out.println("最大数:" + bi2.max(bi1)); // 求出最大数
		System.out.println("最小数:" + bi2.min(bi1)); // 求出最小数
		BigInteger result[] = bi2.divideAndRemainder(bi1); // 求出余数的除法操作
		System.out.println("商是:" + result[0] + ";余数是:" + result[1]);
	}
}

class Student implements Comparable<Student> { // 指定类型为Student
	private String name;
	private int age;
	private float score;

	public Student(String name, int age, float score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public String toString() {
		return name + "\t\t" + this.age + "\t\t" + this.score;
	}

	public int compareTo(Student stu) { // 覆写compareTo()方法,实现排序规则的应用
		if (this.score > stu.score) {
			return -1;
		} else if (this.score < stu.score) {
			return 1;
		} else {
			if (this.age > stu.age) {
				return 1;
			} else if (this.age < stu.age) {
				return -1;
			} else {
				return 0;
			}
		}
	}
}

public class ComparableDemo01 {
	public static void main(String[] args) {
		Student stu[] = { new Student("张三", 20, 90.0f),
				new Student("李四", 22, 90.0f), new Student("王五", 20, 99.0f),
				new Student("赵六", 20, 70.0f), new Student("孙七", 22, 100.0f) };
		java.util.Arrays.sort(stu); // 进行排序操作
		for (int i = 0; i < stu.length; i++) { // 循环输出数组中的内容
			System.out.println(stu[i]);
		}
	}
}

class BinaryTree {
	class Node { // 声明一个节点类
		private Comparable data; // 保存具体的内容
		private Node left; // 保存左子树
		private Node right; // 保存右子树

		public Node(Comparable data) {
			this.data = data;
		}

		public void addNode(Node newNode) {
			// 确定是放在左子树还是右子树
			if (newNode.data.compareTo(this.data) < 0) { // 内容小,放在左子树
				if (this.left == null) {
					this.left = newNode; // 直接将新的节点设置成左子树
				} else {
					this.left.addNode(newNode); // 继续向下判断
				}
			}
			if (newNode.data.compareTo(this.data) >= 0) { // 放在右子树
				if (this.right == null) {
					this.right = newNode; // 没有右子树则将此节点设置成右子树
				} else {
					this.right.addNode(newNode); // 继续向下判断
				}
			}
		}

		public void printNode() { // 输出的时候采用中序遍历
			if (this.left != null) {
				this.left.printNode(); // 输出左子树
			}
			System.out.print(this.data + "\t");
			if (this.right != null) {
				this.right.printNode();
			}
		}
	}

	private Node root; // 根元素

	public void add(Comparable data) { // 加入元素
		Node newNode = new Node(data); // 定义新的节点
		if (root == null) { // 没有根节点
			root = newNode; // 第一个元素作为根节点
		} else {
			root.addNode(newNode); // 确定是放在左子树还是放在右子树
		}
	}

	public void print() {
		this.root.printNode(); // 通过根节点输出
	}
}

public class ComparableDemo02 {
	public static void main(String[] args) {
		BinaryTree bt = new BinaryTree();
		bt.add(8);
		bt.add(3);
		bt.add(3);
		bt.add(10);
		bt.add(9);
		bt.add(1);
		bt.add(5);
		bt.add(5);
		System.out.println("排序之后的结果:");
		bt.print();
	}
}

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Administrator
 * 
 * @description 任务调度程序学习类
 * @history
 */
class MyTask extends TimerTask{	// 任务调度类都要继承TimerTask
	public void run(){
		SimpleDateFormat sdf = null ;
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
		System.out.println("当前系统时间为:" + sdf.format(new Date())) ;
	}
}

public class TaskTestDemo {
	/**
	 * @description
	 * @param args
	 */
	public static void main(String args[]) {
		Timer t = new Timer(); // 建立Timer类对象
		MyTask mytask = new MyTask(); // 定义任务
		t.schedule(mytask, 1000, 2000); // 设置任务的执行,1秒后开始,每2秒重复
	}
}
发表评论
用户名: 匿名