Concurrency in Java

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/56

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

57 Terms

1
New cards

What is concurrency in Java?

Concurrency is the ability of a program to execute multiple tasks simultaneously or out of order to maximize efficiency.

2
New cards

What is multithreading in Java?

A programming technique where multiple threads run in parallel within a single process.

3
New cards

What is a thread?

A lightweight subprocess that can execute independently within a program.

4
New cards

Which class represents a thread in Java?

java.lang.Thread

5
New cards

How can you create a thread in Java?

By extending the Thread class or implementing the Runnable interface.

6
New cards

What is the difference between Runnable and Thread?

Runnable is a functional interface; Thread is a class that can be subclassed.

7
New cards

What method starts a thread?

start()

8
New cards

What method contains the thread's task?

run()

9
New cards

What is the difference between start() and run()?

start() creates a new thread; run() executes in the current thread.

10
New cards

What is the lifecycle of a thread?

New → Runnable → Running → Blocked/Waiting → Terminated

11
New cards

What is thread synchronization?

A technique to control access to shared resources to avoid race conditions.

12
New cards

Which keyword is used for synchronization in Java?

synchronized

13
New cards

What is a race condition?

A situation where multiple threads access shared data and produce unpredictable results.

14
New cards

What is a critical section?

A part of code that must be executed by only one thread at a time.

15
New cards

How do you synchronize a method?

By adding the synchronized keyword to its declaration.

16
New cards

How do you synchronize a block of code?

Using synchronized(obj) to lock a specific object.

17
New cards

What is intrinsic lock or monitor lock?

A lock associated with every object that is used to implement synchronization.

18
New cards

What is deadlock in Java?

A situation where two or more threads are waiting for each other to release resources, causing all to block indefinitely.

19
New cards

What is livelock?

A situation where threads keep changing state in response to each other without making progress.

20
New cards

What is thread starvation?

When a thread is unable to gain regular access to resources due to high-priority threads.

21
New cards

What is wait() in Java?

A method used to make a thread wait until it is notified.

22
New cards

What is notify() in Java?

A method used to wake up a single thread waiting on the object's monitor.

23
New cards

What is notifyAll()?

It wakes up all threads waiting on the object's monitor.

24
New cards

What is the difference between wait() and sleep()?

wait() releases the monitor lock; sleep() does not.

25
New cards

Which class provides high-level concurrency utilities?

java.util.concurrent

26
New cards

What is an ExecutorService?

A high-level replacement for managing threads manually.

27
New cards

Which method is used to submit tasks to ExecutorService?

submit() or execute()

28
New cards

What is Future in Java?

A result placeholder for asynchronous computation.

29
New cards

How do you retrieve a result from Future?

Using future.get()

30
New cards

What is Callable in Java?

A task that returns a result and may throw an exception.

31
New cards

How is Callable different from Runnable?

Callable returns a value; Runnable does not.

32
New cards

What is ThreadPoolExecutor?

A class for managing a pool of reusable threads.

33
New cards

What is ScheduledExecutorService?

An executor that can run tasks periodically or after a delay.

34
New cards

What are Atomic classes?

Classes like AtomicInteger, AtomicBoolean that provide lock-free thread-safe operations.

35
New cards

What is ReentrantLock?

An advanced locking mechanism that offers more flexibility than synchronized.

36
New cards

What is the advantage of ReentrantLock?

Ability to try lock, interrupt lock acquisition, and fair ordering.

37
New cards

What is ReadWriteLock?

A lock that allows multiple readers or one writer at a time.

38
New cards

What is a Semaphore?

A concurrency utility that controls access to a resource with a set number of permits.

39
New cards

What is a CountDownLatch?

A synchronization aid that allows threads to wait until a set of operations completes.

40
New cards

What is a CyclicBarrier?

A barrier that lets threads wait for each other before continuing.

41
New cards

What is a Phaser in Java?

A flexible synchronization barrier that supports dynamic registration of parties.

42
New cards

What is ForkJoinPool?

A framework for parallelism using divide-and-conquer tasks, introduced in Java 7.

43
New cards

What is the common pool in ForkJoinPool?

A shared pool used by the parallelStream() and CompletableFuture.

44
New cards

What is the difference between parallelism and concurrency?

Concurrency is about structure and interleaving; parallelism is about executing multiple threads simultaneously.

45
New cards

What is volatile keyword in Java?

Ensures visibility of changes to variables across threads.

46
New cards

How does volatile differ from synchronized?

volatile ensures visibility, not atomicity; synchronized provides both.

47
New cards

What is ThreadLocal in Java?

A variable that is accessible only by the thread that created it.

48
New cards

What is a daemon thread?

A background thread that terminates when all user threads are finished.

49
New cards

How do you create a daemon thread?

By calling setDaemon(true) before start().

50
New cards

What is the default priority of a thread?

Thread.NORM_PRIORITY (value 5)

51
New cards

Can thread priorities be changed?

Yes, using setPriority(), but behavior is OS-dependent.

52
New cards

What is a ConcurrentHashMap?

A thread-safe implementation of a hash map for high-concurrency scenarios.

53
New cards

What is the role of BlockingQueue?

A thread-safe queue that waits for availability when inserting or removing elements.

54
New cards

Give an example of BlockingQueue implementation.

ArrayBlockingQueue, LinkedBlockingQueue

55
New cards

What are parallel streams in Java 8?

Streams that divide elements and process them in multiple threads using the ForkJoinPool.

56
New cards

Should shared mutable state be used in parallel streams?

No, it can lead to race conditions.

57
New cards

What are some best practices for concurrency in Java?

Minimize shared state, prefer higher-level concurrency APIs, use immutability when possible.