初心者向けのJava講座です。一緒にJavaを勉強しませんか?
はじめてのJava入門初心者向けのJava講座 |
スレッドの優先順位
スレッドのライフサイクルのところで実行可能状態になっても直ぐには起動しないことは説明しました。
それはスレッドの優先順位が高いスレッドから起動されるからです。
スレッドの優先度はint型の整数1から10までで表し数字が大きいほど優先度は高く、小さくなるほど優先度が低くなります。
また、優先度を設定しなければデフォルドで5が自動的に決められます。なおThreadクラスに優先度がstatic定数として定義されています。
スレッドの優先度
上記で説明していますが優先度はint 型の 1から10で表しますがそのほかに
Threadクラスに初めからstatic定数として定義されています。
static定数 |
優先度 |
Thread.MIN_PRIORITY | 1 (最低優先度) |
Thread.NORM_PRIORITY | 5 (デフォルド) |
Thread.MAX_PRIORITY | 10 (最高優先度) |
優先度の設定の仕方
スレッドに優先度を設定するには、
ThreadクラスのsetPriority()メソッドを使います。
設定したい優先度をsetPriority()メソッドの引数に指定します。引数にはstatic定数かもしくはint型の整数を指定します。
例
setPriority(Thread.MAX_PRIORITY) ・・・・・・・・・・優先度10setPriority(10) ・・・・・・・・・・・・・・・・・・・・・・・・・・優先度10
setPriority(7) ・・・・・・・・・・・・・・・・・・・・・・・・・・・優先度7
サンプルプログラム(Sample88.java)
class ThreadSample88 extends Thread{ String str; ThreadSample88(String str){ this.str = str; } public void run(){ System.out.println(str); } } class Sample88{ public static void main(String args[]){ ThreadSample88 th1 = new ThreadSample88("おはよう"); ThreadSample88 th2 = new ThreadSample88("こんばんわ"); ThreadSample88 th3 = new ThreadSample88("こんにちわ"); //優先順位を設定 th1.setPriority(Thread.MAX_PRIORITY); th2.setPriority(Thread.NORM_PRIORITY); th3.setPriority(Thread.MIN_PRIORITY); System.out.println("start!"); th1.start(); th2.start(); th3.start();
} }
プログラムをコピーする場合すべて選択をクリックしてください。
Sponsored link
サンプルプログラム(Sample88_1.java)
class ThreadSample88_1 extends Thread{ String str; ThreadSample88_1(String str){ this.str = str; } public void run(){ System.out.println(str); } } class Sample88_1{ public static void main(String args[]){ ThreadSample88_1 th1 = new ThreadSample88_1("おはよう"); ThreadSample88_1 th2 = new ThreadSample88_1("こんにちわ"); ThreadSample88_1 th3 = new ThreadSample88_1("こんばんわ"); //優先順位を設定 th1.setPriority(Thread.MIN_PRIORITY); th2.setPriority(Thread.NORM_PRIORITY); th3.setPriority(Thread.MAX_PRIORITY); System.out.println("start!"); th1.start(); th2.start(); th3.start();
} }
プログラムをコピーする場合すべて選択をクリックしてください。
Sponsored link
スクール・通信講座の徹底比較!一括資料請求【無料】
36年の実績,利用企業6,000社,受講者総数200万人突破。
技術系の通信教育講座ならJTEX!
☆ドスパラWeb通販プログラム☆
2つのプログラムをコンパイル・実行してみます。
上記2つのプログラムを見てください。Threadクラスの定数を使い優先順位を設定しています。
setPriority()メソッドを用いて優先度を設定しています。
th1,th2,th3に対してstart()が実行されスレッドが開始されています。
優先度の高い順にスレッドが開始されているのがわかると思います。
つまり
setPriority(Thread.MAX_PRIORITY)setPriority(Thread.NORM_PRIORITY)
setPriority(Thread.MIN_PRIORITY)
の順になります。
最初に説明したと思いますがsetPriority()の引数にはint型の1から10までの整数を設定することもできます。次のプログラムを見てください。
サンプルプログラム(Sample88_2.java)
class ThreadSample88_2 extends Thread{ String str; ThreadSample88_2(String str){ this.str = str; } public void run(){ System.out.println(str); } } class Sample88_2{ public static void main(String args[]){
ThreadSample88_2 th1 = new ThreadSample88_2("おはよう"); ThreadSample88_2 th2 = new ThreadSample88_2("ようこそ"); ThreadSample88_2 th3 = new ThreadSample88_2("こんばんわ"); //優先順位を設定 th1.setPriority(1); th2.setPriority(10); th3.setPriority(5); System.out.println("start!"); th1.start(); th2.start(); th3.start(); } }
プログラムをコピーする場合すべて選択をクリックしてください。
Sponsored link
家電品の安さ、品揃えに自信あり!
激安!家電のタンタンショップ
中古パソコンのデジタルドラゴン ドロップシッピング代理店
コンパイル・実行してみます。
このようにsetPriority()の引数にint型の整数を指定してやることで優先度を指定しておくこともできます。
優先度を確認する
自身のスレッドの優先度を確認したい場合はThreadクラスのgetPriority()メソッドを使います。
サンプルプログラム(ThreadSample88_3.java)
class ThreadSample88_3 extends Thread{ String str; ThreadSample88_3(String str){ this.str = str; } public void run(){ System.out.println(str); } } class Sample88_3{ public static void main(String args[]){ ThreadSample88_3 th1 = new ThreadSample88_3("おはよう"); ThreadSample88_3 th2 = new ThreadSample88_3("ようこそ"); ThreadSample88_3 th3 = new ThreadSample88_3("こんばんわ"); //優先順位を設定 th1.setPriority(1); th2.setPriority(10); th3.setPriority(5); System.out.println("start!"); //優先度を確認する
System.out.println("th1の優先度は " + th1.getPriority()); System.out.println("th2の優先度は " + th2.getPriority()); System.out.println("th3の優先度は " + th3.getPriority()); th1.start(); th2.start(); th3.start();
} }
プログラムをコピーする場合すべて選択をクリックしてください。
Sponsored link
コンパイル・実行します。
このようになります。