diff --git a/src/share/vm/jfr/periodic/jfrPeriodic.cpp b/src/share/vm/jfr/periodic/jfrPeriodic.cpp index 67a6d614c4e47267fdf7d203d917eb96ba3e9b78..af2df2a7ed2915e736543f5978a037df91f5679d 100644 --- a/src/share/vm/jfr/periodic/jfrPeriodic.cpp +++ b/src/share/vm/jfr/periodic/jfrPeriodic.cpp @@ -395,12 +395,17 @@ TRACE_REQUEST_FUNC(ThreadAllocationStatistics) { GrowableArray allocated(initial_size); GrowableArray thread_ids(initial_size); JfrTicks time_stamp = JfrTicks::now(); - JfrJavaThreadIterator iter; - while (iter.has_next()) { - JavaThread* const jt = iter.next(); - assert(jt != NULL, "invariant"); - allocated.append(jt->cooked_allocated_bytes()); - thread_ids.append(JFR_THREAD_ID(jt)); + + { + // Collect allocation statistics while holding threads lock + MutexLockerEx ml(Threads_lock); + JfrJavaThreadIterator iter; + while (iter.has_next()) { + JavaThread* const jt = iter.next(); + assert(jt != NULL, "invariant"); + allocated.append(jt->cooked_allocated_bytes()); + thread_ids.append(JFR_THREAD_ID(jt)); + } } // Write allocation statistics to buffer. diff --git a/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp b/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp index 2079fcb28c0c2ccef3bb6a34aa22040221545781..7b6024f75650e88bbe7f9bed56dd53cd40421ea9 100644 --- a/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp +++ b/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp @@ -108,6 +108,7 @@ void JfrCheckpointThreadClosure::do_thread(Thread* t) { void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) { JfrCheckpointThreadClosure tc(writer); + MutexLockerEx ml(Threads_lock); JfrJavaThreadIterator javathreads; while (javathreads.has_next()) { tc.do_thread(javathreads.next());