Which of the following methods will start this thread public class?

Runnable Interface

Package: java.lang

The Runnable interface describes a class whose instances can be run as a thread. The interface itself is very simple, describing only one method (run) that is called automatically by Java when the thread is started.

The Runnable interface is usually used in conjunction with the Thread class. For more information, see Thread Class.

Methods

Method

Explanation

void run()

Called when the thread is started. Place the code that you want the thread to execute inside this method.

To use the Runnable interface to create and start a thread, you have to do the following:

1. Create a class that implements Runnable .

2. Provide a run method in the Runnable class.

3. Create an instance of the Thread class and pass your Runnable object to its constructor as a parameter.

A Thread object is created that can run your Runnable class.

4. Call the Thread object’s start method.

The run method of your Runnable object is called and executes in a separate thread.

Here’s an example of a class that implements Runnable:

public class RunnableClass implements Runnable1

{

public void run()

{

// code to execute when thread is run

// goes here

}

}

Here’s an example that instantiates a RunnableClass object, and then creates a Thread object to run the RunnableClass object:

RunnableClass rc = new RunnableClass();

Thread t = new Thread(rc);

t.start();

Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at . Indicate the book, edition, and question number in your email. Thanks!

Chapter 32 Check Point Questions

Section 32.2

▼32.2.1

(1) Why is multithreading needed? (2) How can multiple threads run simultaneously in a single-processor system?

▼32.2.2

What is a runnable object? What is a thread?

Section 32.3

▼32.3.1

How do you define a task class? How do you create a thread for a task?

▼32.3.2

What would happen if you replace the start() method with the run() method in lines 14-16 in Listing 32.1?

▼32.3.3

What is wrong in the following two programs? Correct the errors.

(a) public class Test implements Runnable { public static void main(String[] args) { new Test(); } public Test() { Test task = new Test(); new Thread(task).start(); } public void run() { System.out.println("test"); } } (b) public class Test implements Runnable { public static void main(String[] args) { new Test(); } public Test() { Thread t = new Thread(this); t.start(); t.start(); } public void run() { System.out.println("test"); } }

Section 32.4

▼32.4.1

Which of the following methods are instance methods in java.lang.Thread? Which method may throw an InterruptedException? Which of them are deprecated in Java?
run, start, stop, suspend, resume, sleep, interrupt, yield, join

▼32.4.2

If a loop contains a method that throws an InterruptedException, why should the loop be placed inside a try-catch block?

▼32.4.3

How do you set a priority for a thread? What is the default priority?

Section 32.5

▼32.5.1

What causes the text to flash?

▼32.5.2

Is an instance of FlashText a runnable object?

▼32.5.3

What is the purpose of using Platform.runLater?

▼32.5.4

Can you replace the code in lines 27-32 using the following code?

Platform.runLater(e -> lblText.setText(text));

▼32.5.5

What happens if line 34 (Thread.sleep(200)) is not used?

▼32.5.6

There is an issue in Listing 16.9 ListViewDemo. If you press the CTRL key and select Canada, Demark, and China in this order, you will get an ArrayIndexOutBoundsException. What is the reason for this error and how do you fix it? (Thanks to Henri Heimonen of Finland for contributing this question 04/30/2016).

Section 32.6

▼32.6.1

What are the benefits of using a thread pool?

▼32.6.2

How do you create a thread pool with three fixed threads? How do you submit a task to a thread pool? How do you know that all the tasks are finished?

Section 32.7

▼32.7.1

Give some examples of possible resource corruption when running multiple threads. How do you synchronize conflicting threads?

▼32.7.2

Suppose you place the statement in line 26 of Listing 32.4 inside a synchronized block to avoid race conditions, as follows:

synchronized (this) { account.deposit(1); }

Will it work?

Section 32.8

▼32.8.1

How do you create a lock object? How do you acquire a lock and release a lock?

Section 32.9

▼32.9.1

How do you create a condition on a lock? What are the await(), signal(), and signalAll() methods for?

▼32.9.2

What would happen if the while loop in line 58 of Listing 32.6 was changed to an if statement?

if (balance < amount)

▼32.9.3

Why does the following class have a syntax error?

public class Test implements Runnable { public static void main(String[] args) { new Test(); } public Test() throws InterruptedException { Thread thread = new Thread(this); thread.sleep(1000); } public synchronized void run() { } }

▼32.9.4

What is a possible cause for IllegalMonitorStateException?

▼32.9.5

Can the wait(), notify(), and notifyAll() be invoked from any object? What is the purpose of these methods?

▼32.9.6

What is wrong in the following code?

synchronized (object1) { try { while (!condition) object2.wait(); } catch (InterruptedException ex) { } }

Section 32.10

▼32.10.1

Can the read and write methods in the Buffer class be executed concurrently?

▼32.10.2

When invoking the read method, what happens if the queue is empty?

▼32.10.3

When invoking the write method, what happens if the queue is full?

Section 32.11

▼32.11.1

What is a blocking queue? What blocking queues are supported in Java?

▼32.11.2

What method do you use to add an element to an ArrayBlockingQueue? What happens if the queue is full?

▼32.11.3

What method do you use to retrieve an element from an ArrayBlockingQueue? What happens if the queue is empty?

Section 32.12

▼32.12.1

What are the similarities and differences between a lock and a semaphore?

▼32.12.2

How do you create a semaphore that allows three concurrent threads? How do you acquire a semaphore? How do you release a semaphore?

Section 32.13

▼32.13.1

What is a deadlock? How can you avoid deadlock?

Section 32.14

▼32.14.1

What is a thread state? Describe the states for a thread.

Section 32.15

▼32.15.1

What is a synchronized collection? Is ArrayList synchronized? How do you make it synchronized?

▼32.15.2

Explain why an iterator is fail-fast.

Section 32.16

▼32.16.1

How do you define a ForkJoinTask? What are the differences between RecursiveAction and RecursiveTask?

▼32.16.2

How do you tell the system to execute a task?

▼32.16.3

What method can you use to test if a task has been completed?

▼32.16.4

How do you create a ForkJoinPool? How do you place a task into a ForkJoinPool?

Which of the following methods will start this thread public class method implements runnable?

1. Which of these method is used to implement Runnable interface? Explanation: To implement Runnable interface, a class needs only to implement a single method called run().

Which of the following methods will start the thread?

Introduction.

Which of the following methods will start the thread public class Mythread?

B is incorrect for the same reason; you can't pass a class or interface name to any constructor. D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.

Which of the following methods will start this thread public class my thread implements runnable public void run ()?

start(); [D]. Explanation: Because the class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started.