Manabu Nakamura mondo@its.hiroshima-cu.ac.jp q w e e e for (int i = 0; i < N; i++) { calculators[i] = new Calculator(); calculators[i].run(); 70 JAVA PRESS Vol.30
import java.math.biginteger; public class Calc1 extends Thread { private final int N = 1000; private BigInteger total = BigInteger.ZERO; public static void main(string[] args) { new Calc1().start(); y t % javac Calc1.java % java Calc1 total = 101785 % java Calc1 total = 100762 Calculator[] calculators = new Calculator[N]; for (int i = 0; i < N; i++) { e calculators[i] = new Calculator(); calculators[i].start(); // r System.out.println("total = " + total); public class Calc2 { private final int N = 1000; private int total = 0; public static void main(string[] args) { new Calc2().run(); private class Calculator extends Thread { for (int j = 0; j < N; j++) calc(); w for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) calc(); System.out.println("total = " + total); private void calc() { total = total.add(biginteger.one); q private void calc() { total++; q () () q private synchronized void calc() { total = total.add(biginteger.one); () () JAVA PRESS Vol.30 71
e e r for (int i = 0; i < N; i++) { calculators[i].join(); catch (InterruptedException e) { w public synchronized void run() { for (int j = 0; j < N; j++) calc(); w synchronized (Calc1.this) { for (int j = 0; j < N; j++) calc(); q 72 JAVA PRESS Vol.30
t new Calc1().start(); new Calc1().start(); t Calc1 calc1 = new Calc1(); calc1.start(); calc1.start(); q we import java.math.biginteger; public class Calc3 extends Thread { private final int N = 1000; private BigInteger total = BigInteger.ZERO; public static void main(string[] args) { new Calc3().start(); Calculator[] calculators = new Calculator[N]; for (int i = 0; i < N; i++) { calculators[i] = new Calculator(); calculators[i].start(); for (int i = 0; i < N; i++) { calculators[i].join(); catch (InterruptedException e) { total = total.add(calculators[i].subtotal); System.out.println("total = " + total); e y private static BigInteger total = BigInteger.ZERO; w synchronized (Calc1.class) { for (int j = 0; j < N; j++) calc(); private class Calculator extends Thread { private BigInteger subtotal = BigInteger.ZERO; for (int j= 0; j < N; j++) calc(); private void calc() { subtotal = subtotal.add(biginteger.one); q w JAVA PRESS Vol.30 73
import java.awt.event.*; import java.math.biginteger; import javax.swing.*; public class ProgressBarTest extends JFrame { private JProgressBar progressbar; private boolean canceled; public static void main(string[] args) { new ProgressBarTest(); q e % javac ProgressBarTest.java % java ProgressBarTest public ProgressBarTest() { super("progressbartest"); JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); JMenu menu = new JMenu("action"); menubar.add(menu); JMenuItem menuitem = new JMenuItem("start"); menuitem.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { progressbar.setvalue(0); canceled = false; for (int i = 1; i <= 100 &&!canceled; i++) { BigInteger total = BigInteger.ZERO; for (int j = 0; j < 500000; j++) total = total.add(biginteger.one); progressbar.setvalue(i); ); menu.add(menuitem); menuitem = new JMenuItem("stop"); menuitem.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { canceled = true; ); menu.add(menuitem); menuitem = new JMenuItem("exit"); menuitem.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { System.exit(0); ); menu.add(menuitem); progressbar = new JProgressBar(0, 100); progressbar.setstringpainted(true); getcontentpane().add(progressbar); pack(); setvisible(true); q w q progressbar.setvalue(0); canceled = false; for (int i = 1; i <= 100 &&!canceled; i++) { BigInteger total = BigInteger.ZERO; for (int j = 0; j < 500000; j++) total = total.add(biginteger.one); progressbar.setvalue(i); progressbar.paintimmediately( progressbar.getvisiblerect()); 74 JAVA PRESS Vol.30
w q e e private volatile Thread calculator; calculator = null; () w q progressbar.setvalue(0); calculator = new Thread() { int i; () for (i = 1; i <= 100 && calculator == this; i++) { // t BigInteger total = BigInteger.ZERO; for (int j = 0; j < 500000; j++) total = total.add(biginteger.one); SwingUtilities.invokeLater(new Runnable() { progressbar.setvalue(i); ); ; // r calculator.start(); JAVA PRESS Vol.30 75
r calculator.setpriority(calculator.getpriority() + 4); yield(); t t sleep(10); catch (InterruptedException e) { t calculator.setpriority(calculator.getpriority() - 1); w if (calculator!= null) { Thread oldcalculator = calculator; calculator = null; oldcalculator.interrupt(); 76 JAVA PRESS Vol.30
<applet code="animationapplet" width="200" height="200"> </applet> import java.awt.event.*; public class AnimationApplet extends java.applet.applet { private int i = 0; private volatile Thread animator; private boolean suspended; public void init() { addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { synchronized (AnimationApplet.this) { suspended =!suspended; if (!suspended) AnimationApplet.this.notify(); ); public void start() { suspended = false; animator = new Thread() { u while (animator == this) { sleep(1000); synchronized (AnimationApplet.this) { while (suspended && animator == this) AnimationApplet.this.wait(); catch (InterruptedException e) { repaint(); ; animator.start(); public void stop() { Thread oldanimator = animator; animator = null; oldanimator.interrupt(); public void paint(java.awt.graphics g) { if (i == 0) { g.drawline(0, 0, getwidth(), getheight()); i = 1; else { g.drawline(getwidth(), 0, 0, getheight()); i = 0; % javac AnimationApplet.java % appletviewer animationapplet.html e y r w t q JAVA PRESS Vol.30 77
q w e r t y q w 78 JAVA PRESS Vol.30 () () () () () () () () () () () () () () ()
q q Thread oldanimator = animator; animator = null; oldanimator.join(); catch (InterruptedException e) { oldanimator.interrupt(); u while (true) { sleep(1000); synchronized (AnimationApplet.this) { while (suspended && animator == this) AnimationApplet.this.wait(); repaint(); catch (InterruptedException e) { q animator.interrupt(); animator = null; JAVA PRESS Vol.30 79
import java.io.*; import java.net.*; // r public class Httpd implements Runnable { public static void main(string[] args) { ServerSocket serversocket = new ServerSocket(8080); while (true) { Socket socket = serversocket.accept(); new Httpd(socket).run(); catch (IOException e) { private Socket socket; public Httpd(Socket socket) { this.socket = socket; BufferedWriter out = null; // w out = new BufferedWriter(new OutputStreamWriter( socket.getoutputstream())); % javac Httpd.java % java Httpd q e e out.write("http/1.0 200 OK\r\n"); q out.write("cotent-type: text/plain\r\n"); out.write("\r\n"); out.write("hello\r\n"); catch (IOException e) { finally { if (out!= null) w out.close(); catch (IOException e) { Thread.sleep(10000); catch (InterruptedException e) { socket.close(); catch (IOException e) { e ServerSocket serversocket = new ServerSocket(8080); while (true) { Socket socket = serversocket.accept(); new Thread(new Httpd(socket)).start(); catch (IOException e) { 80 JAVA PRESS Vol.30
r import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; e PooledExecutor pooledexecutor = new PooledExecutor(10); ServerSocket serversocket = new ServerSocket(8080); while (true) { Socket socket = serversocket.accept(); pooledexecutor.execute(new Httpd(socket)); catch (IOException e) { catch (InterruptedException e) { % jar xf concurrent.zip % cd concurrent % javac -d.. *.java % cd.. % javac Httpd.java % java Httpd JAVA PRESS Vol.30 81
J q w e r 82 JAVA PRESS Vol.30