import java.io.IOException; import static java.lang.System.out; public class MemberDemo { public static void main(String[] args) throws IOException { Member[] members = { new Member("B1234", "Justin", 90), new Member("B5678", "Monica", 95), new Member("B9876", "Irene", 88) }; for(Member member : members) { member.save(); } out.println(Member.load("B1234")); out.println(Member.load("B5678")); out.println(Member.load("B9876")); } }
運行結果:
html
在Java中,對串流能夠有裝飾器,那麼對字符串也有裝飾器書中介紹了InputStreamREader與OutputStreamWriter,BufferedReader與BufferedWriter和PrintWriter這三種打包器。
1.InputStreamREader與OutputStreamWriter:
在創建InputStreamREader與OutputStreamWriter時能夠指定編碼,對字節數據轉化爲相應的編碼字符。
2.BufferedReader與BufferedWriter:
BufferedReader與BufferedWriter能夠將轉換後的數據作緩衝處理,以增長讀取效率。
3.PrintWriter:
PrintWriter與PrintStreame十分類似既能夠對OutputStream打包,也能夠對Writer打包。java
在java中,若是想在main()之外獨立設計流程,能夠撰寫類操做java.lang.Runnable接口,流程的進入點是操做在run()方法中。api
package Thread; import static java.lang.System.out; public class TortoiseHareRace { public static void main(String[] args) { boolean[] flags = {true, false}; int totalStep = 10; int tortoiseStep = 0; int hareStep = 0; out.println("龜兔賽跑開始..."); while(tortoiseStep < totalStep && hareStep < totalStep) { tortoiseStep++; out.printf("烏龜跑了 %d 步...%n", tortoiseStep); boolean isHareSleep = flags[((int) (Math.random() * 10)) % 2]; if(isHareSleep) { out.println("兔子睡着了zzzz"); } else { hareStep += 2; out.printf("兔子跑了 %d 步...%n", hareStep); } } } }
它只有一個流程,它的循環控制爲烏龜或兔子走完十步,比賽結束。按照程序由上而下的運行原則,每次都是烏龜先遞增步數,而後再是兔子隨機睡覺或走兩步,二者好像並非同步的,這樣對兔子來講並不公平,因此,爲了設計一個更加公平的比賽程序,咱們但願二者能夠不受程序運行過程當中前後運行順序的干擾,同時進行步數的移動。這裏就用到了書中介紹到的多線程運行程序。多線程
package Thread; import static java.lang.System.out; public class TortoiseHareRace { public static void main(String[] args) { boolean[] flags = {true, false}; int totalStep = 10; int tortoiseStep = 0; int hareStep = 0; out.println("龜兔賽跑開始..."); while(tortoiseStep < totalStep && hareStep < totalStep) { tortoiseStep++; out.printf("烏龜跑了 %d 步...%n", tortoiseStep); boolean isHareSleep = flags[((int) (Math.random() * 10)) % 2]; if(isHareSleep) { out.println("兔子睡着了zzzz"); } else { hareStep += 2; out.printf("兔子跑了 %d 步...%n", hareStep); } } } }
Condition接口用來搭配Lock,最基本用法就是達到Object的wait()、notify()、notifyAll()方法的做用。dom
class Resource { private String name; private int resource; Resource(String name, int resource) { this.name = name; this.resource = resource; } String getName() { return name; } synchronized int doSome() { return ++resource; } synchronized void cooperate(Resource resource) { resource.doSome(); System.out.printf("%s 整合 %s 的資源%n", this.name, resource.getName()); } } public class DeadLockDemo { public static void main(String[] args) { Resource resource1 = new Resource("resource1", 10); Resource resource2 = new Resource("resource2", 20); Thread thread1 = new Thread(() -> { for (int i = 0; i < 10; i++) { resource1.cooperate(resource2); } }); Thread thread2 = new Thread(() -> { for (int i = 0; i < 10; i++) { resource2.cooperate(resource1); } }); thread1.start(); thread2.start(); } }
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 200/200 | 1/2 | 20/20 | |
第二週 | 300/500 | 1/3 | 18/38 | |
第三週 | 500/1000 | 1/4 | 22/60 | |
第四周 | 300/1300 | 1/5 | 30/90 | |
第五週 | 300/1600 | 1/6 | 30/160 | |
第六週 | 700/2300 | 2/7 | 30/190 |