Runnable
| 日本語 | 実行可能 |
| 英語 | run able |
| ふりがな | らんなぶる |
| フリガナ | ランナブル |
スレッドを作るためのインターフェイス。
J2SEに含まれるインターフェイスのひとつ。パッケージも含めたクラス名はjava.lang.Runnable。
スレッドを作る場合、このRunnableインターフェイスの実装クラスを作るか、Threadクラスのサブクラスを作る。
Runnableインターフェイスのrun()メソッドをオーバーライドし、実装クラスをThreadクラスのコンストラクタに渡し、Threadクラスのstart()メソッドを呼び出すことで、オーバーライドしたrun()メソッドは別スレッドとして実行される。
J2SEに含まれるインターフェイスのひとつ。パッケージも含めたクラス名はjava.lang.Runnable。
スレッドを作る場合、このRunnableインターフェイスの実装クラスを作るか、Threadクラスのサブクラスを作る。
Runnableインターフェイスのrun()メソッドをオーバーライドし、実装クラスをThreadクラスのコンストラクタに渡し、Threadクラスのstart()メソッドを呼び出すことで、オーバーライドしたrun()メソッドは別スレッドとして実行される。
参考サイト
// Sample.java
public class Sample
{
public static void main( String[] args )
{
// TestRunnerクラスを作ります。
TestRunner testRunner1 = new TestRunner();
TestRunner testRunner2 = new TestRunner();
// toString()の結果を出力しておきます。
System.out.println( testRunner1.toString() );
System.out.println( testRunner2.toString() );
// TestRunner@152544e
// TestRunner@17471e0
// Threadクラスに渡します。
Thread thread1 = new Thread( testRunner1 );
Thread thread2 = new Thread( testRunner2 );
// そして実行。
thread1.start();
thread2.start();
// TestRunner@152544e : 0 : 4
// TestRunner@17471e0 : 0 : 3
// TestRunner@17471e0 : 1 : 9
// TestRunner@152544e : 1 : 8
// TestRunner@152544e : 2 : 7
// TestRunner@17471e0 : 2 : 1
// TestRunner@17471e0 : 3 : 3
// TestRunner@17471e0 : 4 : 3
// TestRunner@152544e : 3 : 5
// TestRunner@17471e0 : 終了。
// TestRunner@152544e : 4 : 8
// TestRunner@152544e : 終了。
// このように、ふたつとも関係なく実行されます。
// そして、main()メソッドは先に終了します。
System.out.println( "main() : 終了。" );
// main() : 終了。
// ↑これは、上のスレッドでの出力のどこかに
// 挟まれて出力されると思います。
}
}
/**
* 別スレッドとして実行するためのクラス。
*/
class TestRunner implements Runnable
{
/**
* Runnableクラスのrun()メソッドを
* オーバーライドしたメソッド。このメソッドが
* 別スレッドとして呼び出されます。
*/
public void run()
{
// 5周ループします。
for( int iF1 = 0; iF1 < 5; ++iF1 )
{
// 待機秒数を作ります。
int sleepSecond = ( ( (int)( Math.random() * 10 ) ) + 1 );
// 自分自身のtoString()と、カウンター、それと待機秒数を出力します。
System.out.println( this.toString() + " : " + iF1 + " : " + sleepSecond );
try
{
// ランダムで、1~10秒間待ちます。
Thread.sleep( sleepSecond * 1000 );
}
catch( InterruptedException e )
{
// sleep()メソッドが途中で中断されると
// InterruptedException例外が投げられます。
// 滅多にないですが。
e.printStackTrace();
}
}
// 終わりました。
System.out.println( this.toString() + " : 終了。" );
}
}
public class Sample
{
public static void main( String[] args )
{
// TestRunnerクラスを作ります。
TestRunner testRunner1 = new TestRunner();
TestRunner testRunner2 = new TestRunner();
// toString()の結果を出力しておきます。
System.out.println( testRunner1.toString() );
System.out.println( testRunner2.toString() );
// TestRunner@152544e
// TestRunner@17471e0
// Threadクラスに渡します。
Thread thread1 = new Thread( testRunner1 );
Thread thread2 = new Thread( testRunner2 );
// そして実行。
thread1.start();
thread2.start();
// TestRunner@152544e : 0 : 4
// TestRunner@17471e0 : 0 : 3
// TestRunner@17471e0 : 1 : 9
// TestRunner@152544e : 1 : 8
// TestRunner@152544e : 2 : 7
// TestRunner@17471e0 : 2 : 1
// TestRunner@17471e0 : 3 : 3
// TestRunner@17471e0 : 4 : 3
// TestRunner@152544e : 3 : 5
// TestRunner@17471e0 : 終了。
// TestRunner@152544e : 4 : 8
// TestRunner@152544e : 終了。
// このように、ふたつとも関係なく実行されます。
// そして、main()メソッドは先に終了します。
System.out.println( "main() : 終了。" );
// main() : 終了。
// ↑これは、上のスレッドでの出力のどこかに
// 挟まれて出力されると思います。
}
}
/**
* 別スレッドとして実行するためのクラス。
*/
class TestRunner implements Runnable
{
/**
* Runnableクラスのrun()メソッドを
* オーバーライドしたメソッド。このメソッドが
* 別スレッドとして呼び出されます。
*/
public void run()
{
// 5周ループします。
for( int iF1 = 0; iF1 < 5; ++iF1 )
{
// 待機秒数を作ります。
int sleepSecond = ( ( (int)( Math.random() * 10 ) ) + 1 );
// 自分自身のtoString()と、カウンター、それと待機秒数を出力します。
System.out.println( this.toString() + " : " + iF1 + " : " + sleepSecond );
try
{
// ランダムで、1~10秒間待ちます。
Thread.sleep( sleepSecond * 1000 );
}
catch( InterruptedException e )
{
// sleep()メソッドが途中で中断されると
// InterruptedException例外が投げられます。
// 滅多にないですが。
e.printStackTrace();
}
}
// 終わりました。
System.out.println( this.toString() + " : 終了。" );
}
}
// Sample.java
public class Sample
{
public static void main( String[] args )
{
// TestRunnerクラスを作ります。
TestRunner testRunner1 = new TestRunner();
TestRunner testRunner2 = new TestRunner();
// toString()の結果を出力しておきます。
System.out.println( testRunner1.toString() );
System.out.println( testRunner2.toString() );
// TestRunner@152544e
// TestRunner@17471e0
// Threadクラスに渡します。
Thread thread1 = new Thread( testRunner1 );
Thread thread2 = new Thread( testRunner2 );
// そして実行。
thread1.start();
thread2.start();
// TestRunner@152544e : 0 : 4
// TestRunner@17471e0 : 0 : 3
// TestRunner@17471e0 : 1 : 9
// TestRunner@152544e : 1 : 8
// TestRunner@152544e : 2 : 7
// TestRunner@17471e0 : 2 : 1
// TestRunner@17471e0 : 3 : 3
// TestRunner@17471e0 : 4 : 3
// TestRunner@152544e : 3 : 5
// TestRunner@17471e0 : 終了。
// TestRunner@152544e : 4 : 8
// TestRunner@152544e : 終了。
// このように、ふたつとも関係なく実行されます。
// そして、main()メソッドは先に終了します。
System.out.println( "main() : 終了。" );
// main() : 終了。
// ↑これは、上のスレッドでの出力のどこかに
// 挟まれて出力されると思います。
}
}
/**
* 別スレッドとして実行するためのクラス。
*/
class TestRunner implements Runnable
{
/**
* Runnableクラスのrun()メソッドを
* オーバーライドしたメソッド。このメソッドが
* 別スレッドとして呼び出されます。
*/
public void run()
{
// 5周ループします。
for( int iF1 = 0; iF1 < 5; ++iF1 )
{
// 待機秒数を作ります。
int sleepSecond = ( ( (int)( Math.random() * 10 ) ) + 1 );
// 自分自身のtoString()と、カウンター、それと待機秒数を出力します。
System.out.println( this.toString() + " : " + iF1 + " : " + sleepSecond );
try
{
// ランダムで、1~10秒間待ちます。
Thread.sleep( sleepSecond * 1000 );
}
catch( InterruptedException e )
{
// sleep()メソッドが途中で中断されると
// InterruptedException例外が投げられます。
// 滅多にないですが。
e.printStackTrace();
}
}
// 終わりました。
System.out.println( this.toString() + " : 終了。" );
}
}




