Java学习之路-(异常、多线程)

异常

  • 异常的处理
/*
throw:
	作用:可以使用throws在指定的方法中抛出指定的异常
	注意:必须写在方法内部,后面的new对象必须是Exception或者是子类对象
	格式:throw new xxxException("异常产生的原因")
*/
public class DemoThrow{
	pubulic static void main() {
		int[] arr = null;
		getElement(arr, 0);
	}

	public static int getElement(int[] arr, int index) {
		if(arr==null){
			throw new NullPointerException("空指针异常");
		} 

		if()index < 0 || index >= arr.length) {
			throw new ArrayIndexOutOfBoundsException();
		} 

		int ele = arr[index];
		return ele;		

		// Objects中有个专门验证是否为空的静态方法,会自动抛出异常
		Objects.requireNonNull(obj, "报错信息");
	}
}

/*
throws:
	作用:异常处理的第一种方式
	格式:修饰符 返回值类型 方法名(参数) throws AAAException,BBBException,...{
	}
	注意:编译异常一定要处理,运行异常可以交给JVM处理
*/
public class Demothrows{
	public static void main(String[] args) throws FileNotFoundException{
		readFile();
	}

	public static void readFile(String path) throws FileNotFoundException {
		if (!path.equals("D:\\\\test.txt")){
			throw new FileNotFoundException();
		}
	}
}

/*
捕获异常try..catch:
*/
public class DemoException{
	public static void main(String[] args) {
		int[] arr = {1, 2, 3};
		try {
			System.out.println(arr[3]);
		}catch(Exception e){
			System.out.println(e);
		}
	}
}

/*
捕获异常finally
*/
public class DemoException{
	public static void main(String[] args) {
		int[] arr = {1, 2, 3};
		try {
			System.out.println(arr[3]);
		}catch(Exception e){
			System.out.println(e);
		}finally{
			System.out.println("异常处理结束,必须执行,一般用于资源回收");
		}
	}
}

  • throwable类中定义了3个异常处理方法

    • String getMessage(): 返回此throwable的简短描述
    • String toString(): 返回throwable的详细信息
    • void printStackTrace(): JVM打印异常对象,默认此方法,打印异常的错误流
  • 自定义异常类的写法

/*
	必须继承自Exception或者RuntimeException
*/
public DemoException extends Exception{
	// 父类空参构造方法
	public DemoException(){
		super();
	}
	// 重写构造方法
	public DemeException(String msg){
		super(msg);
	}
}

多线程

/*
java程序属于抢占式调度
Thread类的常用方法:
	1.String getName()  返回该线程的名称
	2.static Thread currentThread()  获取当前线程对象
	3.void setName()  设置线程的名称
	4.static void sleep(Long millis)  使当前线程休眠xxx毫秒
*/

// 1.创建Runnable接口实现类
public class RunnableImpl implements Runnable{
	// 2.在实现类中重写Runnable接口的run方法,设置线程任务
	@override
	public void run() {
		for (int i=0; i<10000; i++) {
			System.out.println(Thread.currentThread().getName() + "---" + i);
		}
	}
}


public class ThreadDemo{
	public static void main(String[] args){
		// 3.创建RunnableImpl接口实现类对象
		RunnableImpl demo = new RunnableImpl();
		// 4.创建Thread类对象,将Runnable对象传入
		Thread t = new Thread(demo);
		// 5.调用thread类中的start方法,即可执行Runnable中的run方法
		t.start();

		// 此处创建一个main主线程来演示线程抢夺执行的情况
		for (int i = 0; i < 1000; i++) {
			System.out.println(Thread.currentThread().getName()+"--"+i);
		}
	}
}


/*
匿名内部类的方式创建多线程
匿名内部类:
	简化代码,把子类继承父类,重写父类方法,创建子类对象合成一步完成
	把实现类接口,重写接口中的方法,创建实现类对象合成一步完成
	格式:
		new 父类/接口() {
			重写父类/接口中的方法
	}
*/
public class InnerClassThreadDemo {
	public static void main(String[] args){ 
		// 最传统的方法
		new Thread(){
			@override
			public void run() {
				for (int i = 0; i<20; i++) {
					System.out.println(n);
				}
			}
		}.start();

		// Runnable接口实现类方法
		Runnable r = new Runnable() {
			@override
			public void run() {
				for ...
			}
		}
		new Thread(r).start();

		// 最简单的方法
		new Thread(new Runnable() {
			@override
			public void run() {
				for ...
			}
		}).start;
	}
}

你可能感兴趣的:(java学习记录)