Threads Flashcards Preview

JAVA OCA7 > Threads > Flashcards

Flashcards in Threads Deck (96)
Loading flashcards...
1

What does thread mean in Java?

Means two different things:
* An instance of class java.lang.Thread
* A thread of execution

2

How threads work in terms of memory?

Each thread has a separate call stack. main( ) has the main thread.

3

How does different JVM's work for giving turns to threads?

Different JVM's work in different ways. One might use round-robin fashion the other might not give turn to others until one finishes.

4

How does a thread begin in Java?

It begins as an instance of java.lang.Thread

5

Explain run( ) method

The job that needs to be done in the separate thread goes into run. It is either in java.lang.Thread or in the implementation of Runnable interface.

6

How does extending Thread class works?

You have to override run method i.e. public void run()
You can overload it but it will be ignored and even if you call it yourself it won't have a separate call stack. But it is not a good design choice. And on a separate note - Thread itself extends Runnable.

7

How does implementing Runnable works?

You implement Runnable and override run i.e. public void run()

8

How do we instantiate a thread that extends Thread?

It is simple. Just;
MyThread t = new MyThread();

9

How do we instantiate a thread that implements Runnable?

This time we need worker and job classes. Thread is the worker that runs the thread-specific code and Runnable is the job class that runs the job that needs to be done.
First instantiate;
MyRunnable r = new MyRunnable();
Next get instance of Thread and give your runnable;
Thread t = new Thread(r);
The Runnable you pass to the Thread constructor is called the target or the target Runnable.

10

What happens when no args are passed to Thread()?

Thread calls its own run( ) method.

11

What happens when same target is passed to multiple Threads?

You can pass a single Runnable instance to multiple Thread objects so that the same Runnable becomes the target of multiple threads.Giving the same target to multiple threads means that several threads of execution will be running the very same job (and that the same job will be done multiple times)

12

What are some thread constructors?

* Thread()
* Thread(Runnable target)
* Thread(Runnable target, String name)
* Thread(String name)

13

How do we start a thread?

t.start();
before start is called the thread is said to be in new state. We start a Thread not a Runnable.

14

What happens when start is called?

* 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.

15

Does this code start a new call stack?
Thread t = new Thread();
t.run();

No. Calling run in Runnable does not start a new call stack.

16

How can we print name of a Thread?

We can call getName() method of the thread that executes the that Runnable's run() method. But we do not have a reference to current thread, so that's how it is done:
Thread.currentThread().getName()

17

Does a thread still has a name even if we do not name it?

Yes. For main thread its "main" and for other threads its like "Thread-0"

18

What happens when a thread completes its run() method?

The stack for that thread dissolves and the thread is considered dead. It's still a thread object and methods on that object can be invoked as any other Java object. But it's not a thread of execution any more. And we can not call start() on that object again.

19

What happens when we try to start a Thread second time?

It can not be started and IllegalThreadStateException is thrown.

20

What does getId() method returns when invoked on a thread?

It returns the id of thread. It's positive, unique, long and a thread's only id for the thread's entire life.

21

What is thread scheduler?

It's part of JVM and decides which thread is going to actually run from all of the eligible ones i.e. in runnable state. And also decides which will be out of the run state.

22

In which order threads are chosen by thread scheduler?

The order in which runnable threads are chosen to run is not guaranteed.

23

What are thread states?

- New
- Runnable
- Running
- Waiting/blocked/sleeping
- Dead

24

Define "New" thread state

The state after Thread is instantiated. It's a thread object but not a thread of execution yet. start() method has not been invoked yet. It is not alive.

25

Define "Runnable" thread state

The thread is eligible to run but not selected by scheduler yet. It can be after start() is called or returned back from a blocked, waiting or sleeping state. It's considered alive.

26

Define "Running" thread state

When a runnable is selected by scheduler from thread pool. That's the only way to be in running state.

27

Define "Waiting/blocked/sleeping" thread state

The thread is alive but not eligible to run in this state. It can blocked waiting for a resource or it might be sleeping by the code.

28

Define "Dead" thread state

When a thread completes the thread's state is dead and can not be brought back to life. If start() is invoked on this thread. runtime exception is thrown.

29

Define "Sleeping" state details

sleep() is a static method and can be called from code
Thread.sleep(). sleep can throw checked InterruptedException

30

How does timer is Thread.sleep() works?

It makes the current thread sleep "at least" for the specified time. It returns back to runnable after time finishes but it might not be returned back to running by scheduler. It's a static method and one thread can not put another one into sleeping.