progJ.pdf 記事一覧 |
![]() Runnable インターフェイスの実装 |
progJ.pdf に [#40] ~ [#44] を追加しました.行数が予定を大幅に超過したのでサンプルプログラムを変更するかも知れません.
※ NetBeans の IDE では補足(3)の "in.txt" はプロジェクトのフォルダの直下(src や build の外)に置いてください.
※ P43.java は不良品です---とりあえず応急処置をしました(11/06 追記).
補足:(1) P40.java
import myLib.*;
public class P40 {
/* コメントにしなくてもエラーにならない.
static class Now{
int n;
Now(int k){n = k;}
void start( ){
System.out.println("n = " + n);
}
}
コメントにしないとこちらが勝つ */
public static void main(String[ ] args) {
Now n = new Now(4); n.start( );
}
}
//Now.java
package myLib;
import java.util.Date;
public class Now extends Thread{
public int cnt;
public Now(int k){cnt = k;}
@Override
public void run( ){
Date d;
while(cnt > 0){
d = new Date( );
System.out.println(cnt + ": " + d); cnt--;
try{
sleep(1000 * cnt);
}catch(InterruptedException e){ }
}
}
}
(2) P43.java
import myLib.*;
import java.io.*;
class QueR extends Que implements Runnable {
QueR( ){super( );}
public void run( ){
BufferedReader r; String s; char c;
r = new BufferedReader(new InputStreamReader(System.in));
try{
s = r.readLine( ); c = s.charAt(0);
while(c != 'e'){
if(c == 'i'){insert(s.substring(2));}
else if(c == 'd'){delete( );}
else if(c == 's'){show( );}
//System.out.println("begin-sleep");
Thread.yield();
//Thread.sleep(1000);//無効
//System.out.println("end-sleep");
s = r.readLine(); c = s.charAt(0);
}
}catch(IOException e){ }
//catch(InterruptedException e){ }
}
}
public class P43{
public static void main(String[] args) {
Now n1 = new Now(4); Now n2 = new Now(4);
n1.setPriority(Thread.MIN_PRIORITY);
n2.setPriority(Thread.MAX_PRIORITY);
QueR q1 = new QueR( ); QueR q2 = new QueR( );
n1.start( );
q1.run( ); q2.run( ); q1.show( ); q2.show( );
n2.start( );
}
}
//List.java
package myLib;
class Cell{
String d; Cell n;
Cell(String s){d = s; n = null;}
}
public class List{
public Cell top;
public int sz;
public List(){top = new Cell(" "); sz = 0;}
public void insert(String s, int k){
if(ksz+1){return;}
Cell c1 = top, c2 = new Cell(s);
while(k > 1){c1 = c1.n; k--;}
if(c1.n != null){c2.n = c1.n;}
c1.n = c2; sz++;
}
public void delete(int k){
if(ksz){return;}
Cell c1 = top;
while(k > 1){c1 = c1.n; k--;}
c1.n = c1.n.n; sz--;
}
public String show(int k){
if(ksz){return "?";}
Cell c1 = top.n; int i = k;
while(k > 1){c1 = c1.n; k--;}
System.out.println(i + ": " + c1.d);
return c1.d;
}
public void show(){
Cell c1 = top.n; int i;
if(sz < 1){System.out.print("empty");}
for(i = 1; i System.out.print(c1.d + " ");
if(i < sz ){c1 = c1.n;}
}
System.out.println();
}
}
//Que.java
package myLib;
public class Que extends List{
Cell last;
public Que( ){ super( ); last = top; }
public void insert(String s){
last.n = new Cell(s);
last = last.n; sz++;
}
public String delete( ){
Cell c = top.n;
delete(1); if(sz == 0){last = top;}
return c.d;
}
}
(3) P45.java
import java.io.*;
class Check{
StringBuffer b;
Check( ){b = new StringBuffer(256);}
void rFile( ){
FileReader r; int c;
try{
r = new FileReader("in.txt");
while((c = r.read( )) != -1){
b.append((char) c);
}
r.close( ); System.out.println("OK?");
}catch(IOException e){
b.append("not found");
}
}
void wFile(){
FileWriter w; String s;
try{
b.append("Hello\nworld.");
w = new FileWriter("out.txt");
for(int k = 0; k < b.length( ); k++){
w.write(b.charAt(k));
}
w.close( ); System.out.println("OK?");
}catch(IOException e){b.append("skipped");}
}
}
public class P45 {
public static void main(String[] args){
Check x = new Check( ); x.rFile( );
System.out.println(x.b);
x = new Check( ); x.wFile( );
System.out.println(x.b);
//
StringBuffer sb = new StringBuffer(10);
sb.append(123.67); sb.append((char) 65); sb.append(503);
System.out.println(sb);
}
}