Commit b27dea1f authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Updating the example that shows multiple stacks

parent d1901c07
Loading
Loading
Loading
Loading
+37 −0
Changes for src/main/java/ch15/MyRunnable.java: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15;

public class MyRunnable implements Runnable {

   public void run() {
      go();
   }

   public void go() {
      doMore();
   }

   public void doMore() {
      System.out.println(Thread.currentThread().getName() + ": top o’ the stack \n" + StackUtils.getCurrentStack());
   }

}

class ThreadTester {
   public static void main (String[] args) {
      Runnable threadJob = new MyRunnable();
      Thread myThread = new Thread(threadJob);

      myThread.start();

      System.out.println(Thread.currentThread().getName() + ": back in main \n" + StackUtils.getCurrentStack());
   }
}

class StackUtils {
   static String getCurrentStack() {
      StringBuffer stack = new StringBuffer();
      StackWalker.getInstance()
                 .forEach(stackFrame -> stack.append(stackFrame.toString()).append("\n"));
      return stack.toString();
   }
}