
JavaでThreadを使いこなす!生成から待ち合わせまで非同期処理の方法
- SE
- JavaでThreadを使いこなすにはどうしたら良いのでしょうか。
- PM
- Threadの基本的な使い方から順に説明しますので、さまざまな使い方を学んでいきましょう。
目次
JavaでThreadを使った非同期処理とは?
今回は、JavaでThreadを使った非同期処理について説明します。
ここでは、
・基本的な使い方(Threadクラス)
・基本的な使い方(Runnableインターフェース)
・複数のスレッドを動かす
・待ち合わせ
・複数スレッド待ち合わせ
について紹介します。
JavaでThreadを使った非同期処理に興味のある方はぜひご覧ください。
基本的な使い方(Threadクラス)
Javaでは、Threadクラスを継承したクラスを作成すれば、非同期処理を実現できます。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class ThreadSample extends Thread { public void run() { for (int i = 0; i < 5; i++) { System.out.println(""Thread i = "" + i); } } } public class Main { public static void main(String[] args) throws Exception { ThreadSample threadSample = new ThreadSample(); threadSample.start(); } } |
実行結果は以下のようになります。
1 2 3 4 5 |
Thread i = 0 Thread i = 1 Thread i = 2 Thread i = 3 Thread i = 4 |
このようにJavaでは、Threadクラスを継承したクラスを作成すれば、非同期処理を実現できます。
基本的な使い方(Runnableインターフェース)
Javaでは、Runnableインターフェースを実装したクラスを作成しても、非同期処理を実現できます。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class ThreadSample implements Runnable { public void run() { for (int i = 0; i < 5; i++) { System.out.println(""Runnable i = "" + i); } } } public class Main { public static void main(String[] args) throws Exception { ThreadSample threadSample = new ThreadSample(); Thread th = new Thread(threadSample); th.start(); } } |
実行結果は以下のようになります。
1 2 3 4 5 |
Runnable i = 0 Runnable i = 1 Runnable i = 2 Runnable i = 3 Runnable i = 4 |
このようにJavaでは、Runnableインターフェースを実装したクラスを作成しても、非同期処理を実現できます。
複数のスレッドを動かす
Javaで複数のスレッドを動かすサンプルを紹介します。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class ThreadSample extends Thread { public void run() { for (int i = 0; i < 5; i++) { System.out.println(""Thread ID:"" + Thread.currentThread().getId() + "", i = "" + i); } } } public class Main { public static void main(String[] args) throws Exception { ThreadSample threadSample1 = new ThreadSample(); ThreadSample threadSample2 = new ThreadSample(); ThreadSample threadSample3 = new ThreadSample(); threadSample1.start(); threadSample2.start(); threadSample3.start(); } } |
実行結果は以下のようになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Thread ID:9, i = 0 Thread ID:11, i = 0 Thread ID:10, i = 0 Thread ID:11, i = 1 Thread ID:9, i = 1 Thread ID:11, i = 2 Thread ID:10, i = 1 Thread ID:11, i = 3 Thread ID:9, i = 2 Thread ID:11, i = 4 Thread ID:10, i = 2 Thread ID:9, i = 3 Thread ID:9, i = 4 Thread ID:10, i = 3 Thread ID:10, i = 4 |
複数のスレッドが並列で動いていることが分かります。
待ち合わせ
joinメソッドを使用すれば、スレッドの終了を待ち合わせできます。実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class ThreadSample extends Thread { public void run() { for (int i = 0; i < 5; i++) { try { sleep(1000); } catch (InterruptedException e) {} System.out.println(""i = "" + i); } } } public class Main { public static void main(String[] args) throws Exception { ThreadSample threadSample = new ThreadSample(); threadSample.start(); try { System.out.println(""threadSample start""); // threadSampleの終了を待ち合わせ threadSample.join(); System.out.println(""threadSample end""); } catch (InterruptedException e) {} } } |
実行結果は以下のようになります。
1 2 3 4 5 6 7 |
threadSample start i = 0 i = 1 i = 2 i = 3 i = 4 threadSample end |
joinメソッドで、スレッドの終了を待ち合わせていることが分かります。
複数スレッド待ち合わせ
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 |
class ThreadSample extends Thread { public void run() { for (int i = 0; i < 5; i++) { try { sleep(1000); } catch (InterruptedException e) {} System.out.println(""Thread ID:"" + Thread.currentThread().getId() + "" i = "" + i); } } } public class Main { public static void main(String[] args) throws Exception { final int THREAD_NUM = 3; Thread[] threadArray = new Thread[THREAD_NUM]; for ( int i = 0; i < THREAD_NUM; i++ ) { ThreadSample threadSample = new ThreadSample(); threadArray[i] = threadSample; threadSample.start(); // タイムラグを発生させるためにsleep Thread.sleep(1000); } try { for (Thread th : threadArray) { th.join(); } System.out.println(""all thread end""); } catch (InterruptedException e) {} } } |
実行結果は以下のようになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Thread ID:9 i = 0 Thread ID:10 i = 0 Thread ID:9 i = 1 Thread ID:11 i = 0 Thread ID:10 i = 1 Thread ID:9 i = 2 Thread ID:11 i = 1 Thread ID:9 i = 3 Thread ID:10 i = 2 Thread ID:11 i = 2 Thread ID:10 i = 3 Thread ID:9 i = 4 Thread ID:11 i = 3 Thread ID:10 i = 4 Thread ID:11 i = 4 all thread end |
- SE
- javaのThreadの使い方がよく分かりました。
- PM
- さまざまな使い方が理解できたら、ご自身でソースコードを書いてさらに知識を深めましょう。
まとめ
いかがでしたでしょうか。JavaでThreadを使った非同期処理について説明しました。基本的な使い方から、待ち合わせ方法について紹介しました。
ぜひご自身でJavaのソースコードを書いて、理解を深めてください。