Premium Partner

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
Licencing Not defined
<iframe src="" width="780" height="150" scrolling="no" frameborder="0"></iframe>

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?

  1. 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
  2. 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