java序列化:ejb和java序列化(2)--测试代码

  接上篇有兴趣朋友可以直接拿我测试代码自行测试,请自行修改诸如线程数执行时间序列化数据量大小等参数如果想尝试做thread dump可以打开相关两个注释会更方便代码中都有相应注释可供参考

  测试代码如下:

package test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public Test implements Runnable {
  //Notice! the three test parameter to what you want first
  /**
   * thread count to run test
   */
  private final THREAD_COUNT = 50;
  /**
   * time in seconds to run test
   */
  private final long TEST_TIME_SECOND = 1 * 30;
  /**
   * during test, we serialize a Data instance with an ArrayList that contains DataItem instance.
   * This is to how many DataItem in the ArrayList.
   */
  private final long ITEMS_COUNT_IN_TEST_OBJECT = 1000;
  private finishedCount = 0;
  private boolean needStop = false;
  private Object needStopLock = Object;
  private Object finishedCountLock = Object;
  private boolean isNeedStop {
    synchronized (needStopLock) {
       needStop;
    }
  }
  private void NeedStop {
    synchronized (needStopLock) {
      needStop = true;
    }
  }
  private void addFinisedCount {
    synchronized (finishedCountLock) {
      finishedCount;
    }
  }
  /**
   * @param args
   */
  public void (String args) {
    // run it first to load all the
     Test.test;
    // to dump thread open these
    // try {
    // Thread.sleep(20 * 1000);
    // .out.prln(" sleep. go to find pid, we need it later to send signal");
    // Thread.sleep(2 * 1000);
    // .out.prln("prepard to dump");
    // } catch (InterruptedException e) {
    // e.prStackTrace;
    // }
    long timeBegin = .currentTimeMillis;
    for ( i = 0; i < THREAD_COUNT; i) {
      Thread t = Thread( Test);
      t.Name("testthread" + i);
      t.start;
    }
    long timeEnd = timeBegin + TEST_TIME_SECOND * 1000;
    while (.currentTimeMillis < timeEnd) {
      try {
        Thread.sleep(50);
      } catch (InterruptedException e) {
        e.prStackTrace;
      }
    }
    NeedStop;
    .out.prln(THREAD_COUNT + " thread finished " + finishedCount
        + " times in " + TEST_TIME_SECOND + " seconds");
    // to dump thread open these
    // try {
    // Thread.sleep(5 * 1000);
    // //.out.prln("dump now");
    // } catch (InterruptedException e) {
    // e.prStackTrace;
    // }
  }
  public void run {
    while (!isNeedStop) {
      test;
      addFinisedCount;
    }
  }
  private void test {
    ByteArrayOutputStream bos = ByteArrayOutputStream;
    Data data = Data;
    try {
      // long time1 = .currentTimeMillis;
      ObjectOutputStream oos = ObjectOutputStream(bos);
      oos.writeObject(data);
      bos.toByteArray;
      // long time2 = .currentTimeMillis;
      // .out.pr((time2 - time1) + " ");
    } catch (IOException e) {
      e.prStackTrace;
    }
  }
  private Data implements Serializable {
    private final long serialVersionUID = -376987039014824563L;
    private final ArrayList DEFAULT = ArrayList;
     {
      for ( i = 0; i < ITEMS_COUNT_IN_TEST_OBJECT; i) {
        DEFAULT.add( DataItem(i));
        // DEFAULT.add(DataItem.a + i);
      }
    }
    private ArrayList content = DEFAULT;
  }
  private DataItem implements Serializable {
    private final long serialVersionUID = 1L;
    private final String a = "sdfsdfsdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
    private number;
    private String content;
    public DataItem( number) {
      this.number = number;
      this.content = a + number;
    }
  }
}


  也可以从这里直接下载到完整eclipse项目除上面代码外还有thread dump文件和已经设置好jprobe配置文件

  (blogjava不能上传文件所以只好放fs2you)

  http://www.fs2you.com/files/59a26119-5d1a-11dd-ad4f-0014221b798a/



Tags:  java反序列化 java什么是序列化 java对象序列化 java序列化

延伸阅读

最新评论

发表评论