Multithreading in Java Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are threads?

A

Threads are lightweight processes that facilitate in allowing multiple activities in a single process. A thread is a series of executed statements. Each thread has its own program counter, stack and local variables but it shares memory, files and per-process state.

In multithreaded environment, programs make maximum use of CPU so that the idle time can be kept to minimum.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the common thread states?

A

here are several thread states, A thread can be in any one of the state at a particular point of time. It can be running state. It can be ready to run state as soon as it gets CPU time. A running thread can be suspended. A suspended thread can be resumed. A thread can be blocked when waiting for a resource. At any time a thread can be terminated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Purpose of multithreading?

A

The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program called a thread. Each thread has a separate path of its execution. So this way a single program can perform two or more tasks simultaneously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create threads in Java?

A

A thread can be created in two ways: 1)By extending Thread class 2) By implementing Runnable interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Methods to manage threads?

A
  • getName(): It is used for Obtaining a thread’s name
  • getPriority(): Obtain a thread’s priority
  • isAlive(): Determine if a thread is still running
  • join(): Wait for a thread to terminate
  • run(): Entry point for the thread
  • sleep(): suspend a thread for a period of time
  • start(): start a thread by calling its run() method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe process by creating a thread thru implementing the Runnable interface.

A
  • Create a class that implements the Runnable interface. We must need to give the definition of run() method.
  • This run method is the entry point for the thread and thread will be alive till run method finishes its execution.
  • Once the thread is created it will start running when start() method gets called. Basically start() method calls run() method implicitly.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe process by creating a thread thru extending Thread class.

A
  • This is the second way of creating a thread. Here we need to create a new class that extends the Thread class.
  • The class should override the run() method which is the entry point for the new thread as described above.
  • Call start() method to start the execution of a thread.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are thread priorities?

A
  • Thread priorities are the integers which decide how one thread should be treated with respect to the others.
  • Thread priority decides when to switch from one running thread to another, process is called context switching
  • A thread can voluntarily release control and the highest priority thread that is ready to run is given the CPU.
  • A thread can be preempted by a higher priority thread no matter what the lower priority thread is doing. Whenever a higher priority thread wants to run it does.
  • To set the priority of the thread setPriority() method is used which is a method of the class Thread Class.
    In place of defining the priority in integers, we can use MIN_PRIORITY, NORM_PRIORITY or MAX_PRIORITY.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is synchronization?

A

When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization. To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why use threads?

A
  • To perform asynchronous or background processing
  • Increases the responsiveness of GUI applications
  • Take advantage of multiprocessor systems
  • Simplify program logic when there are multiple independent entities
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When does a thread end?

A

A Thread ends due to the following reasons:

  • The thread ends when it comes when the run() method finishes its execution.
  • When the thread throws an Exception or Error that is not being caught in the program.
  • Java program completes or ends.
  • Another thread calls stop() methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s deadlock?

A

Whenever there is multiple processes contending for exclusive access to multiple locks, there is the possibility of deadlock. A set of processes or threads is said to be deadlocked when each is waiting for an action that only one of the others can perform.

In order to avoid deadlock, one should ensure that when you acquire multiple locks, you always acquire the locks in the same order in all threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are some common guidelines for synchronization?

A
  1. Keep blocks short. Synchronized blocks should be short — as short as possible while still protecting the integrity of related data operations.
  2. Don’t block. Don’t ever call a method that might block, such as InputStream.read(), inside a synchronized block or method.
  3. Don’t invoke methods on other objects while holding a lock. This may sound extreme, but it eliminates the most common source of deadlock.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly