OCP - 3

Concurrency & IO

Concurrency & IO


Set of flashcards Details

Flashcards 110
Language English
Category Computer Science
Level Other
Created / Updated 08.03.2019 / 08.01.2020
Weblink
https://card2brain.ch/box/20190308_ocp_3
Embed
<iframe src="https://card2brain.ch/box/20190308_ocp_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

Does a parallel stream always remain a prallel?

No, depending on the operation, this might change. Eg. using flatMap creates a new stream that is not parallel by default

Is the order of parallelStreams predictable?

No!

Why should side effects be avioded in parallel streams?

The order of execution is not predictable, making sideeffects likely to go wrong

Can you use any collection for parallel streams?

Yes, but the concurrent collections are preferable as for some operations, non-concurrent lists might provide unexpected resutls

 Do the operations findAny or skip work the same on parallel streams as on serial ones?

No, because they run in parallel, any item might be skipped or returned whereas in serial streams, the order is relevant.

Serial streams can be marked as unordered using the unordered() method, which can greatly improve the performance.

What requirements have to be met to make reduce return an ordered result?

  • The identity must be defined such that all elements in the stream u, combiner.apply(identity, u) is equal to u.
  • The accumulator and combiner operator must be associative and stateless
  • The combiner must be compatible with the identity

What are the requirements for parallel reduction with collect?

  • The stream is parallel
  • The parameter of the collect operation has the Collectior.Characteristics.CONCURRENT characteristic
  • Either the stream is unordered, or the collect operation has the Collectior.Characteristics.UNORDERED characteristic

What is the Cyclic Barrier class for?

It syncs n threads at a given Point (Zoo example: First all must empty the cage in parallel, then they all start at the same time to clean and sync again before letting in the Animals). The number of threds to wait for is provided over the constructor.

What method is usually used on the CyclicBarrier?

await

Can a CyclicBarrier be reused?

Yes, after the barrier is broken, the nuber of waiting threads is set back to 0 and the class can hence be reusd

What has to be considered when using CyclicBarrier with a fixed size ThreadPool?

The nuber of avaiblabe threads must be at least as large as the CyclicBarrier limit. Otherwise, the barrier can never complete

What is the idea of the Fork/Join Framework?

Divide an Conquer: DIvide a task into recursive subtasks adding concurrency with each division. A potential result is then aggergated and returned

What are the three steps of the fork/join framework?

  1. Create a ForkJoinTask
    • ForkJoinTask<?> task = new MyActionOrTask(...);
  2. Create the ForkJoinPool
    • ForkJoinPool pool = new ForkJoinPool();
  3. Start the ForkJoinTask
    • pool.invoke(action)
    • Object result = pool.invoke(task);

 

What is the difference between RecursiveAction and RecursiveTask?

  • RecursiveAction has no return type
  • RecursiveTask returns a generic type

How does the recursive case differ between RecursiveAction and RecursiveTask in the Fork/Join Framework?

  • RecursiveAction just calls invokeAll(new MyAction(...), new Action(...)) as it is "fire and forget"
  • RecursiveTask is more complex:
    • RecursiveTask<Double> otherTask = new MyTask(...);
      otherTask.fork();
      return new MyTask(...).compute() + otherTask.join()

Does the order in which fork(), join() and compute() are called in a recursive task matter?

Yes, very much!

fork() starts the execution of a task while join() await's its completeness. If you call join() before calling compute(), the tasks are not executed in parallel!

What does the fork method in the fork/join Framework do?

It submits a task to the pool

What does the join method in the fork/join Framework do?

casues the current thread to wait for the result of a subtask

What does the compute method in the fork/join Framework do?

It computes the task. When called in the compute method itself, it is calculated within the current thread

How can you pass data into the compute function of a RecursiveTask/RecursiveAction?

Via the constructor as the compute method does not take any arguments

Whe does a deadlock occur?

A deadloc occurs when two or more threads are blocked forever

Whe does a starvation occur?

When a single thread is perpetually denied access to a shared resource

When does a livelock occur?

A livelock is a form of sttarvation where tow or more threads are active but conceptually blocked forever

When dor ace conditions occur

When two or more threads execute at the same time, resulting in an upexpected outcome

Do ExecutorService.submit and ExecutorService.execute throw a checked exception

No

What does the java.io.File class represent

A file or directory on the local disk

How can you query the file system separator?

  • System.getProperty("file.separator")
  • java.io.File.separator

What does the lengh() method on a File object return

The number of bytes in the file, might be more than the actual usage for performance reasons

What does the getParent() method on a File isntance do?

It returns the abstract pathname (as String!) of the Parent of this File or Directory or null if it does not name a parent direcotry

What are java streams conceptually about?

STreams should be conceptually thought of as a long, nearly never-ending "stream of water" with data presented one "wave" at a time.

What is the difference between Readers/Writers and Input-/OutputStreams?

  • For Inputting and outputting all types of binary data, InputStreams and OutputStreams are used
  • For Inputting and outputting character and String data, Readers and Writers are used
    • Basically, Readers and Writers are convenience for Characters. They are conceptually the same

What is the dfference between a low level stream and a high level stream?

  • Low-Level streams connect directly with the source of data, such a file, an array or a string. Eg. a FileInputStream
    • Define basic methods such as read() and close()
  • High-Level streams are built on to of other streams usingwrapping. Eg. a BufferedReader / BufferedInputStream
    • Provide additional convenience methods such as readLine()

Why should BufferedReaders/Writes/Streams be used?

They usually improve the performance singificantly.

What is special about InputStream, OutputStream, Reader and Writer?

They are all abstract classes and the basis for all specialized Readers,Writers,In- and Outputstreams

Can Writers and OutputStreams be mixed?

No, when wrapping a stream you can mix and match only types that inherit from the same abstract parent stream

How can you convert an INputStream into a Reader

By wrapping it with a InputStreamReader

What are PrintStream and PrintWriter for?

They write formatted representations of Java objects to a binary stream

Can streams be used with try-with-resources

Yes, and you should do it!

What is the flush() method for?

For performance, not every write operation is written to the disk/network immediately. Using flush, the OS is requested to actually do it, which might take a while. Call it when it should go, but don't do it to often (expensive)

What is the mark method on an InputStream for? Does it take a parameter?

  • It marks a position in the stream to jump back later using reset()
  • Not all streams support this: Check with markSupported() first!
  • The method takes a readlimit in bytes. After that many bytes, the mark position gets invalidated.
    • Calling reset afterwards might throw an IOException