目录
1.异常处理
1.1.异常
1.2.捕获与声明的要求
1.3.处理异常
import java.io.*;
import java.util.Vector;
class ListofNumbers{
private Vector<Integer> victor; //创建向量victor
final int size=10;
public ListofNumber(){ //构造方法
int i;
victor=new Vector<>(size); //设定向量victor的容量
for(i=0;i<size;i++)
victor.addElement(new Integer(i)); //在victor末尾添加对象
}
public void writeList(){
PrintStream pStr=null;
System.err.println("Entering try statement");
int i;
pStr=new PrintStream(new BufferedOutputStream(new FileOutputSream("OutFile.txt")));
for(i=0;i<size;i++)
pStr.println("Value at:"+i+"="+victor.elementAt(i)); //写入OutFile.txt
pStr.close();
}
public static void main(String[] args){
ListOfNumbers lofn=new ListOfNumbers();
lofn.writeList();
}
}
1.3.1.try块
1.3.2.catch块
try{
···
}catch(ArrayIndexOutOfBoundsException e){
System.err.println("Caught ArrayIndexOutOfBoundsException:"+e.getMessage());
}catch(IOException e){
System.err.println("Caught IOException:"+e.getMessage());
}
catch(ArrayIndexOutOfBoundsException|IOException e){
System.err.println("Caught Exception:"+e.getMessage());
}
1.3.3.finally块
import java.io.*;
import java.util.Vector;
class ListofNumbers{
private Vector<Integer> victor; //创建向量victor
final int size=10;
public ListofNumber(){ //构造方法
int i;
victor=new Vector<>(size); //设定向量victor的容量
for(i=0;i<size;i++)
victor.addElement(new Integer(i)); //在victor末尾添加对象
}
public void writeList(){
PrintStream pStr=null;
try{
System.err.println("Entering try statement");
int i;
pStr=new PrintStream(new BufferedOutputStream(new FileOutputSream("OutFile.txt")));
for(i=0;i<size;i++)
pStr.println("Value at:"+i+"="+victor.elementAt(i)); //写入OutFile.txt
}catch(ArrayIndexOutOfBoundsException e){
System.err.println("Caught ArrayIndexOutOfBoundsException:"+e.getMessage());
}catch(IOException e){
System.err.println("Caught IOException:"+e.getMessage());
}finally{
if(pStr!=null){
System.err.println(Closing PrintStream);
pStr.close();
}else{
System.err.println(PrintStream not open);
}
}
}
public static void main(String[] args){
ListOfNumbers lofn=new ListOfNumbers();
lofn.writeList();
}
}
1.3.4.新形式的try块语句
try(PrintStream pStr=new PrintStream(new BufferedStream(new
FileOutputStream("OutFile.txt")))){
int i;
System.err.println("Entering try statement");
for(i=0;i<size;i++)
pStr.println("Value at:"+i+"="+victor.elementAt(i)); //写入OutFile.txt
}catch(ArrayIndexOutOfBoundsException e){
System.err.println("Caught ArrayIndexOutOfBoundsException:"+e.getMessage());
}catch(IOException e){
System.err.println("Caught IOException:"+e.getMessage());
}
1.4.抛出异常
1.4.1.throws子句
public void writeList() throws IOException,ArrayIndexOutOfBoundsException{
···
}
public static void main(String[] args){
ListOfNumbers lofn=new ListOfNumbers();
try{
lofn.writeList();
}catch(IOException e){
System.err.println("Caught IOException:"+e.getMessage());
}
}
1.4.2.throw子句
1.5.创建自己的Exception类
class LinkedListException extends Exception{
private static final long serialVersionUID=1L;
public LinkedListException(){
super();
}
public LinkedListException(String s){
super(s);
}
}
class List{
Object[] items;
int size=0;
public List(int len){
items=new Object[len];
}
public void addElement(Object o) throws LinkedListException{
if(size==items.lenght)throw LinkedListException("列表已满!");
else{
items[size]=o;
size++;
}
}
public Object getElement(int n) throws LinkedListException{
if(n<0||n>items.length)throw LinkedListException("列表下标越界!");
else return items[n];
}
public Obeject FirstElement() throws LinkedListException{
if(items.length==0)throw LinkedListException("列表中无任何对象!");
//items.length==0感觉有点问题
else return items[0];
}
}
public class myLinkedList{
public static void main(String[] args){
List list=new List(2);
try{
list.addElement("广东");
list.addElement("浙江");
list.addElement("山东");
}catch(LinkedListException e){
System.err.println("err:"+e.getMessage());
}
try{
System.err.println(list.getElement(2));
}catch(LinkedListException e){
System.err.println("err:"+e.getMessage());
}
try{
System.err.println(list.FirstElement());
}catch(LinkedListException e){
System.err.println("err:"+e.getMessage());
}
}
}
//结果:
//err:列表已满
//err:列表下标越界
//广东
2.并发
2.1.线程
2.1.1.线程体
class className extends Thread{
public void run(){
···
}
}
class className implements Runnable{
public void run(){
···
}
}
class AThread extends Thread{
public AThread(String str){
super(str);
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(i+" "+getName());
try{
sleep((int)(Math.random()*1000)); //休眠0到1000毫秒/1秒的随机时间间隔
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
System.out.println("END!"+getName());
}
}
class MyThread{
public static void main(String[] args){
new AThread("Win").start();
new AThread("Lost").start();
}
}
2.1.2.线程的状态
import java.applet.Applet;
import java.awt.Graphics;
import java.util.*;
public class Clock extends Applet implements Runnable{
private static final long serialVersionUID=1L; //定义程序序列化ID
Thread clockThread;
public void start(){
if(clockThread==null){
clockThread=new Thread(this,"Clock");
clockThread.start();
}
}
public void run(){
while(clockThread!=null){
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
}
public void paint(Graphics g){
Calendar now=new GregorianCalendar();
g.drawString(now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+
":"+now.get(Calendar.SECOND),5,10);
}
}
2.1.3.线程优先级
2.1.4.Daemon线程
2.1.5.线程组
2.1.6.ThreadGroup类
2.2.同步与锁定
2.2.1.监视器
class Producer extends Thread{
private Store store;
public Producer(Store s){
store=s;
}
public void run(){
for(int i=0;i<10;i++){
store.put(i);
System.out.println("Producer:"+i);
try{
sleep((int)(Math.random()*100));
}catch(InterruptedException e){System.out.println(e.getMessage());}
}
}
}
class Comsumer extends Thread{
private Store store;
public Comsumer(Store s){
store=s;
}
public void run(){
int value=0;
for(int i=0;i<10;i++){
value=store.get();
System.out.println("Comsumer:"+value);
}
}
}
public class MyProCon{
public static void main(String[] args){
Store s=new Store();
Producer p1=new Producer(s);
Comsumer p2=new Comsumer(s);
p1.start();
p2.start();
}
}
class Store{
private int sep;
private boolean available=false;
public synchronized int get(){
while(available==false){
try{
wait();
}catch(InterruptedException e){System.out.println(e.getMessage());}
}
available=false;
notify();
return sep;
}
public synchronized void put(int value){
while(available==true){
try{
wait();
}catch(InterruptedException e){System.out.println(e.getMessage());}
}
available=true;
sep=value;
notify();
}
}