Lecture 7&8-Threads Flashcards

1
Q

What are The Java Thread Classes and Interface?

A

The classes:

java.lang.Thread and java.lang.

ThreadGroup

The interface:

java.lang.Runnable

Two ways to create a new thread

  • Extending the Thread class
  • Implementing the Runnable interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain java.lang.Runnable?

A

public interface Runnable {
public abstract void run();
}

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

The class must define a method of no arguments called run.

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

How do you extend Java Thread Class?

A

Declare a subclass of Thread, overriding its run()

public class ExampleThread extends Thread {
 int parameter;
 ExampleThread(int para){parameter = para;}
 public void run() {
 ... // what should the thread do?
 }
 }

Allocate and start an instance of the subclass as:

ExampleThread t = new ExampleThread(10);
 t.start();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the Methods of Thread Class?

A

Main methods of the Thread class

start(): Causes this thread to begin execution; JVM will call run() of this thread

run(): Call the run method of the thread directly

sleep(long ms): Cease the execution of current thread for ms (milliseconds)

join(): Causes the current thread to wait for the thread that is currently executing to die

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

General Program Schema of Threads

A
public class T1 extends Thread {
 // attributes and constructor
 public void run() { /\* thread functionality \*/ }
 }
 ......
 public class Tn extends Thread {
 // attributes and constructor
 public void run() { /\* thread functionality \*/ }
 }
 public class MainProgram {
 public static void main(String[] args) {
 S;
 T1 t1 = new T1(); ...; Tn tn = new Tn()
 t1.start(); ...; tn.start();
 T; }
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the Semantics of start()?

A

The execution of S finishes before t1, …, tn as they have not been started yet

The effect of t1.start(), …, tn.start() is equivalent to
interleaving the atomic actions of the started threads

Execution of statement T may interleave with t1, …, tn

Execution of main() terminates only if all of the started threads terminate

If we replace t1.start() …, tn.start() with t1.run(), …,
tn.run(), the program will become a sequential program

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

What is Thread Groups?

A

In order to control or synchronise the behaviour of threads, a thread group can be used

A thread group represents a set of threads and can also include other thread groups.

A thread can access info about its own thread group

Methods of Java ThreadGroup class

  • ThreadGroup(String name): Constructs a new group
  • int activeCount(): Returns the number of active threads in this thread group

Create a thread within a thread group by calling

  • Thread(ThreadGroup group, Runnable target): Allocates a new Thread object and join it to the thread group group
How well did you know this?
1
Not at all
2
3
4
5
Perfectly