Groovy, running on the Java Virtual Machine (JVM), supports concurrency and parallelism through a variety of mechanisms. This quiz will focus on key topics such as multithreading basics, GPars (Groovy Parallel Systems), asynchronous programming, and the Actor Model in Groovy, helping you understand Groovy’s approach to concurrency and parallelism.
MCQs: Concurrency and Parallelism in Groovy
Multithreading Basics
How do you create a new thread in Groovy?
a) Thread.start()
b) new Thread().start()
c) Thread.new()
d) new Thread().run()
Which of the following Groovy methods is used to pause the current thread for a specified period?
a) Thread.sleep(time)
b) sleepThread(time)
c) pause(time)
d) Thread.wait(time)
What is the default priority of a thread in Groovy?
a) 1
b) 5
c) 10
d) 100
Which method is used to start a thread in Groovy after creating it?
a) startThread()
b) begin()
c) start()
d) runThread()
In Groovy, how can you access the current thread from within a program?
a) Thread.current()
b) Thread.currentThread()
c) Thread.getCurrent()
d) Thread.getThread()
GPars (Groovy Parallel Systems)
What is GPars in Groovy?
a) A database framework
b) A concurrency and parallelism library
c) A file I/O library
d) A graphics rendering library
Which of the following methods is used to execute tasks in parallel using GPars?
a) GPars.execute()
b) GPars.parallel()
c) GPars.fork()
d) GPars.run()
Which feature of GPars allows you to process tasks asynchronously using worker threads?
a) ForkJoin
b) Actor System
c) Dataflow
d) Parallel Collections
What does GPars.withPool() do in Groovy?
a) It pools database connections.
b) It manages threads and task execution.
c) It monitors memory usage.
d) It starts a new actor system.
How can you run multiple tasks in parallel in GPars and wait for them to complete?
a) GPars.await()
b) GPars.waitFor()
c) GPars.gather()
d) GPars.all()
Asynchronous Programming
Which method in Groovy is used to execute code asynchronously?
a) async()
b) executeAsync()
c) task()
d) Thread.start()
In Groovy, what class provides an easy way to execute asynchronous code?
a) Future
b) AsyncTask
c) AsyncExecutor
d) ThreadPool
What is the benefit of asynchronous programming in Groovy?
a) It improves user interface performance.
b) It simplifies code structure.
c) It speeds up database access.
d) It prevents memory leaks.
Which method is used in Groovy to wait for an asynchronous task to complete and get the result?
a) awaitResult()
b) getResult()
c) join()
d) get()
Which Groovy feature allows non-blocking code execution to perform tasks in the background?
a) Actor Model
b) Future
c) GPars
d) Promises
Actor Model in Groovy
What is the Actor Model in Groovy used for?
a) Managing databases
b) Performing parallel processing with multiple actors
c) Managing threads and synchronization
d) Executing asynchronous tasks with threads
Which Groovy library provides support for the Actor Model?
a) GPars
b) Groovy Actors
c) Groovy Futures
d) Groovy Async
How do you define an actor in Groovy using the Actor library?
a) actor { ... }
b) def actor = new Actor()
c) actor create()
d) new Actor { ... }
What method is used to send a message to an actor in Groovy?
a) actor.sendMessage()
b) actor.send()
c) actor.sendMsg()
d) actor.post()
How does the Actor Model help in handling concurrency in Groovy?
a) By managing data and threads together
b) By using an event loop to process messages
c) By creating multiple processes
d) By using memory pools for actors
In Groovy, what is the role of an actor mailbox?
a) It stores messages to be processed by the actor.
b) It handles system messages.
c) It stores actor states.
d) It manages thread synchronization.
Which of the following describes the message-passing model in the Actor Model?
a) Actors communicate directly with each other.
b) Actors share memory space to communicate.
c) Actors send messages through a global queue.
d) Actors send messages via a callback system.
What is the purpose of the react method in Groovy actors?
a) It reacts to incoming messages asynchronously.
b) It synchronizes the actor with other threads.
c) It runs background tasks.
d) It stops the actor from processing messages.
How can an actor in Groovy stop processing messages and terminate?
a) actor.stop()
b) actor.exit()
c) actor.shutdown()
d) actor.terminate()
Which statement is true about the Actor Model in Groovy?
a) It provides automatic thread management.
b) It uses shared memory for communication.
c) It performs parallel computation using multiple threads.
d) It enables actors to directly modify the state of other actors.
Which class in GPars is used to create an actor in Groovy?
a) GroovyActor
b) GParsActor
c) Actor
d) ParallelActor
How do actors in Groovy ensure that they don’t block each other?
a) Through message passing
b) Through shared memory
c) By using semaphores
d) By using global synchronization
In Groovy, which method is used to ensure an actor is ready to receive messages?
a) actor.ready()
b) actor.listen()
c) actor.react()
d) actor.await()
What happens when an actor in Groovy receives a message?
a) The actor processes the message and replies.
b) The actor queues the message for later processing.
c) The actor sends a response to the sender.
d) The actor terminates automatically after receiving the message.
Which of the following is a key advantage of using the Actor Model in Groovy for concurrency?
a) It simplifies the management of multiple threads.
b) It allows direct memory access for faster computation.
c) It eliminates the need for network communication.
d) It prevents deadlock by avoiding shared state.
Answer Key
Qno
Answer
1
b) new Thread().start()
2
a) Thread.sleep(time)
3
b) 5
4
c) start()
5
b) Thread.currentThread()
6
b) A concurrency and parallelism library
7
c) GPars.fork()
8
d) Parallel Collections
9
b) It manages threads and task execution.
10
c) GPars.gather()
11
a) async()
12
a) Future
13
a) It improves user interface performance.
14
d) get()
15
c) GPars
16
b) Performing parallel processing with multiple actors
17
b) Groovy Actors
18
a) actor { ... }
19
b) actor.send()
20
b) By using an event loop to process messages
21
a) It stores messages to be processed by the actor.
22
a) Actors communicate directly with each other.
23
a) It reacts to incoming messages asynchronously.
24
a) actor.stop()
25
a) It provides automatic thread management.
26
c) Actor
27
a) Through message passing
28
c) actor.react()
29
a) The actor processes the message and replies.
30
a) It simplifies the management of multiple threads.