-
Notifications
You must be signed in to change notification settings - Fork 103
Lesson 60 (thread) #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: for-pr
Are you sure you want to change the base?
Lesson 60 (thread) #105
Conversation
|
|
||
| DateTimeFormatter mediumTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM); | ||
|
|
||
| Thread timePrinter = new Thread(printTimeCyclical(twoSeconds, mediumTimeFormatter), "timePrinter"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
избыточная переменная
| public static Runnable printTimeCyclical(Duration duration, DateTimeFormatter formatter) { | ||
| return () -> { | ||
| while (!Thread.currentThread() | ||
| .isInterrupted()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
в общем-то, логика понятна, но практически избыточна. Как правило, проверка на прерывание используется тогда, когда действительно есть шанс, что поток будет прерван
| while (!Thread.currentThread() | ||
| .isInterrupted()) { | ||
| System.out.println(LocalDateTime.now() | ||
| .format(formatter)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
подобные переносы выглядят не очень. Лучше уж в одну строку. Или выноси в переменную, если хочется перенести
| .interrupt(); | ||
| } | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
как по мне - переусложнил, но в точки зрения попрактиковаться с новым API - норм
| public static <T> void print2DArray(int[][] array) { | ||
| for (int[] row : array) { | ||
| for (int column : row) { | ||
| System.out.printf("[%s]".formatted(column)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
если printf() - formatted() лишний
| tableFiller.join(); | ||
| } | ||
|
|
||
| private Runnable tableFillingProcess(int[][] table, IntSupplier value) throws InterruptedException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
методы именуются с глагола
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }, "rowFiller"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
имена тредов обычно не задаются вручную - в этом мало смысла, пока нет тред пулов. С ними познакомишься позже (или уже познакомился)
| row[i] = value.getAsInt(); | ||
| } | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
как будто можно было описать лаконичнее/декомпозировать иначе. И, опять же, переусложняешь
| try { | ||
| fillRow(row, partCount, value); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем? В каком случае может вывалиться интерраптед ниже по стеку?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
как будто join закомментил, а здесь не подчистил
| } | ||
| } | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В виде стрима получилось бы красивее:)
| private Runnable tableFillingProcess(int[][] table, IntSupplier value) { | ||
| return () -> { | ||
| for (int[] row : table) { | ||
| Arrays.setAll(row, x -> value.getAsInt()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Даже не знал о таком методе:)
| public class FunctionExecutor<T, R> implements Callable<R> { | ||
| T value; | ||
| Function<T, R> function; | ||
| R result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
что с идентификаторами доступа?
|
|
||
| return result; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Намудрил. Зачем Callable в этом решении? Здесь он как будто из-за рекомендации в условии. Но именно в этой имплементации он оказался не нужен:)
Также, почему Function? Вполне возможна ситуация, когда входного параметра нет. Или их несколько. Как будто неудачный подбор функционального интерфейса
|
|
||
| thread.start(); | ||
|
|
||
| while (true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
можно упростить, засунув условие if в условие цикла
No description provided.