gpt4 book ai didi

Java线程创建流程

转载 作者:行者123 更新时间:2023-11-29 08:55:10 26 4
gpt4 key购买 nike

我正在阅读 Brian Goetz 的 ThreadPools 部分 6.2.3 java concurrency in practice。我遇到的一个说法是“重用现有线程而不是创建新线程可以分摊线程创建和拆卸成本。”

1) 我想获得 java 线程创建过程中涉及的一些指标,我们知道这将涉及堆栈的创建/分配和程序计数器寄存器到创建的线程。是否有我可以使用的工具/实用程序/可视化虚拟机跟踪器/jmx bean,它可以为我提供一些关于线程创建的内存和时间使用情况的指标。有人可以指导我做同样的事情吗?

2) 是否有文本可以详细指导我创建 java 线程的整个过程,其中应该涵盖对 windows 的各个操作系统调用?

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

谢谢

最佳答案

关于你的第二个问题:

2) Is there a text which can guide me to the whole process of java thread creation in detail which should cover the respective OS calls to windows ?

本指南虽然有点偏离主题,但很棒: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 */
}

关于Java线程创建流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20653149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com