Java 线程第三版 第八章 Thread与Collection Class 读书笔记
一、Collection Class的概述
二、同步与Collection Class
import java.util.*; public class CharacterEventHandler { private Vector listeners = new Vector(); public void addCharacterListener(CharacterListener cl) { listeners.add(cl); } public void removeCharacterListener(CharacterListener cl) { listeners.remove(cl); } public void fireNewCharacter(CharacterSource source, int c) { CharacterEvent ce = new CharacterEvent(source, c); CharacterListener[] cl = (CharacterListener[] ) listeners.toArray(new CharacterListener[0]); for (int i = 0; i < cl.length; i++) cl[i].newCharacter(ce); } }
import java.util.*; public class CharacterEventHandler { private ArrayList listeners = new ArrayList(); public synchronized void addCharacterListener(CharacterListener cl) { listeners.add(cl); } public synchronized void removeCharacterListener(CharacterListener cl) { listeners.remove(cl); } public synchronized void fireNewCharacter(CharacterSource source, int c) { CharacterEvent ce = new CharacterEvent(source, c); CharacterListener[] cl = (CharacterListener[] ) listeners.toArray(new CharacterListener[0]); for (int i = 0; i < cl.length; i++) cl[i].newCharacter(ce); } }
import java.util.*; public class CharacterEventHandler { private ArrayList listeners = new ArrayList(); public void addCharacterListener(CharacterListener cl) { synchronized(listeners) { listeners.add(cl); } } public void removeCharacterListener(CharacterListener cl) { synchronized(listeners) { listeners.remove(cl); } } public void fireNewCharacter(CharacterSource source, int c) { CharacterEvent ce = new CharacterEvent(source, c); CharacterListener[] cl; synchronized(listeners) { cl = (CharacterListener[] ) listeners.toArray(new CharacterListener[0]); } for (int i = 0; i < cl.length; i++) cl[i].newCharacter(ce); } }
import java.util.*; import javax.swing.*; import javax.swing.table.*; public class CharCounter { public HashMap correctChars = new HashMap(); public HashMap incorrectChars = new HashMap(); private AbstractTableModel atm; public void correctChar(int c) { synchronized(correctChars) { Integer key = new Integer(c); Integer num = (Integer) correctChars.get(key); if (num == null) correctChars.put(key, new Integer(1)); else correctChars.put(key, new Integer(num.intValue() + 1)); if (atm != null) atm.fireTableDataChanged(); } } public int getCorrectNum(int c) { synchronized(correctChars) { Integer key = new Integer(c); Integer num = (Integer) correctChars.get(key); if (num == null) return 0; return num.intValue(); } } public void incorrectChar(int c) { synchronized(incorrectChars) { Integer key = new Integer(c); Integer num = (Integer) incorrectChars.get(key); if (num == null) incorrectChars.put(key, new Integer(-1)); else incorrectChars.put(key, new Integer(num.intValue() - 1)); if (atm != null) atm.fireTableDataChanged(); } } public int getIncorrectNum(int c) { synchronized(incorrectChars) { Integer key = new Integer(c); Integer num = (Integer) incorrectChars.get(key); if (num == null) return 0; return num.intValue(); } } public void addModel(AbstractTableModel atm) { this.atm = atm; } }
三、生产者/消费者模式
import java.util.*; import java.util.concurrent.*; public class FibonacciProducer implements Runnable { private Thread thr; private BlockingQueue<Integer> queue; public FibonacciProducer(BlockingQueue<Integer> q) { queue = q; thr = new Thread(this); thr.start(); } public void run() { try { for(int x=0;;x++) { Thread.sleep(1000); queue.put(new Integer(x)); System.out.println("Produced request " + x); } } catch (InterruptedException ex) { } } }
import java.util.concurrent.*; public class FibonacciConsumer implements Runnable { private Fibonacci fib = new Fibonacci(); private Thread thr; private BlockingQueue<Integer> queue; public FibonacciConsumer(BlockingQueue<Integer> q) { queue = q; thr = new Thread(this); thr.start(); } public void run() { int request, result; try { while (true) { request = queue.take().intValue(); result = fib.calculateWithCache(request); System.out.println("Calculated result of " + result + " from " + request); } } catch (InterruptedException ex) { } } }
四、使用Collection Class
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。