Skip to content

Conversation

@Binary-Cat-01
Copy link

No description provided.


DateTimeFormatter mediumTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);

Thread timePrinter = new Thread(printTimeCyclical(twoSeconds, mediumTimeFormatter), "timePrinter");
Copy link
Owner

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()) {
Copy link
Owner

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));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

подобные переносы выглядят не очень. Лучше уж в одну строку. Или выноси в переменную, если хочется перенести

.interrupt();
}
}
};
Copy link
Owner

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));
Copy link
Owner

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 {
Copy link
Owner

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");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

имена тредов обычно не задаются вручную - в этом мало смысла, пока нет тред пулов. С ними познакомишься позже (или уже познакомился)

row[i] = value.getAsInt();
}
};
}
Copy link
Owner

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем? В каком случае может вывалиться интерраптед ниже по стеку?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

как будто join закомментил, а здесь не подчистил

}
}
};
}
Copy link
Owner

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());
Copy link
Owner

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

что с идентификаторами доступа?


return result;
}
}
Copy link
Owner

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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно упростить, засунув условие if в условие цикла

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants