本次PTA做業題集異常、多線程java
答:只有try執行後纔會執行finally,不管在try塊的哪一個地方返回,finally塊中的代碼必定會被執行。4-2這裏用resource.close();釋放資源,並且finally中的異常要在finally中用try/catch捕獲。百度查到說finally中最好不要使用return,若是finally中return了, 則執行finally中的reutrn 語句,而後程序結束,不會執行try塊中的return。連接:http://1194867672-qq-com.iteye.com/blog/1571561
答:該題只是對以前的代碼進行了修改: 若是棧滿,if(top== arr.length){ throw new FullStackException();}拋出棧滿的異常。 若是棧空,if(top==0){ throw new EmptyStackException();}拋出棧滿的異常 把之前代碼的arr[]改爲題目中的arrStack[]就好了
答:自定義異常,須要繼承已有的父類。Checked Exception繼承Exception類,Uncheck Exception繼承RuntimeException類。自定義的異常在拋出時要明確告訴用戶異常的緣由,好比:throw new IllegalNameException("the first char of name must not be digit, name="+name);
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.NoSuchElementException; import java.util.Scanner; public class ReadFile201521123103 { public static void main(String[] args) throws FileNotFoundException{ Scanner in = null; ArrayList<User> student=new ArrayList<User>(); try{ int count = 0; in = new Scanner(new File("身份證號.txt"));//爲myfile.txt這個File建立一個掃描器in while(in.hasNextLine()){ String line = in.nextLine();//讀出myfile.txt的下一行 count++; @SuppressWarnings("resource") Scanner lineScanner = new Scanner(line);//爲每一行創建一個掃描器 //System.out.println("lineScanner="+lineScanner); lineScanner.useDelimiter(" ");//使用空格做爲分隔符 //System.out.println(line); String a1 = lineScanner.next();//姓名 String a2 = lineScanner.next();//身份證號 String a3 = lineScanner.next();//性別 try{ if(a1.length() == 0 || a1.length() > 3) { throw new IllegalArgumentException("a1.length = " + a1.length()); } if(a2.length() == 0) { throw new IllegalArgumentException("a2.length = " + a2.length()); } if(!a3.equals("男") && !a3.equals("女")) { throw new IllegalArgumentException("性別出錯:"+a3); } String a4 = lineScanner.next();//年齡 String a5 = lineScanner.next();//地址 while(lineScanner.hasNext()){//謹防地址只有一段 a5 += lineScanner.next(); } student.add(new User(a1,a2,a3,Integer.parseInt(a4),a5)); }catch(IllegalArgumentException e) { System.out.println("拋出異常:"+e+"。錯誤在文件的第"+count+"行:"+line); } catch(NoSuchElementException e) { System.out.println("拋出異常:"+e+"。錯誤在文件的第"+count+"行:"+line); } } Collections.sort(student,new Comparator<User>(){ @Override public int compare(User o1, User o2) { return o1.getAge()-o2.getAge(); } }); for (User user : student) { System.out.println(user.toString()); } }catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println(e); } finally { if (in != null) { in.close(); } } } } class User{ private String name; private String id; private String gender; private int age; private String address; public User(String name, String id, String gender, int age, String address) { super(); this.name = name; this.id = id; this.gender = gender; this.age = age; this.address = address; } @Override public String toString() { return "User [name=" + name + ", id=" + id + ", gender=" + gender + ", age=" + age + ", address=" + address + "]"; } public int getAge() { // TODO Auto-generated method stub return age; } }
答:(1)在正確的地方設置斷點(2)啓動調試模式,Eclipse有一個專門的debug perspective(3)運行程序,使用F五、F六、F7快捷鍵進行調試(4)查看狀態值
答:F5:Step into 跳入函數或方法 調試某個函數內部的代碼,進入函數內部要使用F5快捷鍵 F6: Step over 跳過 一行一行調試,不進入函數內部 F7: Step return 跳出某函數
答:使用System.out.println輸出值來進行調試。
import java.util.Arrays; public class Main4 { public static void main(String[] args){ // TODO Auto-generated method stub final String mainThreadName = Thread.currentThread().getName(); Thread t1 = new Thread(()->{ System.out.println(mainThreadName); System.out.println(Thread.currentThread().getName()); System.out.println(Arrays.toString(Thread.class.getInterfaces())); }); t1.start(); } }
答:3-1 守護線程:用t1.setDaemon(true);使線程成爲守護線程,若是全部前臺線程死亡,守護線程自動結束。 3-2 線程讓步:用t1.join()方法讓處於運行狀態的線程調用另外一個線程,等其餘線程執行完再執行本線程。 4-1 編寫MyThread類繼承自Thread,按題目要求編寫public void run()方法覆蓋Thread類的run()方法。 4-2 這個題錯了好屢次,一直說沒有終止線程,問同窗,要捕獲異常來終止try{Thread.sleep(1); } catch(InterruptedException e){ }還有題目要求對每一個傳入的word只能檢查一遍。因此要if(word!=null) this.word=null; 4-3 PPT中和Runnable與任務後的一個選擇題同樣,用到匿名內部類
答:BallRunnable是支持多線程的類,它實現了Runnable接口,該類調用小球移動的方法move(),生成小球的位置移動,利用repaint()對界面進行重畫,用Thread.sleep()使其睡眠一小段時間,造成小球移動的現象。
答:getShape()得到小球的大小和move(Rectangle2D bounds)小球移動的位置座標變化,小球移動。
答:add(Ball b)加小球和paintComponent(Graphics g)畫小球。
答:點擊start按鈕的時候,調用addBall()方法,加入一個小球,啓動一個新線程。
答;創建一個圖形界面,點擊start按鈕就出現一個小球,而後讓小球移動,等小球移動到必定位置中止,再次點擊start按鈕,增長一個小球,重複以前操做。
答:一個用戶登陸能夠當作一個線程,當多個用戶一塊兒購物時,就啓動了多個線程,也就是多線程問題。
題目集:異常、多線程(3-1, 3-2, 4-1, 4-2, 4-3)git
在碼雲的項目中,依次選擇「統計-Commits歷史-設置時間段」, 而後搜索並截圖多線程