标签:class blog code java tar ext
import javax.swing.*; import java.awt.event.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import javathreads.examples.ch05.*; public class ScoreLabel extends JLabel implements CharacterListener { private AtomicInteger score = new AtomicInteger(0); private AtomicInteger char2type = new AtomicInteger(-1); private AtomicReference<CharacterSource> generator = null; private AtomicReference<CharacterSource> typist = null; public ScoreLabel (CharacterSource generator, CharacterSource typist) { this.generator = new AtomicReference(generator); this.typist = new AtomicReference(typist); if (generator != null) generator.addCharacterListener(this); if (typist != null) typist.addCharacterListener(this); } public ScoreLabel () { this(null, null); } public void resetGenerator(CharacterSource newGenerator) { CharacterSource oldGenerator; if (newGenerator != null) newGenerator.addCharacterListener(this); oldGenerator = generator.getAndSet(newGenerator); if (oldGenerator != null) oldGenerator.removeCharacterListener(this); } public void resetTypist(CharacterSource newTypist) { CharacterSource oldTypist; if (newTypist != null) newTypist.addCharacterListener(this); oldTypist = typist.getAndSet(newTypist); if (oldTypist != null) oldTypist.removeCharacterListener(this); } public void resetScore() { score.set(0); char2type.set(-1); setScore(); } private void setScore() { // This method will be explained later in chapter 7 SwingUtilities.invokeLater(new Runnable() { public void run() { setText(Integer.toString(score.get())); } }); } public void newCharacter(CharacterEvent ce) { int oldChar2type; // Previous character not typed correctly - 1 point penalty if (ce.source == generator.get()) { oldChar2type = char2type.getAndSet(ce.character); if (oldChar2type != -1) { score.decrementAndGet(); setScore(); } } // If character is extraneous - 1 point penalty // If character does not match - 1 point penalty else if (ce.source == typist.get()) { while (true) { oldChar2type = char2type.get(); if (oldChar2type != ce.character) { score.decrementAndGet(); break; } else if (char2type.compareAndSet(oldChar2type, -1)) { score.incrementAndGet(); break; } } setScore(); } } }
import java.awt.*; import javax.swing.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import javathreads.examples.ch05.*; public class AnimatedCharacterDisplayCanvas extends CharacterDisplayCanvas implements CharacterListener, Runnable { private AtomicBoolean done = new AtomicBoolean(true); private AtomicInteger curX = new AtomicInteger(0); private AtomicInteger tempChar = new AtomicInteger(0); private Thread timer = null; public AnimatedCharacterDisplayCanvas() { startAnimationThread(); } public AnimatedCharacterDisplayCanvas(CharacterSource cs) { super(cs); startAnimationThread(); } private void startAnimationThread() { if (timer == null) { timer = new Thread(this); timer.start(); } } public void newCharacter(CharacterEvent ce) { curX.set(0); tempChar.set(ce.character); repaint(); } protected void paintComponent(Graphics gc) { char[] localTmpChar = new char[1]; localTmpChar[0] = (char) tempChar.get(); int localCurX = curX.get(); Dimension d = getSize(); int charWidth = fm.charWidth(localTmpChar[0]); gc.clearRect(0, 0, d.width, d.height); if (localTmpChar[0] == 0) return; gc.drawChars(localTmpChar, 0, 1, localCurX, fontHeight); curX.getAndIncrement(); } public void run() { while (true) { try { Thread.sleep(100); if (!done.get()) { repaint(); } } catch (InterruptedException ie) { return; } } } public void setDone(boolean b) { done.set(b); } }
高级的atomic数据类型
import java.lang.*; import java.util.concurrent.atomic.*; public class AtomicDouble extends Number { private AtomicReference<Double> value; public AtomicDouble() { this(0.0); } public AtomicDouble(double initVal) { value = new AtomicReference<Double>(new Double(initVal)); } public double get() { return value.get().doubleValue(); } public void set(double newVal) { value.set(new Double(newVal)); } public boolean compareAndSet(double expect, double update) { Double origVal, newVal; newVal = new Double(update); while (true) { origVal = value.get(); if (Double.compare(origVal.doubleValue(), expect) == 0) { if (value.compareAndSet(origVal, newVal)) return true; } else { return false; } } } public boolean weakCompareAndSet(double expect, double update) { return compareAndSet(expect, update); } public double getAndSet(double setVal) { Double origVal, newVal; newVal = new Double(setVal); while (true) { origVal = value.get(); if (value.compareAndSet(origVal, newVal)) return origVal.doubleValue(); } } public double getAndAdd(double delta) { Double origVal, newVal; while (true) { origVal = value.get(); newVal = new Double(origVal.doubleValue() + delta); if (value.compareAndSet(origVal, newVal)) return origVal.doubleValue(); } } public double addAndGet(double delta) { Double origVal, newVal; while (true) { origVal = value.get(); newVal = new Double(origVal.doubleValue() + delta); if (value.compareAndSet(origVal, newVal)) return newVal.doubleValue(); } } public double getAndIncrement() { return getAndAdd((double) 1.0); } public double getAndDecrement() { return getAndAdd((double) -1.0); } public double incrementAndGet() { return addAndGet((double) 1.0); } public double decrementAndGet() { return addAndGet((double) -1.0); } public double getAndMultiply(double multiple) { Double origVal, newVal; while (true) { origVal = value.get(); newVal = new Double(origVal.doubleValue() * multiple); if (value.compareAndSet(origVal, newVal)) return origVal.doubleValue(); } } public double multiplyAndGet(double multiple) { Double origVal, newVal; while (true) { origVal = value.get(); newVal = new Double(origVal.doubleValue() * multiple); if (value.compareAndSet(origVal, newVal)) return newVal.doubleValue(); } } public int intValue() { return DoubleValue().intValue(); } public long longValue() { return DoubleValue().longValue(); } public float floatValue() { return DoubleValue().floatValue(); } public double doubleValue() { return DoubleValue().doubleValue(); } public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } public Double DoubleValue() { return value.get(); } public boolean isNaN() { return DoubleValue().isNaN(); } public boolean isInfinite() { return DoubleValue().isInfinite(); } public String toString() { return DoubleValue().toString(); } public int hashCode() { return DoubleValue().hashCode(); } public boolean equals(Object obj) { Double origVal = DoubleValue(); return ((obj instanceof Double) && (origVal.equals((Double)obj))) || ((obj instanceof AtomicDouble) && (origVal.equals(((AtomicDouble)obj).DoubleValue()))); } public int compareTo(Double aValue) { return Double.compare(doubleValue(), aValue.doubleValue()); } public int compareTo(AtomicDouble aValue) { return Double.compare(doubleValue(), aValue.doubleValue()); } }
import java.util.concurrent.atomic.*; public class AtomicScoreAndCharacter { public class ScoreAndCharacter { private int score, char2type; public ScoreAndCharacter(int score, int char2type) { this.score = score; this.char2type = char2type; } public int getScore() { return score; } public int getCharacter() { return char2type; } } private AtomicReference<ScoreAndCharacter> value; public AtomicScoreAndCharacter() { this(0, -1); } public AtomicScoreAndCharacter(int initScore, int initChar) { value = new AtomicReference<ScoreAndCharacter> (new ScoreAndCharacter(initScore, initChar)); } public int getScore() { return value.get().getScore(); } public int getCharacter() { return value.get().getCharacter(); } public void set(int newScore, int newChar) { value.set(new ScoreAndCharacter(newScore, newChar)); } public void setScore(int newScore) { ScoreAndCharacter origVal, newVal; while (true) { origVal = value.get(); newVal = new ScoreAndCharacter (newScore, origVal.getCharacter()); if (value.compareAndSet(origVal, newVal)) break; } } public void setCharacter(int newCharacter) { ScoreAndCharacter origVal, newVal; while (true) { origVal = value.get(); newVal = new ScoreAndCharacter (origVal.getScore(), newCharacter); if (value.compareAndSet(origVal, newVal)) break; } } public void setCharacterUpdateScore(int newCharacter) { ScoreAndCharacter origVal, newVal; int score; while (true) { origVal = value.get(); score = origVal.getScore(); score = (origVal.getCharacter() == -1) ? score : score-1; newVal = new ScoreAndCharacter (score, newCharacter); if (value.compareAndSet(origVal, newVal)) break; } } public boolean processCharacter(int typedChar) { ScoreAndCharacter origVal, newVal; int origScore, origCharacter; boolean retValue; while (true) { origVal = value.get(); origScore = origVal.getScore(); origCharacter = origVal.getCharacter(); if (typedChar == origCharacter) { origCharacter = -1; origScore++; retValue = true; } else { origScore--; retValue = false; } newVal = new ScoreAndCharacter(origScore, origCharacter); if (value.compareAndSet(origVal, newVal)) break; } return retValue; } }
import java.util.*; public abstract class Calculator { private static ThreadLocal<HashMap> results = new ThreadLocal<HashMap>() { protected HashMap initialValue() { return new HashMap(); } }; public Object calculate(Object param) { HashMap hm = results.get(); Object o = hm.get(param); if (o != null) return o; o = doLocalCalculate(param); hm.put(param, o); return o; } protected abstract Object doLocalCalculate(Object param); }
Java 线程第三版 第五章 极简同步技巧 读书笔记,布布扣,bubuko.com
标签:class blog code java tar ext
原文地址:http://blog.csdn.net/androiddevelop/article/details/31030709