ThreadTestExt.java
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の修正部分
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
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の追加
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の追加
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
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());
}
}