how to get a seq No among several threads.

--Main.jave---------------------------------------
package pkg.y2009.m06.test001;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import pkg.test001.Runnablee;
import pkg.test001.Runnablee2;

public class Main {

  /**
   * @param args
   */
  public static void main(String[] args) {
    try {
      test001();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

  }

  /**
   * how to get a seq no among several threads.
   * @throws InterruptedException
   *
   */
  private static void test001() throws InterruptedException {
    int hangingTime = 2000;
    int runnerListSize = 10;
    
    ////////////////////Runnablee
    int subId = 1;
    Thread.currentThread().getThreadGroup().list();
    List list = Runnablee.getInstanceList(runnerListSize, subId);
    for (Runnablee t : list) t.run();//1st execution
    Thread.sleep(hangingTime);
    
    subId = 2;
    Thread.currentThread().getThreadGroup().list();
    list = Runnablee.getInstanceList(runnerListSize, subId);
    for (Runnablee t : list) t.run();//renew and execute again
    Thread.sleep(hangingTime);
    
    subId = 3;
    Thread.currentThread().getThreadGroup().list();
    list = Runnablee.getInstanceList(runnerListSize, subId);
    ExecutorService es = Executors.newFixedThreadPool(2);
    for (Runnablee t : list) es.execute(t);
    es.shutdown();
    Thread.sleep(hangingTime);
    
    subId = 4;
    Thread.currentThread().getThreadGroup().list();
    list = Runnablee.getInstanceList(runnerListSize, subId);
    es = Executors.newCachedThreadPool();
    for (Runnablee t : list) es.execute(t);
    es.shutdown();
    Thread.sleep(hangingTime);
    
    Thread.currentThread().getThreadGroup().list();
    
    ////////////////////Runnablee2
    subId = 1;
    Thread.currentThread().getThreadGroup().list();
    List list2 = Runnablee2.getInstanceList(runnerListSize, subId);
    for (Runnablee2 t : list2) t.run();//1st execution
    Thread.sleep(hangingTime);
    
    subId = 2;
    Thread.currentThread().getThreadGroup().list();
    list2 = Runnablee2.getInstanceList(runnerListSize, subId);
    for (Runnablee2 t : list2) t.run();//renew and execute again
    Thread.sleep(hangingTime);
    
    subId = 3;
    Thread.currentThread().getThreadGroup().list();
    list2 = Runnablee2.getInstanceList(runnerListSize, subId);
    ExecutorService es2 = Executors.newFixedThreadPool(2);
    for (Runnablee2 t : list2) es2.execute(t);
    es2.shutdown();
    Thread.sleep(hangingTime);
    
    subId = 4;
    Thread.currentThread().getThreadGroup().list();
    list2 = Runnablee2.getInstanceList(runnerListSize, subId);
    es2 = Executors.newCachedThreadPool();
    for (Runnablee2 t : list2) es2.execute(t);
    es2.shutdown();
    Thread.sleep(hangingTime);

    Thread.currentThread().getThreadGroup().list();
  }

}
--/Main.jave--------------------------------------
--Runnablee.jave----------------------------------
package pkg.y2009.m06.test001;

import java.util.ArrayList;
import java.util.List;

public class Runnablee implements Runnable {

  static final String SYSOUT_PREFIX = "【TEST001】";

  static {
    System.out.println("Runnablee.threadId has the overrided get-method which returns next value.");
  }

  public static int SEQ = 0;

  public static synchronized int next() {
    return ++SEQ;
  }

  public static ThreadLocal threadId = new ThreadLocal() {

    @Override
    public Integer get() {
      return next();
    }

    @Override
    protected Integer initialValue() {
      return next();
    }

  };

  int subId = -1;

  public Runnablee(int subId) {
    this.subId = subId; 
  }


//  @Override
  public void run() {

    sysout(subId, this.hashCode(), threadId.get(), Thread.currentThread().getId(), this.getClass().getName());

  }

  static void sysout(int subId, int hashCode, Integer seq, long threadId, String className) {
    System.out.println(SYSOUT_PREFIX + className + "(" + subId + "): " + seq
        + "\t (threadId: " + threadId + ")" + " [ " + hashCode + " ]");
  }

  public static List getInstanceList(int listSize, int subId) {
    List list = new ArrayList();
    for (int i = 1; i <= listSize; i++) {
      list.add(new Runnablee(subId));
    }
    return list;
  }

}
--/Runnablee.jave---------------------------------
--Runnablee2.jave---------------------------------
package pkg.y2009.m06.test001;

import java.util.ArrayList;
import java.util.List;

public class Runnablee2 implements Runnable {

