Chapter 9: Threads Flashcards

1
Q

What are the important thread methods?

A
  • start (start a new thread)
  • run (descirbes the thread job)
  • sleep (pauses the thread)
  • yield (return to the runnable pool, so other threads can run)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two ways to define a threads behaviour?

A
Extend the Thread class
Implement the Runnable interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Considering MyThread contains the Threads behaviour (and extends Thread), what is the result of the following?

Thread t = new Thread(new MyThread());
t.start();
A

Compiles and runs fine.

Thread implements the Runnable interface by default, so a sub-class of Thread can be passed as the runnable to the new Thread object.

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

What are the four constructors for the Thread class?

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

How to get the currents Thread name?

A

Thread t = Thread.currentThread();

t.getName();

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

What is the result of the following:

Thread t1 = new Thread();
Thread t2 = new Thread();
t1.start();
t2.start();
t1.start();
A

Throws an IllegalThreadStateException

A Thread object can only be started once.

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

Describe the Thread lifecycle

A

Waiting/Blocked

New -> Runnable Running -> Dead

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

When can a thread go into the runnable state?

A
  • When the start method has been called on the Thread object.
  • When returing from a blocked, waiting, sleeping state.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When can a thread go into the running state?

A

When the thread scheduler choses the thread from the runnable thread as the currently executing proces.

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

What is the result?

public static void main(String... args) {
System.out.println("start");
Thread.sleep(5*60*1000);
System.out.println("end");
}
A

Compile error.

Missing try-catch or throws clause.

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

What is the result?

public static void main(String... args) {
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.sleep(1000);
t1.start();
t2.start();
t2.sleep(1000);
}
A

The current thread (not t1 or t2) will sleep for 1000 ms starts both the threads and then will sleep again for 1000 ms.

sleep() is a static method on the Thread class. So it will let the currently executing thread sleep.

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

How to set a Threads priority?

A

thread.setPriority(5);

For min, max and norm values use:
Thread.MIN_PRIORITY;
Thread.MAX_PRIORITY;
Thread.NORM_PRIORITY;

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

What does Thread.yield(); do?

A

Returnt he current thread to the runnable pool to give other threads with equal priorities a chance to run. No guarantees, the Thread scheduler may choose the same thread as the new running thread.

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

What does thread.join(); do?

A

The current thread stops executing and waits till the “joining” thread is finished.

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

What does the synchronized keyword do?

A
  • Can be used on methods and code-blocks
  • Only one thread can run a synchronized method from a particular instance at the same time.
  • When a thread has a lock (meaning it is inside a synchronized method or block) No other thread can lock the object (enter a synchronized method or block).
  • When a thread goes to sleep, it keeps the lock on a object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False:

Static methods can’t be synchronized.

A

False

Static methods can be synchronized, the lock will be put on the Class instance (f.e. MyClass.class).

17
Q

What does the object.wait() method do?

A

Puts the current thread on the waiting list for that instance. Once the object calls notify() one random waiting thread will continue to execute. Once the object calls notifyAll() all waiting threads will continue to execute.

18
Q

What is the result of the following code?

synchronized(anotherObject) {
anotherObject.wait();
}

A

wait call should be enclosed in both a synchronized block (or method) and a try-catch clause.

19
Q
What is the result of the following code?
synchronized(anotherObject) {
    try {
    anotherObject2.wait();
    } catch(InterruptedException e) {}
}
A

wait call can only be called on the object that is locked by the current thread.