Java concurrency Flashcards Preview

SCJP > Java concurrency > Flashcards

Flashcards in Java concurrency Deck (33)
Loading flashcards...
1
Q

How can you create a thread?

A

You can create a thread by invoking the start() method on an object of the Thread class or you can implement the Runnable interface

2
Q

What happens if an exception gets thrown inside a synchronized block?

A

The lock will be released

3
Q

Why cant you declare constructors synchronized?

A

The jvm ensures that only one thread can invoke a constructor call(for a specific constructor) at a given point in time

4
Q

When does a deadlock arises?

A

When locking threads result in a situation where they cannot proceed and thus wait indefinitely for others to terminate

5
Q

What is the purpose of CyclicBarrier?

A

Wait for a number of threads wait until they are synchronized at one point, and then another thread can run

6
Q

What are the three main differences between ArrayList and CopyOnWriteArrayList?

A
  1. ArrayList is not thread-safe but CopyOnWirteArrayList is thread safe
  2. Methods in ArrayList such as remove(), add() and set() methods can throw java.util.ConcurrentModificationException is another thread modifies the ArrayList when one thread is acessing it. However, it is safe to perform these operations from multiple threads in CopyOnWriteArrayList
  3. You can get an iterator by calling the Iterator() method on a List object. If you call remove() method when the underlying container is modified, you may get a ConcurrentModificationException. However, you cannot call the remove() method on an Iterator of a CopyOnWriteList: It always throws the UnsupportedOperationException
7
Q

What is Executor?

A

is an interface that declared only one method: void execute(Runnable)

8
Q

What is Callable?

A

Callable is an interface that declares only one method: call(). Its full signature is V call() throws Exception. It represents a task that needs to be completed by a thread. Once the task completes, it returns a value. If the call() method cannot execute or fails, it throws an Exception

9
Q

What is a thread pool?

A

Is a collection of threads that can execute tasks.

10
Q

How can you create a Thread pool?

A

You can create a thread pool using the Executors utility class. This class provides methods to get instances of threads pools, thread factories, and so on

11
Q

What is ExecutorService?

A

the ExecutorService interface extends the Executor interfaces and provides services such as termination of threads and production of Future objects. Some tasks may take considerable execution time to complete, so when you submit a task to the executor service, you get a Future object

12
Q

What is Future class?

A

Future represents objects that contain a value that is returned by a thread in the future(ie. returns the value once the hread terminates in the “future”). You can use the isDone() method in the Future class to check if the task is complete and then use the get() method to fetch the task result. If you call the get() method directly while the task is not complete, the method blocks until it completes and returns the value once available

13
Q

What is the use of the Fork/Join framework?

A

It helps simplify writing parallelized code. The framework is an implementation of the ExecutorService interface and provides an easy to use concurrent platform in order to exploit multiple processors. Drive and conquer problems. This approach is suitable for tasks that can be divided recursively and computed on a smaller scalled; the computed results are then combined.

14
Q

What is forking?

A

dividing smaller tasks

15
Q

What is joining?

A

merging the results from the smaller tasks

16
Q

What is the ForkJoinPool?

A

It is a thread pool for running fork/join tasks and it executes an instance of ForkJoinTask. It executes tasks and manages their lifecycle

17
Q

What is a ForkJoinTask{V}?

A

is a lightweight thread-like entity representing a task that defines methods such as fork() and join()

18
Q

What is a RecursiveTask{V}?

A

is a task that can run in a ForkJoinPool; the compute() method returns a value of type V. It inherits from ForkJoinTask

19
Q

what is RecursiveAction?

A

is a task that can run in a ForkJoinPool; its compute() method performs the actual computation steps in the task. It is similiar to RecursiveTask, but does not return a value

  • Abstract class that requires us to implement compute() : void
20
Q

What is the difference between RecursiveTask and RecursiveAction

A

RecursiveTask returns a value and RecursiveAction doesnt

21
Q

What are parallel streams?

A

Parallel streams split the elements into multiple chunks, process each chunk with different threads, and (if necessary) combine the results from those threads to evaluate the final result

22
Q

What is Runnable?

- Give some examples on how to create it using lambda

A
  • is a functional interface that takes no arguments and returns no data
  • () -> sout(“Hello”)
  • () -> { int i =10; i++;}
  • () -> { return;}
  • () -> {}
23
Q

True or false

with a single thread executor, results are guaranteed to be executed in the order in which they are added to the ExecutorService

A

true

24
Q

What is the difference between execute() and sumit()?

A

Submit returns a Future object that can be used to determine of the task is complete

25
Q

What are the ExecuteService methods?

A
  • void execute(Runnable command)
  • 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 “ “)
26
Q

What is the difference between scheduleAtFixedRate() and scheduleAtFixedDelay()

A

scheduleAtFixedRate creates a new task and submits it to the executor regardless of whether or not the previous task is finished

27
Q

service.scheduleAtFixedRate(command 5, 1, TimeUnit.Minute)

What does this method do?

A

runs every minute following an initial five-minute delay

28
Q

True or false

Neither scheduleAtFixedRate and scheduleAtFixedDelay take callable object as input params

A

true

29
Q

What are the Executors methods?

A
  • newSingleThreadExecutor : ExecutorService
  • new SingleThreadSchedulerExecutor: ScheduledExecutorService
  • new CachedThreadPool : ExecutorService
  • newFixedThreadPool(int thread): ExecutorService
  • newScheduleThreadPool(int nThreads): ScheduledExecutorService
30
Q

What are the Atomic classes?

A
  • AtomicBoolean
  • AtomicInteger
  • AtomicIntegerArray
  • AtomicLong
  • AtomicLongArray
  • AtomicReference
  • AtomicReferenceArray
31
Q

What are the Concurrent collection classes?

A

ClassName : Java collection Interface

ConcurrentHashMap : ConcurrentMap
ConcurrentLinkedDeque: Deque
ConcurrentLinkedQueue: Queue
ConcurrentSkipListMap : ConcurrentMap/SortedMap/ NavigableMap
ConcurrentSkipListSet: SortedSet/NavigableSet
CopyOnWriteList: List
CopyOnWriteSet: Set
LinkedBlockingDeque: BlockingDeque, BlockingDeque

32
Q

True or false

Callable and Runnable dont take input params

A

True

33
Q

Given:

AtomicInteger ai = new AtomicInteger(5)

what is the difference between:

a) int x = ai.getAndIncrement()
b) int x = ai.incrementAndGet()
c) int x = ai.addAndGet(1)
d) int x = ai.getAndSet(6)

A

a) It will increase the ai to 6, but it returns the old value to x, so x = 5
b) it will increase ai to 6, and return the new value, so x=6
c) it will increase ai +1 and return the new value, so x = 6
d) it will set ai to 6, but it will return the old value, so x=5