java 2 3
fdsa
fdsa
-
- 1 / 496
-
Lernkarten
Is Files.walk lazy?
Yes
Why is the getSize method outsourced here?
private long getSize(Path p) {
try{return Files.size(p);} catch(IOException e) {}
return 0L;
}
public long getPathSize(Path source) throws IOException {
try(var s= Files.walk(source)) {
return s.parallel().filter(p-> !Files.isDirectory(p))
.mapToLong(this::getSize)
.sum();
}
}
Files.size() declares IOException and we'd rather not put a try/catch block inside a lambda expression
Whats the minimum depth for Files.walk to get a non-zero result?
at least 1
Does Files.walk traverse symbolic links by default?
No
How to follow symbolic links with Files.walk?
try(var s = Files.walk(source, FileVisitOption.FOLLOW_LINKS))..
What happens if Files.walk follows symbolic links and there is a link to root?
Every file in the system will be searched
What happens with Files.walk if it follows symbolic links but a Path is visited twice?
It will throw a FileSystemLoopException
Whats the syntax of Files.find?
public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) throws IOException
Does Files.find follow symbolic links by default?
No, but supports FOLLOW_LINK like walk
Whats a alternative for Files.readAllLines()?
File.lines()
What is the syntax of Files.lines()?
public static Stream<String> lines(Path path) throws IOException
Whats the definition/syntax of the interface runable?
@FunctionalInterface public interface Runnable {
void run();
}
In which thread will a runable be run=
Separate from the main application thread
How to pass information to runnables?
Implement the interface Runnable and pass information to the constructor
How to execute a Task with the Thread interface?
Define the thread with the corresponding task then start the Thread via Thread.start()
How to define the Task that a thread instance will execute?
Either:
- provide a runnable object or lambda expression to the Thread constructor
- create a class that extends Thread and overrides the run() method
Whats the difference between calling Thread.run() and Thread.start()?
Thread.run will just execute the method and doesnt start a new thread
Whats the signature of Thread.sleep?
public static void sleep(long millis) throws InterruptedException
Do we have to shutdown an ExecutorService?
Yes, if not then the application will never terminate
What happens if a new Thread is submitted to a ExecutorService while it is shutting down?
RejectedExecutionException will be thrown
How to cancel a running/upcoming task in an executorService?
List<Runnable> shutdownNow()
What is returned by ExecutorService.shutdownNow?
A List<Runnable> of tasks that were submitted but never started
Can we use try-with-resources for ExecutorServices?
No, doesnt implement AutoClosable
How to execute a Runnable task at some point in the future? with a ExecutorService? (Signature)
void execute(Runnable command)
How to execute a Runnable task at some point in the future and return a Future representing the task with a ExecutorService? (Signature)
Future<?> submit(Runnable task)
How to execute a Callable tasks at some point in the future and return a Future representing the pending result of the task with a ExecutorService? (Signature)
<T> Future<T> submit(Callable<T> task)
How to execute the given tasks and wait for all tasks to complete and return a list of Future instances in the same order they were in the original collection with a ExecutorService? (Signature)
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
How to execute the given tasks and wait for at least one to complete and return a future instance for a complete tasks and cancel any unfinished tasks with a ExecutorService? (Signature)
<T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
Whats the Signature of the Future-method for returning true if the task was completed/threw exception/cancelled?
boolean isDone()
Whats the Signature of the Future-method for returning true if the task was cancelled before it completed normally?
boolean isCancelled()
Whats the Signature of the Future-method for attempting tio cancel execution fof the task and returning true if it was successfully cancelled of false if it could not be cancelled/is complete
boolean cancel(boolean mayIntteruptIfRunning)
Whats the Signature of the Future-method for retrieving the ruslt of a task, waiting endlessy if it is not yet availiable
V get()
Whats the Signature of the Future-method for retrieving the result of a task, waiting the specified amount of time. If the result is not ready by time throw a checked TimeoutException
V get(long timeout, TimeUnit unit)
Whats the returntype of Future.get when its the return of a Runnable?
will be allways null
Whats the definition of the Callable interface?
@FunctionalInterface public interface Callable<V> {
V call() throws Exception;
}
When are Runnable and Callable interchangable?
When the lambda does not throw an exception and there is no returntype
What does the ExecutorService.awaitTermination do?
waits the specified time to complete all tasks, returning sooner if all tasks finish or an InterruptedException is detected
What is the syntax of ExecutorService.awaitTermination?
boolean awaitTermination(long timeout, TimeUnit unit)
What happens if we call awaitTermination without calling shutdown before?
The thread will wait the full timeoutvalue sent with awaitTermination
Are invokeAll and invokeAny synchronous or aysnchronous?
synchronous -> wait until the result are avaiable before return control to the enclosing programm