Java学习笔记 CH7异常处理
1.程序错误:
异常:阻止当前方法或作用域继续执行的问题。文件名找不到等等
错误:用于指示合理的应用程序不应该试图捕获的严重问题。JVM崩溃等等
2.异常对象:存放异常消息的对象
3.异常处理:
main->fun0->fun1->fun2->throw exception
->找fun2处理->找fun1处理->找fun0处理->找main处理->中断
4.常见exception
ArithmeticException :RuntimeException,如除数为零
ArrayIndexOutOfBoundsException
ArrayStoreException
IOException
FileNotFoundException
NullPointerException :RuntimeException,如尝试读取的引用被删除
MalformedURLException
NumberFormatException
5.该函数只跑出异常,自己不处理
public fun(String s)throws IOException,ArithmeticException{
if(...)
throw new IOException();
else if(...)
throw new ArithmeticException();
}
6.子类覆盖父类方法不能跑出更多异常
7.非必检异常:RuntimeException,Error
必检异常 :其他
8.try{
throw new Exception();
}
catch (Exception e){
...
}
try一旦跑出异常,throw后面的程序均不执行
catch语句组,具有先后顺序,所以判断条件先特殊后一般,最后 Exception
9.finally与try匹配,无论catch能否捕捉到异常,均质性,无论try是否抛出异常,均执行。
try{
throw new ...();
}
catch(...){}
catch(...){}
catch(...){}
finally{...}//一定执行
Java语言学习 Ch8线程
1.程序:静态文件->进程:执行中的程序->线程:运行与摸个进程中,完成某个具体任务
2.定义线程:extends Thread,implements Runnable
3.继承Thread类或者Ruannble接口
4.线程生存周期
创建线程->start()->就绪->运行->消亡;
运行->sleep()->休眠->sleep()结束->运行
运行->wait()->等待->notify()->运行
运行->IO阻塞->IO阻塞->IO可用->运行
运行->suspend()->挂起->resume()->运行
5.优先级1~10,默认5,setPriority(7)
6.yield()自己放弃CPU,但有可能立即重新获得
7.synchronized(this、类名、value)
{...}当()中相同时不能运行,缺省this
例子:
//CubbyHole.java
1.程序错误:
异常:阻止当前方法或作用域继续执行的问题。文件名找不到等等
错误:用于指示合理的应用程序不应该试图捕获的严重问题。JVM崩溃等等
2.异常对象:存放异常消息的对象
3.异常处理:
main->fun0->fun1->fun2->throw exception
->找fun2处理->找fun1处理->找fun0处理->找main处理->中断
4.常见exception
ArithmeticException :RuntimeException,如除数为零
ArrayIndexOutOfBoundsException
ArrayStoreException
IOException
FileNotFoundException
NullPointerException :RuntimeException,如尝试读取的引用被删除
MalformedURLException
NumberFormatException
5.该函数只跑出异常,自己不处理
public fun(String s)throws IOException,ArithmeticException{
if(...)
throw new IOException();
else if(...)
throw new ArithmeticException();
}
6.子类覆盖父类方法不能跑出更多异常
7.非必检异常:RuntimeException,Error
必检异常 :其他
8.try{
throw new Exception();
}
catch (Exception e){
...
}
try一旦跑出异常,throw后面的程序均不执行
catch语句组,具有先后顺序,所以判断条件先特殊后一般,最后 Exception
9.finally与try匹配,无论catch能否捕捉到异常,均质性,无论try是否抛出异常,均执行。
try{
throw new ...();
}
catch(...){}
catch(...){}
catch(...){}
finally{...}//一定执行
10.
class MyException extends Exception{
private int detail;
MyException(int a)
{
detail = a;
}
public String toString(){
return "MyException"+detail;
}
}
Java语言学习 Ch8线程
1.程序:静态文件->进程:执行中的程序->线程:运行与摸个进程中,完成某个具体任务
2.定义线程:extends Thread,implements Runnable
3.继承Thread类或者Ruannble接口
class PrintThread extends Thread{ //或者改成implements Runnable
private int sleepTime; //改成implements Runnable此处加private String name
public PrintThread(String name){
super(name); //改成implements Runnable此处改为this.name = name
sleepTime = (int)(Math.random()*5000);
System.out.println("Name:"+getName()+";sleep:"+sleepTime);//改成implements Runnable此处改为System.out.println("Name:"+name+";sleep:"+sleepTime);
}
public void run(){
try{
System.out.println(getName()+"going to sleep"); //改成implements Runnable此处改为System.out.println(name+"going to sleep");
Thread.sleep(sleepTime);
}
catch(InterruptedException ie){
System.err.println(ie.toString());
}
}
public static void main(String []args){
PrintThread thread1,thread2,thread3;
thread1 = new PrintThread("thread 1");
thread2 = new PrintThread("thread 2");
thread3 = new PrintThread("thread 3");
thread1.start(); //改成implements Runnable此处改为new Thread(thread1).start();
thread2.start(); //改成implements Runnable此处改为new Thread(thread2).start();
thread3.start(); //改成implements Runnable此处改为new Thread(thread3).start();
}
}
4.线程生存周期
创建线程->start()->就绪->运行->消亡;
运行->sleep()->休眠->sleep()结束->运行
运行->wait()->等待->notify()->运行
运行->IO阻塞->IO阻塞->IO可用->运行
运行->suspend()->挂起->resume()->运行
5.优先级1~10,默认5,setPriority(7)
6.yield()自己放弃CPU,但有可能立即重新获得
7.synchronized(this、类名、value)
{...}当()中相同时不能运行,缺省this
例子:
//CubbyHole.java
public class CubbyHole{
private int data;
private boolean getable;
public synchronized int get(){
while(getable == false)
{
try{
wait();
}
catch(InterruptedException e){
...
}
}
getable = false;
notifyAll();
return this->data;
}
public synchronized void put(int data){
while(getable == true){
try{
wait();
}catch(Interrupted Exception e){
}
}
this.data = data;
getable = true;
notifyAll();
}
}
//Producer.java
public class Producer extends Thread{
private CubbyHoles;
public Producer (CubbyHoles res){
this.res = res;
}
public void run(){
for(int i =0;i<10;i++){
res.put(i);
System.out.println("Producer put: "+i);
try{
sleep(100);
}catch(InterruptedException e){
}
}
}
}
//Consumer.java
public class Consumer extends Thread{
private CubbyHole res;
public Consumer(CubbyHole res){
this.res = res;
}
public void run(){
for(int i = 0;i<10;i++){
System.out.println("Consumer get: "+res.get());
try{
sleep((int)(Math.random()*100));
}catch(InterruptedException e){
}
}
}
}
//ProdConsTest.java
public class ProdConsTest {
public static void main(String[] args) {
CubbyHole res = new CubbyHole();
Producer pro = new Producer(res);
Consumer con = new Consumer(res);
pro.start();
con.start();
}
}
8.所有线程均属于某个线程组,缺省为main
ThreadGroup myGroup = new ThreadGroup("MyGroup");
Thread thread1 = new Thread(myGroup,"thread One");
Thread thread2 = new Thread(myGroup,"thread Two");
int a = myGroup.activeCount();
myGroup.stop();
myGroup.suspend();
myGroup.resume();
myGroup.setMaxPrioity(MAX_PRIORITY/MIN_PRIORITY/NORM_PRIORITYT);