Thread

Similar documents
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

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

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

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

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

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

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

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

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

r3.dvi

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

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

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

: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入門

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

226

ガイダンス

< F2D834F838C A815B A CC>

< F2D B838A835882CC8CF68EAE2E6A7464>

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

: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

ガイダンス

: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

: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

< F2D F B834E2E6A7464>


アプレットの作成

r4.dvi

r2.dvi

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

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

< F2D82518CC282CC D2E6A7464>

< F2D92DE82E8914B82CC977088D32E6A7464>

K227 Java 2

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

< F2D825282CC947B909482CC A815B83682E6A>

r14.dvi

目 次 Java GUI 3 1 概要 クラス構成 ソースコード例 課題...7 i

< F2D82B682E182F182AF82F12E6A7464>

解きながら学ぶJava入門編

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;

ガイダンス

< F2D8EA CE909482CC92EA82852E6A7464>

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

r8.dvi

ガイダンス

例外(exception)とは何か

ガイダンス

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 +=

2

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

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

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

I. (i) Java? (A). Foo_Bar (B). G day (C). 999 (D). Golgo13 (ii)? (A). Java public (B). Java (C). Java JavaScript (D). Java C Java C (iii)? (A). Java (

Programming-C-9.key

text_13.dvi

LMNtal LMNtal LMNtal JAVA JAVA JAVA LMNtal LMNtal LMNtal

Java 3 p.2 3 Java : boolean Graphics draw3drect fill3drect C int C OK while (1) int boolean switch case C Calendar java.util.calendar A

2

Java (5) 1 Lesson 3: x 2 +4x +5 f(x) =x 2 +4x +5 x f(10) x Java , 3.0,..., 10.0, 1.0, 2.0,... flow rate (m**3/s) "flow

8 if switch for while do while 2

** 平成 16 年度 FE 午後問題 Java** 示現塾プロジェクトマネージャ テクニカルエンジニア ( ネットワーク ) など各種セミナーを開催中!! 開催日 受講料 カリキュラム等 詳しくは 今すぐアクセス!! 平成 16

JavaプログラミングⅠ

/ ( ) 8/7/ :21 p.2/64

text_10.dvi

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

untitled

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

Java (7) Lesson = (1) 1 m 3 /s m 2 5 m 2 4 m 2 1 m 3 m 1 m 0.5 m 3 /ms 0.3 m 3 /ms 0.6 m 3 /ms 1 1 3

GUIプログラムⅣ

次の演習課題(1),(2)のプログラムを完成させよ

Java言語 第1回

B 10 : N ip2003f10.tex B : 9/12/ :02 p.1/71

GUIプログラムⅤ

I java A

Chapter 20. [ ] ; [ ] = new [ ] ; Color colors [ ] = new Color[ 20 ]; // 20 Button operations [ ] = new Button[ 10 ]; // 10 colors[ 3 ] = new Color( 1

ALG ppt

GIMP import javafx.application.application; import javafx.scene.scene; import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext;

アプレットⅤ

untitled

JavaプログラミングⅠ

Java学習教材

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

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

IT プロジェクト


Microsoft PowerPoint ppt

1 Java Java GUI , 2 2 jlabel1 jlabel2 jlabel3 jtextfield1 jtextfield2 jtextfield3 jbutton1 jtextfield1 jtextfield2 jtextfield3

text_12.dvi

アルゴリズムとデータ構造1

. IDE JIVE[1][] Eclipse Java ( 1) Java Platform Debugger Architecture [5] 3. Eclipse GUI JIVE 3.1 Eclipse ( ) 1 JIVE Java [3] IDE c 016 Information Pr

Java 2 - Lesson01

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

データ構造とアルゴリズム論

r3.dvi

I 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mousec

明解Javaによるアルゴリズムとデータ構造

: : : TSTank 2

Transcription:

14 2013 7 16 14.1....................................... 14 1 14.2 Thread................................... 14 1 14.3............................. 14 5 14.4....................................... 14 10 14.5 : FreeCellPanel.java........................ 14 11 14.1 10 Java ( ) (threads) Java main main main GUI main GUI ( ) GUI 14.2 Thread Java java.lang.thread Thread Thread() ( run ) Thread(Runnable r) r.run() static Thread ( ) currentthread() static void ms ( sleep(long ms) InterruptedException ) void interrupt() boolean isalive() void join() ( InterruptedException ) void join(long ms) ms ( InterruptedException ) void run() Runnable run 14 1

void start() Java Thread start Thread 2 start 1. Thread run start run 2. Runnable Thread start (Runnable ) run G1401.java 3 1 3 1 G1401Panel Thread G1401Shuffle (30 36 ) start (26 27 ) 1 import java.awt.event.*; 2 import javax.swing.*; 3 import jp.ac.ryukoku.math.graphics.*; 4 5 class G1401Panel extends GamePanel { 6 Deck[] decks = new Deck[3]; 7 JButton button = new JButton(" "); 8 9 G1401Panel() { 10 int x = 160; 11 for (int i = 0; i < decks.length; i++) { 12 decks[i] = new Deck(); 13 decks[i].flip(); 14 add(decks[i], x, 240); 15 x += 200; 16 17 add(button, 352, 380); 18 button.addactionlistener(new ActionListener() { 19 public void actionperformed(actionevent e) { G1401.java 14 2

20 shuffleall(); 21 22 ); 23 24 25 void shuffleall() { 26 Thread th = new G1401Shuffle(); 27 th.start(); 28 29 30 class G1401Shuffle extends Thread { 31 public void run() { 32 for (Deck d : decks) { 33 d.shuffle(); 34 35 36 37 38 39 class G1401 implements Runnable { 40 public void run() { 41 JFrame f = new JFrame(); 42 f.setdefaultcloseoperation(jframe.exit_on_close); 43 f.add(new G1401Panel()); 44 f.pack(); 45 f.setvisible(true); 46 47 48 public static void main(string[] args) { 49 SwingUtilities.invokeLater(new G1401()); 50 51 ActionListener ( ) actionperformed (19 21 ) G1401Panel shuffleall (25 28 ) Thread G1401Shuffle (26 ) start (27 ) start shuffleall ( actionperformed) start G1401Shuffle run (31 36 ) run 3 shuffleall void shuffleall() { for (Deck d : decks) { d.shuffle(); 14 3

3 ( ) 1 2 G1401.java 2 G1401Shuffle Thread Runnable *30 class G1401Shuffle implements Runnable { 31 public void run() { 32 for (Deck d : decks) { 33 d.shuffle(); 34 35 36 26 *26 Thread th = new Thread(new G1401Shuffle()); ( 26 30 ) 1 2 Runnable 2 2 1 2 1 G1401.java Thread G1401Shuffle G1401Panel shuffleall void shuffleall() { Thread th = new Thread() { public void run() { for (Deck d : decks) { d.shuffle(); ; th.start(); th void shuffleall() { new Thread() { 1 d.shuffle(); d.shuffleasync(); 3 ( ) shuffleasync shuffleall 1 2 Runnable Thread 14 4

..start(); 14.3 G1401.java (multithreaded programming) G1402.java ( ) ( ) 2 (Pile) ( ) ( ) 2 ( ) 1 1 ( ) G1402.java 1 import jp.ac.ryukoku.math.graphics.*; 2 3 class G1402Target extends Pile { 4 G1402Target() { 5 super(12, 0); 6 7 8 boolean trytoadd(card c) { 9 int num = c.rank.getnumber(); 10 Card t = top(); 11 if (t == null t.rank.getnumber() == num - 1 12 t.rank.getnumber() == num + 1) { 13 c.moveto(this); 14 return true; 15 16 return false; 17 18 19 20 class G1402 extends GameFrame { 21 Pile reds = new Pile(25, 0); 22 Pile blacks = new Pile(25, 0); 23 G1402Target target = new G1402Target(); 24 14 5

25 G1402() { 26 Deck d = new Deck(); 27 d.shuffle(); 28 d.flip(); 29 while (!d.isempty()) { 30 Card c = d.pick(); 31 c.moveto(c.isred()? reds : blacks); 32 33 add(reds, 45, 60); 34 add(blacks, 45, 420); 35 add(target, 45, 240); 36 startjob(reds); 37 startjob(blacks); 38 39 40 void startjob(final Pile from) { 41 new Thread() { 42 public void run() { 43 while (!from.isempty()) { 44 for (Card c : from.getcards()) { 45 if (target.trytoadd(c)) { 46 break; 47 48 49 50 51.start(); 52 53 54 public static void main(string[] args) { 55 new G1402(); 56 57 36 37 startjob (40 52 ) 2 (reds) (target) (blacks) (target) G1402Target (Pile ) trytoadd (8 17 ) 1 1 true false 2 trytoadd (43 49 ) ( ) 14 6

2 trytoadd ( ) 1 5 6 4 5 6 4 5 6 trytoadd ( ) 1 (mutual exclusion) 3 (critical sections) G1402.java trytoadd Java (monitors) 4 1 Java (lock) (unlock) 5 3 4 Java 5 ( ) ( ) 14 7

1 ( ) 6 synchronized synchronized ( ) Java synchronized synchronized ( ) synchronized synchronized G1402.java trytoadd (* ) 8 boolean trytoadd(card c) { * 9 synchronized (this) { 10 int num = c.rank.getnumber(); 11 Card t = top(); 12 if (t == null t.rank.getnumber() == num - 1 13 t.rank.getnumber() == num + 1) { 14 c.moveto(this); 15 return true; 16 17 return false; *18 19 trytoadd 9 synchronized G1402Target ( ) synchronized synchronized * 8 synchronized boolean trytoadd(card c) { 9 int num = c.rank.getnumber(); 6 ( ) 14 8

10 Card t = top(); 11 if (t == null t.rank.getnumber() == num - 1 12 t.rank.getnumber() == num + 1) { 13 c.moveto(this); 14 return true; 15 16 return false; 17 synchronized 7 synchronized (synchronized ) G1402Target class G1402Target extends Pile { Object lock = new Object(); G1402Target() { super(12, 0); // ( ) trytoadd this boolean trytoadd(card c) { synchronized (lock) { // this lock int num = c.rank.getnumber();. return false; 7 synchronized 1 14 9

(dead locks) 1 1 1 14.4 1. FreeCellPanel.java 12 (b) (d) (a) ( 12 2) (b) (c) 1 (d) 0.1 (c) (d) (1) FreeCellPanel run 2. FreeCellPanel (b) finish ( movehome) (2) finish 14 10

14.5 : FreeCellPanel.java 12 FreeCellPanel.java * 87 88 volatile Java volatile FreeCellPanel.java 1 import java.awt.*; 2 import jp.ac.ryukoku.math.graphics.*; 3 4 abstract class Cell extends Pile { 5 Cell() { 6 7 8 Cell(double dx, double dy) { 9 super(dx, dy); 10 11 12 /* card free 13 abstract boolean movablefrom(card card, int free); 14 15 /* from card 16 abstract boolean movableto(cell from, Card card, int free); 17 18 19 class Free extends Cell { 20 boolean movablefrom(card card, int free) { 21 return true; 22 23 24 boolean movableto(cell from, Card card, int free) { 25 return isempty(); 26 27 28 29 class Home extends Cell { 30 boolean movablefrom(card card, int free) { 31 return false; 32 33 34 boolean movableto(cell from, Card card, int free) { 35 if (isempty()) { 36 return card.rank == Rank.ACE; 37 38 Card top = top(); 39 return card.suit == top.suit 40 && card.rank.getnumber() == top.rank.getnumber() + 1; 41 42 43 44 class Cascade extends Cell { 45 Cascade() { 46 super(0.0, 30.0); 14 11

47 48 49 boolean movablefrom(card card, int free) { 50 Card prev = null; 51 for (Card c : getcards()) { 52 if (prev!= null) { 53 if (free-- <= 0) { 54 return false; 55 56 if (c.isred() == prev.isred() c.rank.getnumber() 57!= prev.rank.getnumber() - 1) { 58 return false; 59 60 prev = c; 61 else if (c == card) { 62 prev = c; 63 64 65 return true; 66 67 68 boolean movableto(cell from, Card card, int free) { 69 /* free 1 70 if (isempty() &&!from.movablefrom(card, free - 1)) { 71 return false; 72 73 if (isempty()) { 74 return true; 75 76 Card top = top(); 77 return (card.isred()!= top.isred() 78 && card.rank.getnumber() == top.rank.getnumber() - 1); 79 80 81 82 public class FreeCellPanel extends GamePanel 83 implements CardListener, Runnable { 84 Cell[] freecells = new Cell[4]; 85 Cell[] homecells = new Cell[4]; 86 Cell[] cascades = new Cell[8]; * 87 volatile long time; * 88 volatile Thread thread; 89 90 public FreeCellPanel() { 91 for (int i = 0; i < freecells.length; i++) { 92 freecells[i] = new Free(); 93 add(freecells[i], 30 + i * 90, 30); 94 95 for (int i = 0; i < homecells.length; i++) { 96 homecells[i] = new Home(); 97 add(homecells[i], 420 + i * 90, 30); 98 99 for (int i = 0; i < cascades.length; i++) { 100 cascades[i] = new Cascade(); 101 add(cascades[i], 40 + i * 90, 180); 102 103 104 105 /* 14 12

*106 protected void paintcomponent(graphics g) { *107 super.paintcomponent(g); *108 if (time > 0) { *109 g.setcolor(color.white); *110 g.drawstring(string.format(" %.1f ", *111 time / 1000.0), 30, 20); *112 *113 114 115 /* 116 public void start() { *117 if (thread!= null) { *118 Thread oldthread = thread; *119 thread = null; *120 try { *121 oldthread.join(); *122 catch (InterruptedException e) { *123 *124 thread = (1) ; *126 thread.start(); 127 128 129 /* *130 public void run() { 131 reset(); 132 Deck d = new Deck(); 133 for (Card c : d.getcards()) { 134 c.setsticky(true); 135 136 d.shuffle(); 137 d.flip(); 138 add(d, 20, 620); 139 int i = 0; 140 Card[] cards = d.getcards(); 141 while (!d.isempty()) { 142 d.pick(); 143 144 d.remove(); 145 for (Card c : cards) { *146 if (thread == null) { *147 break; *148 149 c.addcardlistener(this); *150 c.setspeed(100); *151 c.moveto(cascades[i++ % cascades.length]); *152 c.setspeed(card.default_speed); 153 *154 try { *155 long starttime = System.currentTimeMillis(); *156 while (thread!= null && countfree() < 12) { *157 Thread.sleep(100); *158 time = System.currentTimeMillis() - starttime; *159 repaint(); *160 *161 catch (InterruptedException e) { *162 14 13

*163 164 165 /* 166 public void reset() { 167 for (Pile p : freecells) { 168 p.clear(); 169 170 for (Pile p : homecells) { 171 p.clear(); 172 173 for (Pile p : cascades) { 174 p.clear(); 175 176 177 178 /* from 1 179 true false *180 private boolean movehome(cell from) { *181 Card card = from.top(); *182 if (card!= null) { *183 for (Cell to : homecells) { *184 if (to.movableto(from, card, 0)) { *185 card.moveto(to); *186 return true; *187 *188 *189 *190 return false; *191 192 193 /* *194 public void finish() { *195 boolean moved; *196 do { *197 moved = false; *198 for (Cell c : freecells) { *199 moved = movehome(c); *200 *201 for (Cell c : cascades) { *202 moved = movehome(c); *203 *204 while (moved); *205 206 207 /* 208 public int countfree() { 209 int free = 0; 210 for (Pile p : freecells) { 211 if (p.isempty()) { 212 free++; 213 214 215 for (Pile p : cascades) { 216 if (p.isempty()) { 217 free++; 218 219 220 return free; 221 14 14

222 223 /* 224 public boolean cardselected(cardevent e) { 225 Cell cell = (Cell) e.getpile(); 226 return cell.movablefrom(e.getcard(), countfree()); 227 228 229 /* 230 public boolean cardmoved(cardevent e) { 231 Card card = e.getcard(); 232 Cell from = (Cell) e.getpile(); 233 Cell to = (Cell) e.getdest(); 234 if (from == null to == null) { 235 return false; 236 237 if (to.movableto(from, card, countfree())) { 238 from.movecardsasync(card, to); 239 return true; 240 241 return false; 242 243 244 /* 245 public void cardpicked(cardevent e) { *246 Card card = e.getcard(); *247 Cell from = (Cell) e.getpile(); *248 if (!from.movablefrom(card, 0)) { (2) *254 return; *255 *256 for (Cell to : homecells) { *257 if (to.movableto(from, card, 0)) { *258 card.moveasyncto(to); *259 return; *260 *261 *262 263 14 14 15