ThreadTestExt.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package jp.ac.utsunomiya_u.is; public class ThreadTestExt extends Thread { /** * スレッド名 */ private String name = null ; /** * コンストラクタ * * @param name スレッド名 */ public ThreadTestExt(String name) { this .name = name; } /** * メソッドrunのオーバーライド */ @Override public void run() { for ( int i = 0 ; i < 10 ; i++) { System.out.println(name + " " + i); try { sleep( 1000 ); } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } } } public static void main(String[] args) { ThreadTestExt threadTestExt1 = new ThreadTestExt( "thread 1" ); threadTestExt1.start(); } } |
出力
thread 1 0 thread 1 1 thread 1 2 thread 1 3 thread 1 4 thread 1 5 thread 1 6 thread 1 7 thread 1 8 thread 1 9
ThreadTestExt.javaの修正部分
1 2 3 4 5 6 | public static void main(String[] args) { ThreadTestExt threadTestExt1 = new ThreadTestExt( "thread 1" ); ThreadTestExt threadTestExt2 = new ThreadTestExt( "thread 2" ); threadTestExt1.start(); threadTestExt2.start(); } |
出力例
thread 1 0 thread 2 0 thread 2 1 thread 1 1 thread 1 2 thread 2 2 thread 2 3 thread 1 3 thread 2 4 thread 1 4 thread 1 5 thread 2 5 thread 1 6 thread 2 6 thread 1 7 thread 2 7 thread 1 8 thread 2 8 thread 2 9 thread 1 9
ThreadTestImp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package jp.ac.utsunomiya_u.is; public class ThreadTestImp implements Runnable { /** * スレッド名 */ private String name = null ; /** * コンストラクタ * * @param name スレッド名 */ public ThreadTestImp(String name) { this .name = name; } /** * runメソッドのオーバーライド */ @Override public void run() { for ( int i = 0 ; i < 10 ; i++) { System.out.println(name + " " + i); try { Thread.sleep( 1000 ); } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } } } public static void main(String[] args) { ThreadTestImp threadTestImp1 = new ThreadTestImp( "thread 1" ); ThreadTestImp threadTestImp2 = new ThreadTestImp( "thread 2" ); Thread thread1 = new Thread(threadTestImp1); Thread thread2 = new Thread(threadTestImp2); thread1.start(); thread2.start(); } } |
ThreadTestImp.javaの追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void main(String[] args) { ThreadTestImp threadTestImp1 = new ThreadTestImp( "thread 1" ); ThreadTestImp threadTestImp2 = new ThreadTestImp( "thread 2" ); Thread thread1 = new Thread(threadTestImp1); Thread thread2 = new Thread(threadTestImp2); System.out.println(thread1.getPriority()); System.out.println(thread2.getPriority()); thread1.setPriority(Thread.MAX_PRIORITY); thread2.setPriority(Thread.MIN_PRIORITY); System.out.println(thread1.getPriority()); System.out.println(thread2.getPriority()); thread1.start(); thread2.start(); } |
ThreadTestImp.javaの追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static void main(String[] args) { ThreadTestImp threadTestImp1 = new ThreadTestImp( "thread 1" ); ThreadTestImp threadTestImp2 = new ThreadTestImp( "thread 2" ); Thread thread1 = new Thread(threadTestImp1); Thread thread2 = new Thread(threadTestImp2); System.out.println(thread1.getPriority()); System.out.println(thread2.getPriority()); thread1.setPriority(Thread.MAX_PRIORITY); thread2.setPriority(Thread.MIN_PRIORITY); System.out.println(thread1.getPriority()); System.out.println(thread2.getPriority()); thread1.start(); thread2.start(); try { thread1.join(); System.out.println( "thread 1 has finished." ); } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } } |
BankTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package jp.ac.utsunomiya_u.is; /** * 銀行口座クラス */ class Account { /** * 預金額 */ private int balance = 0 ; /** * 入金 * * @param amount 入金額 */ void deposit( int amount) { balance += amount; } /** * 預金額の取得 * * @return 預金額 */ public int getBalance() { return balance; } } /** * 顧客クラス */ class Customer implements Runnable { /** * 銀行口座 */ private final Account account; /** * コンストラクタ * * @param account 銀行口座 */ Customer(Account account) { this .account = account; } /** * runメソッドのオーバーライド */ @Override public void run() { for ( int i = 0 ; i < 1000 ; i++) { // 1入金 account.deposit( 1 ); } } } public class BankTest { /** * 顧客数 */ private final static int NUMBER_OF_CUSTOMERS = 10 ; public static void main(String[] args) { // 銀行口座生成 Account account = new Account(); // 顧客配列生成 Customer[] customers = new Customer[NUMBER_OF_CUSTOMERS]; // スレッド生成 Thread[] threads = new Thread[NUMBER_OF_CUSTOMERS]; // 個々の顧客生成とスレッド開始 for ( int i = 0 ; i < NUMBER_OF_CUSTOMERS; i++) { customers[i] = new Customer(account); threads[i] = new Thread(customers[i]); threads[i].start(); } // 全てのスレッド終了まで待機 for ( int i = 0 ; i < NUMBER_OF_CUSTOMERS; i++) { try { threads[i].join(); } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } } // 銀行口座の預金額表示 System.out.println(account.getBalance()); } } |