OCP - 3
Concurrency & IO
Concurrency & IO
Kartei Details
Karten | 110 |
---|---|
Sprache | English |
Kategorie | Informatik |
Stufe | Andere |
Erstellt / Aktualisiert | 08.03.2019 / 08.01.2020 |
Weblink |
https://card2brain.ch/box/20190308_ocp_3
|
Einbinden |
<iframe src="https://card2brain.ch/box/20190308_ocp_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
Lernkarteien erstellen oder kopieren
Mit einem Upgrade kannst du unlimitiert Lernkarteien erstellen oder kopieren und viele Zusatzfunktionen mehr nutzen.
Melde dich an, um alle Karten zu sehen.
Do you have to manually cast the results of the readObject() method?
Yes, the return type is of type Object
How can you determine EOF when using a ObjectInputStream?
Catch the EOFException!
is it safe to use inputsream.available() to check if a stream is EOF?
NO! Use the specific strategies for each kind of stream
How is a serialized object created?
- Java calls the first non-arg constructor for the first nonserializable parent class, skipping the constructors of all serialized classes in between!
- Static variables are ignored
- default intializers (eg private String myField = "123" and {}) are ignored
What are the PrintStream and PrintWriter classes for?
They offer a high-level interface to write formatted representation of Java objects to a text-based outputs stream
What is a common use of PrintStream?
The System.err and Sytstem.out fields
What is special about the print, println, printf and format methods found on the PrintStream/PrintWriter classes?
They do not throw an exception! The checkError() method can be used for that instead
What line separator does the println method od PrintWriter/PrintStream use?
It depends on the os. You can access the value using System.getProperty("line.separator")
What is the difference between format and printf on PrintWriter/PrintStream
They are identical, printf calls format behind the scenes
What is the Console class for?
It is convenience for the Readers/Writers System.in, System.out and System.err
What is special about the Console class?
It is a Singleton that is accessed using the System.console() method
What common methods does the Console class provide?
- reader()
- writer()
- format() / printf()
- Locale override is not supported! Use writer.format instead!
- flush()
- readLine()
- readPassword()
When should you use the Console.flush() method?
Before using readLine and readPassword to ensure only user input is read
Why does Console.readPassword return a byte array?
If a string were returned, the passwourd might be available in the String pool long after the user entered it. By using a byte array, the password can be wiped as soon as it is not required anymore using Arrays.fill(passwordArray, 'x')
Does the method Writer.append exist
Yes
What is the relation between a Task and a Thread?
A task is a single unit of work performed by a thread. A thread can implement multiple independent tasks but only one task at a time. In Java, such a Task is usually defined as an implementation of the Runnable Interface.
What is the signature of the Runnable interface?
One single abstract method: void run();
How can you start a new Tread?
- Configure the Tread class by either passing a Runnable Object in the Thread constructor or by extending the Thread class and overriding the run method
- Calling the start Method on the Thread instance
What is always important in concurrent applications?
Never make assumptions on the order of execution of Threads and tasks. This is the responsibility of the operating system and you cannot assume anything!
What is more prefereable for implementing a Task, extending Thread or implementing Runnable?
Almost always the Runnable class. THread might be useful for complex rules upon which multiple tasks rely.
The runnable class is preferable becauase of single inheritance and the use of the Concurrency API
What is Thread.sleep(int) for and when should it be used?
While waiting for the result of another thread, it might be necessary to poll for the Results of the other Thread. Using just a while loop is not only wasted energy but also a potential infinite loop. By using Thread.sleep, the current thread is paused for the defined amount of time, allowing other threads to do their work.
What has to be minded when using the Thread.sleep method?
It declares the checked InterruptedException which must be handeled
What is the ExecutorService Class for?
It simplifies the execution of concurrent tasks by abstracting common error-prone operations
What is important when using the ExecutorService class?
It is important to call the shutdown() method. A thread executor creates a non-demon thread on the first task that is executed, so failing to call shutdown will result in you application never terminating.
What assumption can be made, when using the a single-threaded executor service?
All Tasks that are queued are executed in the order in which they were added to the executor service
What is the ExecutorService lifecycle?
- After construction, an ExecutorService is active, meaning it accepts new tasks and executes tasks.
- After calling shutdown(), an ExecutorService is shutting down, meaning it rejects new tasks and executes tasks.
- After all Tasks are finished, an ExecutorService is terminated, meaning it rejects new tasks and no tasks are running
What does the ExecutorService.shutdownNow() method do?
- It attempts to stop all running tasks (NO Guarantees)
- Typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
- This method does not wait for actively executing tasks to terminate. Use awaitTermination to do that.
- No new tasks are started and all queued, unexecuted Tasks are returned
What common ExecutorService methods for launching Tasks exist?
- Future<?> submit(Runnable task)
- <T> Future<T> submit(Callable<T> task)
- <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
What is the difference between execute and submit on the ServiceExecutor class?
- execute is fire and forget - has no return type. It accepts a Runnable Instance, meaning that the implementation cannot throw a checked exception
- submit returns a Future to access it's result. It accepts a Calleable Instance, meaning that the implementation can throw a checked exception
How does the ExecutorService.invokeAll method beahve?
- It executes the given task synchronously!
- Future.isDone() is true for each element of the returned list.
- The result is a list of all task results as a Collection of futures
- Same order as the original collection
-
- 1 / 110
-