-
Notifications
You must be signed in to change notification settings - Fork 0
ThreadPool task #1
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: master
Are you sure you want to change the base?
Conversation
DanAnastasyev
left a comment
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.
У тебя куча бинарных файлов в репозиторий попало. Почисть их и настрой gitignore
ThreadPool/src/CustomQueue.java
Outdated
| @@ -0,0 +1,4 @@ | |||
| public interface CustomQueue<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.
А зачем ты этот интерфейс создал, если потом не используешь никак?
ThreadPool/src/MyQueue.java
Outdated
| import java.util.LinkedList; | ||
| import java.util.Queue; | ||
|
|
||
| public class MyQueue<T> implements CustomQueue<T> { |
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.
MyQueue, конечно, очень говорящее название...
ThreadPool/src/MyQueue.java
Outdated
| import java.util.LinkedList; | ||
| import java.util.Queue; | ||
|
|
||
| public class MyQueue<T> implements CustomQueue<T> { |
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.
Вообще говоря, в java уже есть класс очереди, который умеет блокировать поток при отсутствии элементов
ThreadPool/src/MyQueue.java
Outdated
| @Override | ||
| public synchronized void enqueue(T task) { | ||
| queue.add(task); | ||
| // Wake up anyone waiting on the queue to put some item. |
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.
Неловко получилось, мда.
Давайте попробуем вычислить новизну кода. По моим подсчётам она (без учёта теста) составляет 11 строк (возможно, меньше). Остальные изменения являются добавленными пустыми строчками, переименованными переменными, либо переписанными комментариями.
Хватает ли 11 строк кода на то, чтобы зачесть задачу? Определённо, нет.
А за повторную попытку списывания я бы предложил запретить сдавать эту задачу. Что, кстати, довольно грустно с учётом того, что её решение является необходимым условием получения зачёта.
ThreadPool/src/ThreadPool.java
Outdated
|
|
||
| private final int threadPoolCapacity; | ||
| private MyQueue<Callable> myQueue = new MyQueue(); | ||
| private ArrayList<Thread> threads = new ArrayList(); |
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 CustomQueue<Callable> myQueue = new MyQueue<>();
private List<Thread> threads = new ArrayList<>();Тогда бы появился глубинный смысл у создания интерфейса очереди
ThreadPool/src/ThreadPool.java
Outdated
| private ArrayList<Thread> threads = new ArrayList(); | ||
|
|
||
| private ThreadPool(int capacity) { | ||
| this.threadPoolCapacity = capacity; |
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.
Зачем this?
ThreadPool/src/Task.java
Outdated
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class Task implements Callable { |
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.
Кстати, в задании было про Runnable и реализацию определенного интерфейса
ThreadPool/src/ThreadPool.java
Outdated
| for (int i = 0; i < threadPoolCapacity; ++i) { | ||
| threads.get(i).interrupt(); | ||
| } | ||
| System.out.println("Good bye!"); |
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.
Не надо в System.out в таком классе писать. Мало ли как он использоваться будет
ThreadPool/src/Task.java
Outdated
| System.out.println("Начало задачи"); | ||
| TimeUnit.SECONDS.sleep(time); | ||
| System.out.println("Конец задачи"); | ||
| return null; |
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.
Зачем?
ThreadPool/src/Task.java
Outdated
| } | ||
|
|
||
| @Override | ||
| public Object call() 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.
Правильнее было бы не throws InterruptedException, а обернуть sleep в try-catch
ThreadPool/src/MyQueue.java
Outdated
| @Override | ||
| public synchronized void enqueue(T task) { | ||
| queue.add(task); | ||
| // Wake up anyone waiting on the queue to put some item. |
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.
Хороший же коммент был, зачем стирать
ThreadPool/src/ThreadPool.java
Outdated
| int num = Integer.parseInt(i.toString());//args[0]); | ||
| int i = 3; | ||
| int num = i;//args[0]; | ||
| ThreadPool threadPool = new ThreadPool(num); |
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.
Будь смелее, пиши сразу ThreadPool threadPool = new ThreadPool(3);!
ThreadPool/src/ThreadPool.java
Outdated
| myQueue.enqueue(r); | ||
| } | ||
|
|
||
| public static void main(String... args) throws InterruptedException, ExecutionException { |
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 void main(String[] args). Не очень понимаю, откуда ты String... выкопал
No description provided.