スレッドの同期・非同期


::::::::::::::
AbstractService.java
::::::::::::::
/**
* $Id: Thread-Synchronizing.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public abstract class AbstractService {

    protected int id;

    protected AbstractService( int id ) {
        this.id = id;
    }

}
::::::::::::::
Service.java
::::::::::::::
/**
* $Id: Thread-Synchronizing.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public interface Service {

    public void doService();
    public int getId();

}

::::::::::::::
ServiceImpl.java
::::::::::::::
/**
* $Id: Thread-Synchronizing.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class ServiceImpl extends AbstractService implements Service {

    public ServiceImpl( int id ) {
        super( id );
    }
    public void doService() {
        System.out.printf( "%3d * %3d = %6d\n", id, id, id * id );
    }

    public int getId() {
        return id;
    }

    public static void main( String[] args ) {
        Service service = new ServiceImpl( 10 );
        service.doService();
    }
}


::::::::::::::
ServiceThread.java
::::::::::::::
/**
* $Id: Thread-Synchronizing.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class ServiceThread implements Runnable {

    private Service service = null;

    public void setService( Service service ) {
        this.service = service;
    }

    public void run() {

        long sleepTime = service.getId() % 8 * 100;
        try {
            Thread.sleep( sleepTime );
        } catch ( Exception e ) {
            e.printStackTrace();
        }

        // ビジネス・ロジックの実行
        service.doService();

    }

}
::::::::::::::
TMain.java
::::::::::::::
/**
* $Id: Thread-Synchronizing.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $
* @author KISHI Yasuhiro
* <pre>起動するスレッドの数を制御する</pre>
*/
public class TMain {
    public static void main( String[] args ) {

        // 5個のサービス実行終了を同期させる処理を10回繰り返す
        int index = 0;
        for ( int i = 0;i < 10;i++ ) {
            for ( int j = 0;j < 5;j++ ) {
                ServiceThread st = new ServiceThread();
                st.setService( new ServiceImpl( index++ ) );
                Thread thread = new Thread( st );
                thread.start();
                try {
                    thread.join();
                } catch ( InterruptedException e ) {
                    e.printStackTrace();
                }
            }
            System.out.println( "** FINISHED **" );
        }

    }

}

■以下実行結果

$ java -cp . TMain
  0 *   0 =      0
  1 *   1 =      1
  2 *   2 =      4
  3 *   3 =      9
  4 *   4 =     16
** FINISHED **
  5 *   5 =     25
  6 *   6 =     36
  7 *   7 =     49
  8 *   8 =     64
  9 *   9 =     81
** FINISHED **
 10 *  10 =    100
 11 *  11 =    121
 12 *  12 =    144
 13 *  13 =    169
 14 *  14 =    196
** FINISHED **
 15 *  15 =    225
 16 *  16 =    256
 17 *  17 =    289
 18 *  18 =    324
 19 *  19 =    361
** FINISHED **
 20 *  20 =    400
 21 *  21 =    441
 22 *  22 =    484
 23 *  23 =    529
 24 *  24 =    576
** FINISHED **
 25 *  25 =    625
 26 *  26 =    676
 27 *  27 =    729
 28 *  28 =    784
 29 *  29 =    841
** FINISHED **
 30 *  30 =    900
 31 *  31 =    961
 32 *  32 =   1024
 33 *  33 =   1089
 34 *  34 =   1156
** FINISHED **
 35 *  35 =   1225
 36 *  36 =   1296
 37 *  37 =   1369
 38 *  38 =   1444
 39 *  39 =   1521
** FINISHED **
 40 *  40 =   1600
 41 *  41 =   1681
 42 *  42 =   1764
 43 *  43 =   1849
 44 *  44 =   1936
** FINISHED **
 45 *  45 =   2025
 46 *  46 =   2116
 47 *  47 =   2209
 48 *  48 =   2304
 49 *  49 =   2401
** FINISHED **


今度は、同期しないで勝手に各々実行をするように変更してみます。 /** * $Id: Thread-Synchronizing.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $ * @author KISHI Yasuhiro * <pre>起動するスレッドの数を制御する</pre> */ public class TMain { public static void main( String[] args ) { int index = 0; for ( int i = 0;i < 10;i++ ) { for ( int j = 0;j < 5;j++ ) { ServiceThread st = new ServiceThread(); st.setService( new ServiceImpl( index++ ) ); Thread thread = new Thread( st ); thread.start(); /** try { thread.join(); } catch ( InterruptedException e ) { e.printStackTrace(); } **/ } // System.out.println( "** FINISHED **" ); } } } ■fork/joinを使用しないで実行した場合の結果 $ java -cp . TMain 0 * 0 = 0 8 * 8 = 64 16 * 16 = 256 24 * 24 = 576 32 * 32 = 1024 40 * 40 = 1600 1 * 1 = 1 48 * 48 = 2304 9 * 9 = 81 17 * 17 = 289 25 * 25 = 625 33 * 33 = 1089 2 * 2 = 4 41 * 41 = 1681 49 * 49 = 2401 10 * 10 = 100 18 * 18 = 324 26 * 26 = 676 34 * 34 = 1156 3 * 3 = 9 42 * 42 = 1764 11 * 11 = 121 19 * 19 = 361 27 * 27 = 729 35 * 35 = 1225 4 * 4 = 16 43 * 43 = 1849 12 * 12 = 144 20 * 20 = 400 28 * 28 = 784 36 * 36 = 1296 44 * 44 = 1936 5 * 5 = 25 13 * 13 = 169 21 * 21 = 441 29 * 29 = 841 37 * 37 = 1369 45 * 45 = 2025 6 * 6 = 36 14 * 14 = 196 22 * 22 = 484 30 * 30 = 900 38 * 38 = 1444 46 * 46 = 2116 7 * 7 = 49 15 * 15 = 225 23 * 23 = 529 31 * 31 = 961 39 * 39 = 1521 47 * 47 = 2209

  1. asistobeのメモ
inserted by FC2 system