github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/examples/java-jfr/fib/Main.java (about) 1 import java.util.concurrent.locks.Lock; 2 import java.util.concurrent.locks.ReentrantLock; 3 4 class MyRunnable implements Runnable { 5 private Lock lock; 6 7 public MyRunnable(Lock lock) { 8 this.lock = lock; 9 } 10 11 public static long fib(long n) { 12 if (n < 2) 13 return n; 14 return fib(n-1) + fib(n-2); 15 } 16 17 public void run() { 18 while (true) { 19 this.lock.lock(); 20 try { 21 fib(40); 22 } finally { 23 this.lock.unlock(); 24 } 25 } 26 } 27 } 28 29 class Main { 30 public static long fib(long n) { 31 if (n < 2) 32 return n; 33 return fib(n-1) + fib(n-2); 34 } 35 36 public static void main(String[] args) { 37 Lock l = new ReentrantLock(); 38 Runnable r = new MyRunnable(l); 39 new Thread(r).start(); 40 41 while (true) { 42 l.lock(); 43 try { 44 fib(40); 45 } finally { 46 l.unlock(); 47 } 48 } 49 } 50 } 51