Created
December 9, 2018 00:32
-
-
Save Shtaba09/7c97bdc8c4921767828dd7f564decad7 to your computer and use it in GitHub Desktop.
Вычисление факториала числа в потоках
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
package com.javarush.task.task28.task2813; | |
import java.util.concurrent.Callable; | |
public class FactorialCalculator implements Callable { | |
private final int number; | |
public FactorialCalculator(int number) { | |
this.number = number; | |
} | |
@Override | |
public Long call() { | |
try { | |
return factorial(number); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return 0L; | |
} | |
public long factorial(int number) throws InterruptedException { | |
if (number < 0) { | |
throw new IllegalArgumentException("Number must be greater than zero"); | |
} | |
long result = 1; | |
while (number > 1) { | |
Thread.sleep(1); | |
result = result * number; | |
number--; | |
} | |
return result; | |
} | |
} |
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
package com.javarush.task.task28.task2813; | |
import java.util.concurrent.*; | |
/* | |
FutureTask | |
*/ | |
public class Solution { | |
private static final ExecutorService threadpool = Executors.newFixedThreadPool(4); | |
private static final int n = 16; | |
public static void main(String args[]) throws InterruptedException, ExecutionException { | |
FactorialCalculator task = new FactorialCalculator(n); | |
System.out.println("Submitting Task ..."); | |
Future future = threadpool.submit(task); | |
System.out.println("Task was submitted successfully"); | |
while (!future.isDone()) { | |
System.out.println("Task is not completed yet...."); | |
Thread.sleep(1); | |
} | |
System.out.println("Task is completed, let's check the result"); | |
long factorial = (long) future.get(); | |
System.out.println("Factorial of " + n + " is : " + factorial); | |
threadpool.shutdown(); | |
} | |
} |
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
taskKey="com.javarush.task.task28.task2813"\n\nFutureTask | |
Класс FactorialCalculator предназначен для вычисления факториала числа переданного в его конструктор в качестве | |
параметра. К сожалению, он реализован некорректно и тебе необходимо это исправить. | |
Внимательно разбери метод main класса Solution. | |
Требования: | |
1. Класс FactorialCalculator должен поддерживать интерфейс Callable. | |
2. Метод call класса FactorialCalculator должен возвращать результат выполнения метода factorial. | |
3. Метод call класса FactorialCalculator должен быть публичным. | |
4. Поле number класса FactorialCalculator должно быть приватным. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment