vol.30.}...`.X...b.h



Similar documents
Thread

6 p.1 6 Java GUI GUI paintcomponent GUI mouseclicked, keypressed, actionperformed mouseclicked paintcomponent thread, 1 GUI 6.0.2, mutlithread C

2

アプレットの作成

55 7 Java C Java TCP/IP TCP/IP TCP TCP_RO.java import java.net.*; import java.io.*; public class TCP_RO { public static void main(string[] a

I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi

II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K

< F2D834F838C A815B A CC>

< F2D B838A835882CC8CF68EAE2E6A7464>

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

untitled

< F2D F B834E2E6A7464>

< F2D82518CC282CC D2E6A7464>

< F2D82B682E182F182AF82F12E6A7464>

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i

< F2D92DE82E8914B82CC977088D32E6A7464>

Java演習(4) -- 変数と型 --

r3.dvi

2 static final int DO NOTHING ON CLOSE static final int HIDE ON CLOSE static final int DISPOSE ON CLOSE static final int EXIT ON CLOSE void setvisible

:30 12:00 I. I V II. III. IV. ( a d) V. VI : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeyliste

Java言語 第1回

< F2D8EA CE909482CC92EA82852E6A7464>

< F2D825282CC947B909482CC A815B83682E6A>

226


try catch Exception Java try catch try { } catch ( Exception e ) { } e 16-1 try catch 0 try { int x = 0; int y = 10 / x; } catch ( Exception e ) { Sys

ただし 無作為にスレッドを複数実行すると 結果不正やデッドロックが起きる可能性がある 複数のスレッド ( マルチスレッド ) を安全に実行する ( スレッドセーフにする ) ためには 同期処理を用いるこ とが必要になる 同期処理は 予約語 synchronized で行うことができる ここでは sy

r8.dvi

Object MenuComponent MenuBar MenuItem Menu CheckboxMenuItem

r14.dvi

K227 Java 2

Safari AppletViewer Web HTML Netscape Web Web 15-1 Applet Web Applet init Web paint Web start Web HTML stop destroy update init Web paint start Web up

I. java.awt.rectangle java.lang.math random Java TM API java.awt Rectangle Rectangle (x,y)... public int x Rectangle X public int y Rectangle Y public

2

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

Java演習(9) -- クラスとメソッド --

ガイダンス

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

text_10.dvi

新・明解Java入門

r2.dvi

I. (i) Foo public (A). javac Foo.java java Foo.class (C). javac Foo java Foo (ii)? (B). javac Foo.java java Foo (D). javac Foo java Foo.class (A). Jav

Programming-C-9.key

Java言語 第1回

ガイダンス

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap

r4.dvi

Java演習(2) -- 簡単なプログラム --


< F2D E E6A7464>

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

問題1 以下に示すプログラムは、次の処理をするプログラムである

< F2D89BA8EE882C E6A7464>

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

Microsoft PowerPoint prog1_doc2x.pptx

2

問1

Local variable x y i paint public class Sample extends Applet { public void paint( Graphics gc ) { int x, y;... int i=10 ; while ( i < 100 ) {... i +=

アプレットⅤ


r3.dvi

Microsoft PowerPoint - OOP.pptx

r2.dvi

Java プログラミング Ⅰ 3 回目変 数 今日の講義講義で学ぶ内容 変数とは 変数の使い方 キーボード入力の仕方 変 数 変 数 一時的に値を記憶させておく機能 変数は 型 ( データ型 ) と識別子をもちます 2 型 ( データ型 ) 変数に記憶する値の種類変数の型は 記憶できる値の種類と範囲

19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics;

text_13.dvi

問題1 以下に示すプログラムは、次の処理をするプログラムである

シミュレーションの簡単な例 GUI 無しのシミュレーションを作る GUI を作る パラメタを設定するデモンストレーションをする 2 オブジェクト指向プログラミング特論

< F2D82518E9F8AD CC834F CC8CFC82AB82C68D4C>

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

GUIプログラムⅣ


Java言語 第1回

Microsoft Word - keisankigairon.ch doc

GUIプログラムⅤ

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

< F2D A839382CC906A2E6A7464>

I java A

public class MyFrame { private JPanel panel1; private JPanel panel2; private JPanel panel3; private JPanel panel4; private JLabel label1; private JTex

Microsoft PowerPoint prog1_doc2.pptx

< F2D82518E9F8AD CC95BD8D7388DA93AE2E6A7464>

2.2 Java C main Java main 2 C 6 C Java 3 C Java ( ) G101Hello.java G101Hello main G101Hello.java /* G101Hello */ class G101Hello { /* main */ public s

JavaプログラミングⅠ

< F2D B825082CC96E291E82E6A7464>

< F2D A838B838D96402E6A7464>

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def

Javaセキュアコーディングセミナー東京 第3回 入出力(File, Stream)と例外時の動作 演習解説

ALG ppt

( ) p.1 x y y = ( x ) 1 γ γ = filtergamma.java import java.applet.*; public class filtergamma extends Applet{ Image img; Image new_img; publi

ÿþ˜u#u·0¹0Æ0à0

untitled

Microsoft PowerPoint - prog10.ppt

例外(exception)とは何か

: : : TSTank 2

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

解きながら学ぶJava入門編

8 if switch for while do while 2

ガイダンス

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の break; までを処理し どれにも一致しない場合 default; から直後の break; までを処理する 但し 式や値 1

Transcription:

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