  static {
    System.out.println("Runnablee2.threadId2 doesnt have the overrided get-method.");
  }

  public static int SEQ2 = 0;

  public static synchronized int next2() {
    return ++SEQ2;
  }

  /**
   * no get overriding.
   */
  public static ThreadLocal threadId2 = new ThreadLocal() {

    @Override
    protected Integer initialValue() {
      return next2();
    }

  };

  public Runnablee2(int subId) {
    this.subId = subId; 
  }
  
  int subId = -1;
  
//  @Override
  public void run() {

    Runnablee.sysout(subId, this.hashCode(), threadId2.get(), Thread.currentThread().getId(), this.getClass().getName());

  }

  public static List getInstanceList(int listSize, int subId) {
    List list = new ArrayList();
    for (int i = 1; i <= listSize; i++) {
      list.add(new Runnablee2(subId));
    }
    return list;
  }

}
--/Runnablee2.jave--------------------------------
--console log-------------------------------------
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
Runnablee.threadId has the overrided get-method which returns next value.
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 1  (threadId: 1) [ 11394033 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 2  (threadId: 1) [ 4384790 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 3  (threadId: 1) [ 9634993 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 4  (threadId: 1) [ 1641745 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 5  (threadId: 1) [ 11077203 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 6  (threadId: 1) [ 14576877 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 7  (threadId: 1) [ 12677476 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 8  (threadId: 1) [ 33263331 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 9  (threadId: 1) [ 6413875 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(1): 10  (threadId: 1) [ 21174459 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 11  (threadId: 1) [ 827574 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 12  (threadId: 1) [ 17510567 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 13  (threadId: 1) [ 27744459 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 14  (threadId: 1) [ 6927154 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 15  (threadId: 1) [ 24355087 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 16  (threadId: 1) [ 5442986 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 17  (threadId: 1) [ 10891203 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 18  (threadId: 1) [ 9023134 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 19  (threadId: 1) [ 19336051 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(2): 20  (threadId: 1) [ 6336176 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 21  (threadId: 7) [ 21091971 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 22  (threadId: 7) [ 23746042 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 23  (threadId: 7) [ 3327002 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 24  (threadId: 7) [ 15244180 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 25  (threadId: 7) [ 20848735 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 26  (threadId: 7) [ 29324282 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 27  (threadId: 7) [ 5718203 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 28  (threadId: 8) [ 5947506 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 29  (threadId: 7) [ 1088076 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(3): 30  (threadId: 8) [ 131577 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 31  (threadId: 9) [ 12848256 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 32  (threadId: 10) [ 14410104 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 33  (threadId: 11) [ 28286498 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 34  (threadId: 12) [ 11988197 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 35  (threadId: 13) [ 24825485 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 36  (threadId: 13) [ 26130918 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 37  (threadId: 13) [ 21944831 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 38  (threadId: 12) [ 30028170 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 39  (threadId: 14) [ 24807699 ]
【TEST001】pkg.y2009.m06.test001.Runnablee(4): 40  (threadId: 13) [ 29193342 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
Runnablee2.threadId2 doesnt have the overrided get-method.
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 22485126 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 17514905 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 33536220 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 29459700 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 33537384 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 15472292 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 29530802 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 20591247 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 30246505 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(1): 1  (threadId: 1) [ 3373112 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 18468004 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 13249998 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 25516242 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 25839584 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 24431647 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 17108511 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 14831146 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 24756869 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 13755908 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(2): 1  (threadId: 1) [ 5510959 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 32320232 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 27010805 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 12694833 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 22982825 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 27284232 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 11227341 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 21352552 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 3  (threadId: 16) [ 7704521 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 2  (threadId: 15) [ 3341135 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(3): 3  (threadId: 16) [ 17905416 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 4  (threadId: 17) [ 23105029 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 5  (threadId: 18) [ 783148 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 6  (threadId: 19) [ 9042915 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 6  (threadId: 19) [ 16259139 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 5  (threadId: 18) [ 56667 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 4  (threadId: 17) [ 12893404 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 7  (threadId: 20) [ 4932403 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 6  (threadId: 19) [ 19456522 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 5  (threadId: 18) [ 17829242 ]
【TEST001】pkg.y2009.m06.test001.Runnablee2(4): 4  (threadId: 17) [ 15006066 ]
java.lang.ThreadGroup[name=main,maxpri=10]
  Thread[main,5,main]
--/console log------------------------------------
2009-06-22 01:37 : __lang__java : コメント : 0 : トラックバック : 0 :
コメントの投稿
非公開コメント

« next  ホーム  prev »

search

ad



counter


tag cloud

category cloud