プログラミングC 第9回 例外 スレッド 白石路雄 <shira@is.sci.toho-u.ac.jp>
2
finally try{ ( 例外が発生するかもしれない処理 ) catch(exception のクラス名 e){ ( 例外が発生した時の処理 ) finally{ ( 例外の発生の有無に関わらず 必ず行う処理 ) 3
Integer.parseInt() NumberFormatException 4
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readline(); int value = Integer.parseInt(str); Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:65) at java.lang.integer.parseint(integer.java:492) at java.lang.integer.parseint(integer.java:527) at NumberFormatExceptionTest.main(NumberFormatExceptionTest.java:10) 5
NumberFormatException BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readline(); try{ int value = Integer.parseInt(str); catch(numberformatexception e){ System.out.println("Error!"); 6
public class KeyboardReader{ public int readinteger(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readline(); int value = 0; try{ value = Integer.parseInt(str); catch(numberformatexception e){ System.out.println("Error!"); return value; 7
String readline IOException public class KeyboardReader{ public int readinteger(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readline(); int value = 0; try{ value = Integer.parseInt(str); catch(numberformatexception e){ System.out.println("Error!"); return value; 8
try-catch-finally throws 9
public class KeyboardReader{ public int readinteger(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int value = 0; try{ String str = br.readline(); value = Integer.parseInt(str); catch(numberformatexception e){ System.out.println("Error!"); catch(ioexception e){ System.out.println("Error!"); return value; 10
public class KeyboardReader{ public int readinteger() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readline(); int value = 0; try{ value = Integer.parseInt(str); catch(numberformatexception e){ System.out.println("Error!"); return value; 11
Exception throw 12
13
Thread Runnable 14
Runnable public void run() public class Life implements Runnable{ public void run(){ String[] s = {"Eat", "Play", "Nap"; for(int i=0; i<6; i++){ try{ System.out.println("Life: " + s[i%3]); Thread.sleep(800); catch(interruptedexception e){ 15
Runnable Thread Thread start() 16
public class Main{ public static void main(string[] args){ Life l = new Life(); Thread t = new Thread(l); t.start(); for(int i=0; i<8; i++){ try{ System.out.println("Main: Work"); Thread.sleep(600); catch(interruptedexception e){ 17
Main: Work Life: Eat Main: Work Life: Play Main: Work Life: Nap Main: Work Life: Eat Main: Work Main: Work Life: Play Main: Work Life: Nap Main: Work 18
join() 19
join() public class Main{ public static void main(string[] args){ Life l = new Life(); Thread t = new Thread(l); t.start(); for(int i=0; i<8; i++){ try{ if(i==4) t.join(); System.out.println("Main: Work"); Thread.sleep(600); catch(interruptedexception e){ 20
Main: Work Life: Eat Main: Work Life: Play Main: Work Life: Nap Main: Work Life: Eat Life: Play Life: Nap Main: Work Main: Work Main: Work Main: Work 21
consumecard() public class RareCardServer{ public int count = 100; public void consumecard(){ int currrentcount = count; // データベースへの接続などにかかる時間を // シミュレートするため 100 ミリ秒停止 try{thread.sleep(100); catch(interruptedexception e){ count = currentcount - 1; 22
public class RareCardConsumer implements Runnable{ private RareCardServer server; public RareCardConsumer(RareCardServer server){ this.server = server; public void run(){ // 50 枚適当に寝ながら使う for(int i=0; i<50; i++){ server.consumecard(); try{thread.sleep((int)(math.random()*100)); catch(intteruptedexception e){ System.out.println("50 枚つかったぜー "); 23
RareCardConsumer public class RareCardTest{ public static void main(string[] args){ RareCardServer s = new RareCardServer(); Thread t1 = new Thread(new RareCardConsumer(s)); Thread t2 = new Thread(new RareCardConsumer(s)); t1.start(); t2.start(); try{ t1.join(); t2.join(); catch(intteruptedexception e){ System.out.println("Server に残ったカードの枚数 :"+s.count); 24
50 枚つかったぜー 50 枚つかったぜー Server に残ったカードの枚数 :47 25
consumecard() count currentcount consumecard() count currentcount int currrentcount = count; try{thread.sleep(100); catch(interruptedexception e){ count = currentcount - 1; 26
count count (;_;) int currrentcount = count; try{thread.sleep(100); catch(interruptedexception e){ count = currentcount - 1; 27
synchronized public class RareCardServer{ public int count = 100; public synchronized void consumecard(){ int currrentcount = count; // データベースへの接続などにかかる時間を // シミュレートするため 100 ミリ秒停止 try{thread.sleep(100); catch(interruptedexception e){ count = currentcount - 1; 28
google 29
ExceptionTest.java exit 3.2 exit2 exit 30
FileTest.java FileWatchThread 31
test.txt test.txt 監視するファイル名を指定してください > test.txt ファイルをチェックしています 最初のサイズは 3 バイトです ファイルが更新されました サイズは 7 バイトです ファイルが更新されました サイズは 9 バイトです 32
// import java.io.*; として 以下のようにして // ファイルを表すクラスのインスタンスを作成します File file = new File("test.txt"); // ファイルサイズの取得は以下のように行います long size = file.length(); // ファイルの更新時刻 ( エポック からの経過時間をミリ秒単位で表したもの ) の取得は以下のように行います long lastmodified = file.lastmodified(); 33