不知不覺,心又飄了起來。整個人也很輕。什麼也做不下去,什麼也不想做。總想著應該得到更多的回報,卻一直沒有付出更多的努力。
看演算法,看資料結構。看groovy,看nodejs,看jvm。什麼都看,卻懶於動手實踐。進展緩慢。
最近,在關注nodejs的應用。也在github上fork了幾個項目,希望能夠學到想學的東西。
今天,看了電影《最長的一日》。
寫了生產者消費者的程式。雖然想象中很簡單,但是,實現起來,還是把一些細節搞錯了。還好,最終搞定了,也搞明白了問題之所在。也算是今天的一點小收穫吧。
package test.multithread;public class MultiThreads {static int cnt = 0;static class Counter {private int cnt = 0;synchronized void inc() {cnt ++;}synchronized int get() {return cnt;}}public static void main(String[] args) {final Counter counter = new Counter();Thread producer = new Thread(new Runnable(){@Overridepublic void run() {while(true) {synchronized(counter) {counter.inc();int value = counter.get();System.out.println("producer : " + value);counter.notifyAll();try {counter.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (value >= 1000) {break;}}}System.out.println("producer exited");}});Thread consumer = new Thread(new Runnable(){@Overridepublic void run() {while(true) {synchronized(counter) {int value = counter.get();System.out.println("consumer : " + value);counter.notifyAll();if (value >= 1000) {break;}try {counter.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}System.out.println("consumer exited.");}});producer.start();consumer.start();}}
希望明天能把它用lock改寫一下。用clojure的STM改寫一下。
今天看到一個似曾相識的演算法題,就是不知怎麼搞了。翻到舊文章一看,一模一樣。太尷尬了。竟然一點印象都沒有了。
少說話,尤其是牢騷很多的時候,心情不好的時候。
心平氣和。