목차
출력
Java java지도 시간 Java에서 StackWalker API를 사용하여 다양한 스택 프레임을 인쇄하는 방법은 무엇입니까?

Java에서 StackWalker API를 사용하여 다양한 스택 프레임을 인쇄하는 방법은 무엇입니까?

Sep 06, 2023 pm 08:17 PM

如何使用Java中的StackWalker API打印不同的堆栈帧?

Java 9는 게으름과 프레임 필터링을 제공하는 StackWalkerAPI를 정의합니다. StackWalker 개체를 사용하면 스택을 걷고 액세스할 수 있으며 유용한 메서드인 walk()가 포함되어 있습니다. 이 메서드는 현재 스레드에 대한 StackFramestream을 연 다음 해당 StackFrame스트림에 함수를 적용합니다. StackWalker 개체를 가져온 다음 StackWalker.getInstance() 메서드를 사용해야 합니다.

아래 예에서는 StackWalkerAPI를 사용하여 모든 스택 프레임, 건너뛰기 특정 스택 프레임 및 제한 스택 프레임 등 다양한 콘텐츠 스택 프레임을 인쇄할 수 있습니다. p>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

import java.lang.StackWalker.StackFrame;

import java.util.*;

import java.util.stream.*;

 

public class StackWalkerTest {

   public static void main(String args[]) {

      new StackWalkerTest().walk();

   }

   private void walk() {

      new Walker1().walk();

   }

   private class Walker1 {

      public void walk() {

         new Walker2().walk();

      }

   }

   private class Walker2 {

      public void walk() {

         Method1();

      }

      void Method1() {

         Method2();

      }

      void Method2() {

         StackWalker stackWalker = <strong>StackWalker.getInstance</strong>(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES), 16);

         <strong>Stream<StackFrame></strong> stackStream = <strong>StackWalker.getInstance().walk</strong>(f -> f);

 

         System.out.println("--- Walk all StackFrames ---");

         <strong>List<String></strong> stacks = walkAllStackframes();

         System.out.println(stacks);

 

         System.out.println("--- Skip some StackFrames ---");

         <strong>List<String></strong> stacksAfterSkip = walkSomeStackframes(3);

         System.out.println(stacksAfterSkip);

 

         System.out.println("--- Limit StackFrames ---");

         <strong>List<String></strong> stacksByLimit = walkLimitStackframes(3);

         System.out.println(stacksByLimit);

      }

      private List<String> walkAllStackframes() {

         return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>collect</strong>(Collectors.toList()));

      }

      private List<String> walkSomeStackframes(int numberOfFrames) {

         return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>skip</strong>(numberOfFrames).<strong>collect</strong>(Collectors.toList()));

      }

      private List<String> walkLimitStackframes(int numberOfFrames) {

         return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>limit</strong>(numberOfFrames).<strong>collect</strong>(Collectors.toList()));

      }

   }

}

로그인 후 복사

출력

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<strong>--- Walk all StackFrames ---

[

StackWalkerTest$Walker2/walkAllStackframes,

StackWalkerTest$Walker2/Method2,

StackWalkerTest$Walker2/Method1,

StackWalkerTest$Walker2/walk,

StackWalkerTest$Walker1/walk,

StackWalkerTest/walk,

StackWalkerTest/main</strong>

<strong>]

--- Skip some StackFrames ---

[

StackWalkerTest$Walker2/walk,

StackWalkerTest$Walker1/walk,

StackWalkerTest/walk,

StackWalkerTest/main</strong>

<strong>]

--- Limit StackFrames ---

[

StackWalkerTest$Walker2/walkLimitStackframes,

StackWalkerTest$Walker2/Method2,

StackWalkerTest$Walker2/Method1</strong>

<strong>]</strong>

로그인 후 복사

위 내용은 Java에서 StackWalker API를 사용하여 다양한 스택 프레임을 인쇄하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. 크로스 플레이가 있습니까?
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)