10. Threads Flashcards

1
Q

How do operations execute in a single-thread environment?

A

The operations execute one after another (another operation happen only when the previous one is finished)

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

What is a thread in java?

A
- An instance of class java.lang.Thread
        An object that lives en dies on the heap and has method and variables
  • An thread of execution
    An individual proces that has its own call stack. In java there is 1 thread per call stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

in how many threads does the main method run?

A

The main method runs in one thread, called the main thread.

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

What is the most important thing to understand about threads?

A

When it comes to thread, very little is guaranteed

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

How does a thread begin?

A

A thread in java begins as an instance of java.lang.Thread

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

Which methods of thread do you need to know?

A
  • start()
  • yield()
  • sleep()
  • run()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how does the thread of executing begin?

A

The thread of execution always begin by invoking the run() method.

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

in which 2 ways you can define and instantiate a thread?

A
  • Extend the java.lang.Thread class

- Implement the Runnable interface

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

Can you overload the run() method?

A

Keep in mind that you are free to overload the run() method in your Thread subclass. But know this: the overloaded methods will be ignored by the Thread class unless you call it yourself. The thread class expects a run() method with no arguments, and it will execute this method for you in a separate call stack after the thread has been started.

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

Is the Runnable Interface a functional interface?

A

Yes, That means we can use a lambda expression

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

What are the overloaded constructors in Thread?

A
  • Thread()
  • Thread(Runnable target)
  • Thread(Runnable target, String name)
  • Thread(String name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When is a thread considered alive?

A

To get an actual thread – a new call stack – we still have to start the thread. We do that with the start(), once the start() method is called, the thread is considered alive.

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

When is a thread considered dead?

A

A thread is considered dead

after the run() method completes.

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

What is the best way to determine if a thread has been started but has not yet completed its run() method?

A

The isAlive() method

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

What happens after you call start()?

A
  • A new thread of execution starts (with a new call stack)
  • The thread moves from the new state to the runnable state
  • When the thread gets a chance to execute its target run() method will run.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can a Thread be started twice?

A

Once a thread has been started it can never be started again. If you do, you will get a IllegalThreadException.

17
Q

What does the getId() method returns?

A

The getId() method returns a positive unique long number, and that number will be that thread’s only ID number for the thread’s entire life.

18
Q

What is the Thread Scheduler?

A

The thread scheduler is part of the JVM that decides which thread should run at any given moment and also takes threads out of the run state. Assuming a single processor machine, only one thread can actually run at a time.

19
Q

What is the Runnable pool?

A

when a thread has finished with its turn it moves to the end of the line of the
runnable pool and waits until it eventually gets to the front of the line, where it can be chosen again.

20
Q

Can we control the Thread Schedular?

A

Although we don’t control the thread scheduler, we can sometime influence it. The following
methods give us some tools for influencing the scheduler.
- public static void sleep (long millis) throws InterruptedException
- public static void yield()
- public final void join() throws InterruptedException
- public final void setPriority(int newPriority)

21
Q

Which three thread related related methods inherits every class?

A
  • public final void wait() throws InterruptedException
  • public final void notify()
  • public final void notifyAll()
22
Q

What states can a thread be in?

A

A thread can be only in one of five states:

  • New
  • Runnable
  • Running
  • Waiting/Blocked/Sleeping
  • Dead
23
Q

What does the NEW state mean?

A

this is the state the thread is in after the Thread instance has been created but the
start() method has not been invoked on the thread. It is a live Thread but not a thread of
execution. At this point thread is not alive.

24
Q

What does the Runnable state mean?

A

This is the state a thread is in when it is eligible to run but the scheduler has not selected it to be running the thread. A thread first enters the runnable state when the start() method is invoked, but can also return to runnable state after returning from a blocked/waiting/sleeping state. When the thread is in Runnable state it is considered alive.

25
Q

What does the Running state mean?

A

The big time where the action is. This is the state a thread is in when the thread
scheduler selects it from the runnable pool to be the currently executing proces.

26
Q

What does the Waiting/blocked/sleeping state mean?

A

This is the state a thread is in when it is not eligible to run. They
all have one thing common: the thread is still alive but is currently not eligible to run. So it is
not runnable but it might return to a runnable state later if a particular event occurs.

27
Q

What does the dead state mean?

A

A thread is considered dead when its run() method completes. It may still be a
available thread object, but is is no longer a separate thread of execution.

28
Q

What does the sleep() method do?

A

The sleep() method is a static method of class Thread. You use it in your code to slow a thread down by forcing it to go into a sleep mode before coming back to runnable. By invoking the static Thread.sleep() method, giving it a time in milliseconds. Notice that the sleep() can throw a checked InterruptedException, so you must acknowledge the exception with a handle or declare.

29
Q

What does the yield() method do?

A

The yield() supposed to do is make the currently running thread head back runnable to allow other threads of the same priority to get their turn. In reality, the yield() is not guaranteed to do what is claims, even if yield() does cause a thread to step out of running and back to runnable, there is no guarantee the yielding thread won’t just be chosen again over all the others.

30
Q

What does the join() method do?

A

The non-static join() method of class Thread lets one thread join onto the end of another thread. If you have a thread B that can’t do its work until another thread A has completed its work, then you want thread B to join thread A.

31
Q

How does Thread priority works?

A

A thread gets default priority that is the priority of the thread of execution that creates it. For example if you define a Thread in the main method (main thread) the Thread instance will have the same priority as the main thread because the main thread is executing the code that creates the Thread instance.

32
Q

Which three constants does the Thread class have?

A
  • Thread.MIN_PRIORITY (1)
  • Thread.NORM_PRIORITY (5)
  • Thread.MAX_PRIORITY (10)
33
Q

Beside yield(), join(), sleep() with following scenarios can cause a thread leaving the running state?

A
  • The thread’s run() method completes
  • A call to wait() on an object
  • A thread can’t acquire the lock on the object whose method code it is attempting to run
  • The thread scheduler can decide to move the current thread from running to runnable in
    order to give another thread a chance to run.
34
Q

How to protect a method so that only one thread at a time can execute it?

A

You must protect the method code so only one thread at a time can be executing that code. You do this with the synchronized keyword. With the synchronized keyword you guarantee that once a thread start the proces by invoking the method the other thread cannot enter that method until the first one completes the process by exiting the method.

35
Q

What happens if a Thread can’t get the lock?

A

If a thread tries to enter a synchronized method an the lock is already taken, the thread is said to be blocked on the object’s lock. Essentially, the thread goes into a kind of pool for that particular object and has to sit there until the lock is released and the thread can become again runnable. There might
be more threads waiting on the lock, there is no guarantee that the thread that has waited the longest will get the lock first.

36
Q

How to make a class Thread-safe?

A
To keep things simple, in order to make a class thread-safe, methods that access changeable fields need to be synchronized. Access to static field should be done using static synchronized methods and access to
non-static fields should be done using non-static synchronized methods.
37
Q

What is a DeadLock?

A

Deadlock occurs when two threads are blocked, with each waiting for the other’s lock. Neither can run until the other gives up its lock, so they will sit there forever.

38
Q

What is a LiveLock?

A

Livelock is almost as scary to a java program as deadlock. Livelock is similar to deadlock, except that the threads aren’t officially dead: they are just too busy to make progress.

39
Q

How to use wait(), notify() and notifyAll()?

A

One key point to remember about wait() and notify() is this: wait() and notify() and notifyAll() must
be called from within a synchronized context! A thread can’t invoke a wait() or notify() method on an
object unless it owns that objects lock.