Created
August 26, 2017 13:34
-
-
Save Chikitonik/6db052dcb3f98b134a25b5a4babfcb32 to your computer and use it in GitHub Desktop.
JavaRush Thread поток методов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Кто меня вызывал? | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws Exception { | |
method1(); | |
} | |
public static int method1() { | |
method2(); | |
StackTraceElement[] a = Thread.currentThread().getStackTrace(); | |
System.out.println("Я метод "+a[1].getMethodName()); | |
System.out.println("Меня вызвал "+a[2].getMethodName()); | |
System.out.println("Вызов произошел из строки номер "+a[2].getLineNumber()); | |
return /*напишите тут ваш код*/ a[2].getLineNumber(); | |
//решение в одну строку: | |
//return Thread.currentThread().getStackTrace()[2].getLineNumber(); | |
//дополнительно, как вызвать весь поток методов: | |
//for (StackTraceElement b:a){ | |
// System.out.println("Метод по очереди "+b.getMethodName()); | |
//} | |
} | |
public static int method2() { | |
method3(); | |
StackTraceElement[] a = Thread.currentThread().getStackTrace(); | |
System.out.println("Я метод "+a[1].getMethodName()); | |
System.out.println("Меня вызвал "+a[2].getMethodName()); | |
System.out.println("Вызов произошел из строки номер "+a[2].getLineNumber()); | |
return /*напишите тут ваш код*/ a[2].getLineNumber(); | |
} | |
public static int method3() { | |
method4(); | |
StackTraceElement[] a = Thread.currentThread().getStackTrace(); | |
System.out.println("Я метод "+a[1].getMethodName()); | |
System.out.println("Меня вызвал "+a[2].getMethodName()); | |
System.out.println("Вызов произошел из строки номер "+a[2].getLineNumber()); | |
return /*напишите тут ваш код*/ a[2].getLineNumber(); | |
} | |
public static int method4() { | |
method5(); | |
StackTraceElement[] a = Thread.currentThread().getStackTrace(); | |
System.out.println("Я метод "+a[1].getMethodName()); | |
System.out.println("Меня вызвал "+a[2].getMethodName()); | |
System.out.println("Вызов произошел из строки номер "+a[2].getLineNumber()); | |
return /*напишите тут ваш код*/ a[2].getLineNumber(); | |
} | |
public static int method5() { | |
StackTraceElement[] a = Thread.currentThread().getStackTrace(); | |
System.out.println("Я метод "+a[1].getMethodName()); | |
System.out.println("Меня вызвал "+a[2].getMethodName()); | |
System.out.println("Вызов произошел из строки номер "+a[2].getLineNumber()); | |
return /*напишите тут ваш код*/ a[2].getLineNumber(); | |
} | |
} |
Спасибо
Спасибо тебе, только сейчас понял что к чему
Спасибо тебе большое. Я хоть что-то понял за в этом
Thank you!
Дякую!
Благодарю!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Дай бог здоровья тебе, очень доходчиво.