Week1 -Threading Flashcards

1
Q

Multi threading

A

two or more part that can run concurrently. They can run at the same time, while making optimal use of available resources, such as the CPU.

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

Thread Life Cycle

A

The life cycle of a variety of stages: Born, Started, Run, Die.

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

Thread States

A

The thread states are

  • New
  • Runnable
  • Block
  • Wait
  • Timed_Waiting
  • Terminate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

New

A

Is the born thread state. It stays in this state until the program starts the thread.

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

Runnable

A

The thread has been started and is executing a task.

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

Block

A

when a thread try’s to acquire an object lock, but said lock is being used by another thread. It will remain in this state until the other thread has finished an releases the lock

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

Wait

A

a thread goes to wait state once its calls the wait() method on an object. It will need to wait indefinitely for another thread to perform a particular task.

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

Timed_Waiting

A

thread is waiting for another thread to perform a particular action for a specific amount of time. The thread transitions back to Runnable after interval time has expired or when the event its waiting for occurs.

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

Terminate

A

thread completes execution. terminated.

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

Thread Priorities

A

helps the operation system to determine the order in which threads are scheduled. Thread priority does not guarantee the order in which the threads will execute. and are very platform dependent.

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

Thread Priority range

A

MIN_PRIORITY - CONSTANT 1
MAX_PRIORITY CONSTANT 10.
By default every thread is given a priority of NORM_PRIORITY CONSTANT 5.

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

Joining Threads

A

Allow one thread to wait for another thread to finish its execution. If t is a thread object whose threads are currently executing. Then t.join() will make sure that the t is terminated, before next instructions are executed by the program. If there are multiple threads calling join() method, it mean overloading on the joins allows the programmer to specify a waiting period.

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

3 overload functions

A

join()
join(long millis)
join( long millis, int nanos)

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

join()

A

waits for the thread to die. It will put the current thread on wait until the thread in which it was called is dead. If interrupted will throw an interrupted exception.

syntax: public final void join(){
do something
}

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

join(long millis)

A

waits at the most millis millisecond for the thread to die.

syntax: public final synchronized void join (long millis){
do something
}

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

join( long millis, int nanos)

A

waits for the millis millisecond plus nanos nanosecond for the thread to die.

syntax: public final void join(long millis, int nanos){
do something
}

17
Q

How many thread are creating when you start a java program?

A

Three thread are creating

  • Main
  • Garbage Collector
  • Thread Scheduler
18
Q

Garbage Collector

A

The garbage collector intermittently removed de-referenced objects from memory. We can not force garbage collection but can request for it to run.
System.gc() - gently request garbage to run.
We can however intervene with garbage collection, in order to specify behavior at the time of garbage collection. java.lang.object.finalize().
A form of Deamon Thread - we don’t interact or control these. They are low priority background processes.

19
Q

final

A

is a reserved keyword in java. It can’t be used a identifier. It can be used with methods, variables (objects) and classes.
final class- can’t be extend
final method - cant be overriden
final variable - the value of the variable can not be changed once its initialized .

20
Q

finally

A

is used within the try/catch block. It guarantees that a section of the code will be executed even if an exception is thrown.

21
Q

finalized()

A

is the method of object class. This method id called just before an object is garbage collected. finalized() method overrides the dispose system resources, performs clean up activities and minimize memory leaks.

22
Q

Thread Scheduler

A

The Thread scheduler in java is the part of the JVM that decides which thread should run.
a form of Deamon thread- we don’t interact or control these. They are low priority background processes.

23
Q

How to create a thread.

A
  • extend thread

- implement Runnable Interface

24
Q

extend thread

A

within the main args you start by calling the start() method , which is inhearited by the thread. It runs the method. extendThread.start(). A thread is already Runnable.

syntax: 
public class MyThread extends Thread{
public void  run(){
do something 
} }
25
Q

implement Runnable Interface

A
you start a Runnable  thread by creating a thread, pass the Runnable to it call start(). 
A Runnable  can subclass  something other that Thread using Runnable. Since each class can only extend  one other class. 
On the other hand anything that extend Thread is already considered a Thread . so its convenient to use sometimes
syntax:
public  class MyRunnable  implements Runnable {
public  void run(){
do something 
}}
26
Q

Thread Safe

A

one thread at a time can access if there’s an array list of synchronized elements.

27
Q

Can thread be garbage collected

A

Yes, an as long as its active it won’t get garbage collected.

28
Q

Synchronized

A

Keyword ensures only on thread can access a resource at a given time, such as methods variables objects and code blocks.
By synchronizing you can consistency but lose speed.
By allowing multiple threads to read (but not write) and synchronize writing creates a normal reading method and a synchronized writing method. Also, when a collection is thread safe its members are protected from being accessed by more than thread at a time.

29
Q

Thread Methods

A

start() - calls run
setPriority() - values from 1-10 changes the priority
getPriority - values from 1-10 gets priority
isAlive() - checks whether the thread is running
wait() - specify the amount to time to wait. Can be notified.
sleep() - will pause of a specified about of time . Can not be notified
notify() - waskes up waiting thread
notifyAll() - notifies all waiting threads
join() - waits for another thread to stop executing.Catches up with another thread.

30
Q

Deadlock

A

When multiple threads are forever blocking each other from trying to access the same resource.

31
Q

Starvation

A

when a thread is not granted CPU time because other threads grabbed it all. This is caused when high priority thread swallow up all the CPU from low priority thread.

32
Q

Saturation

A

Basically bottle necking. you are producing faster that they being consumed.

33
Q

Producers and Consumers

A

Its the producers job to generate data, put into the buffer and start again. While at the same time the consumer is consuming the data (removing it) from the buffer one piece at a time.

34
Q

Blocking Queue

A

a queue that is thread safe to put elements into and take elements from.

35
Q

What is a Thread?

A

A process or a single line of logic

36
Q

What is a subprocess

A

A child process of the main parents logic

37
Q

Do Threads really run concurrently in Java?

A

No its handled by the thread scheduler