Java线程创建过程流程

时间:2013-12-18 08:35:29

标签: java multithreading

我正在阅读Brian Goetz在实践中阅读ThreadPools部分6.2.3 java并发部分。我遇到的一个声明是 “重用现有线程而不是创建新线程会分摊线程创建和拆卸成本。”

1)我想得到java线程创建过程中涉及的一些度量,我们知道这将包括创建/分配堆栈和程序计数器注册到创建的线程。是否有一个工具/实用程序/可视vm跟踪器/ jmx bean,我可以使用它可以给我一些关于内存和线程创建时间使用的指标。有人可以指导我一样吗?

2)是否有一个文本可以指导我详细介绍java线程创建的整个过程,它应该涵盖对Windows的各自OS调用?

Why is creating a Thread said to be expensive?给了我一些信息,但我想详细研究java线程创建的内部结构

由于

1 个答案:

答案 0 :(得分:0)

关于你的第二个问题:

  

2)是否有一个文本可以指导我整个java的过程   详细的线程创建应涵盖相应的OS调用   到了窗户?

本指南虽然略显偏离主题,但很棒:http://blog.jamesdbloom.com/JVMInternals.html

下面的书The Java Virtual Machine Specification, Java SE 7 Edition (google book link)深入解释了JVM的内部结构,因此这是我最好的选择(免责声明:我只通过可见部分浏览

如果这还不够好,您可以始终download open jdk (7)的源代码并抓取代码......

打开的jdk代码中的摘录是这样的(openjdk \ hotspot \ src \ share \ vm \ runtime \ thread.cpp - c'tor):

// Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
// JavaThread


Thread::Thread() {
  // stack and get_thread
  set_stack_base(NULL);
  set_stack_size(0);
  set_self_raw_id(0);
  set_lgrp_id(-1);

  // allocated data structures
  set_osthread(NULL);
  set_resource_area(new ResourceArea());
  set_handle_area(new HandleArea(NULL));
  set_active_handles(NULL);
  set_free_handle_block(NULL);
  set_last_handle_mark(NULL);

  // This initial value ==> never claimed.
  _oops_do_parity = 0;

  // the handle mark links itself to last_handle_mark
  new HandleMark(this);

  // plain initialization
  debug_only(_owned_locks = NULL;)
  debug_only(_allow_allocation_count = 0;)
  NOT_PRODUCT(_allow_safepoint_count = 0;)
  NOT_PRODUCT(_skip_gcalot = false;)
  CHECK_UNHANDLED_OOPS_ONLY(_gc_locked_out_count = 0;)
  _jvmti_env_iteration_count = 0;
  set_allocated_bytes(0);
  _vm_operation_started_count = 0;
  _vm_operation_completed_count = 0;
  _current_pending_monitor = NULL;
  _current_pending_monitor_is_from_java = true;
  _current_waiting_monitor = NULL;
  _num_nested_signal = 0;
  omFreeList = NULL ;
  omFreeCount = 0 ;
  omFreeProvision = 32 ;
  omInUseList = NULL ;
  omInUseCount = 0 ;

  _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
  _suspend_flags = 0;

  // thread-specific hashCode stream generator state - Marsaglia shift-xor form
  _hashStateX = os::random() ;
  _hashStateY = 842502087 ;
  _hashStateZ = 0x8767 ;    // (int)(3579807591LL & 0xffff) ;
  _hashStateW = 273326509 ;

  _OnTrap   = 0 ;
  _schedctl = NULL ;
  _Stalled  = 0 ;
  _TypeTag  = 0x2BAD ;

  // Many of the following fields are effectively final - immutable
  // Note that nascent threads can't use the Native Monitor-Mutex
  // construct until the _MutexEvent is initialized ...
  // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
  // we might instead use a stack of ParkEvents that we could provision on-demand.
  // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
  // and ::Release()
  _ParkEvent   = ParkEvent::Allocate (this) ;
  _SleepEvent  = ParkEvent::Allocate (this) ;
  _MutexEvent  = ParkEvent::Allocate (this) ;
  _MuxEvent    = ParkEvent::Allocate (this) ;

#ifdef CHECK_UNHANDLED_OOPS
  if (CheckUnhandledOops) {
    _unhandled_oops = new UnhandledOops(this);
  }
#endif // CHECK_UNHANDLED_OOPS
#ifdef ASSERT
  if (UseBiasedLocking) {
    assert((((uintptr_t) this) & (markOopDesc::biased_lock_alignment - 1)) == 0, "forced alignment of thread object failed");
    assert(this == _real_malloc_address ||
           this == (void*) align_size_up((intptr_t) _real_malloc_address, markOopDesc::biased_lock_alignment),
           "bug in forced alignment of thread objects");
  }
#endif /* ASSERT */
}