ISYS 316 Advanced Java Programming Chapter 30 Multi-threading and Parallel Programming

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following statements are true?

A. A condition is associated with a lock. B. To invoke methods on a condition, the lock must be obtained first. C. Once you invoke the await method on a condition, the lock is automatically released. Once the condition is right, the thread re-acquires the lock and continues executing.

Which of the following statements are true?

A. A synchronized instance method acquires a lock on the object for which the method was invoked. B. A synchronized instance method acquires a lock on the class of the object for which the method was invoked. C. A synchronized statement can be used to acquire a lock on any object, not just this object, when executing a block of the code in a method. D. A synchronized statement is placed inside a synchronized block.

You can create a blocking queue using _____________.

A. ArrayBlockingQueue B. LinkedBlockingQueue C. PriorityBlockingQueue

Which of the following statements are true?

A. Semaphores can be used to restrict the number of threads that access a shared resource. B. Before accessing the resource, a thread must acquire a permit from the semaphore. C. After finishing with the resource, the thread must return the permit back to the semaphore. D. You can create a Semaphore with a specified number of permits.

Which of the following statements are true?

A. You can use a timer or a thread to control animation. B. A timer is a source component that fires an ActionEvent at a 'fixed rate.' C. The timer and event-handling run on the same event dispatcher thread. If it takes a long time to handle the event, the actual delay time between two events will be longer than the requested delay time. D. In general, threads are more reliable and responsive than timers.

Which of the following statements are true?

A. a blocking queue has a capacity. B. A blocking queue causes a thread to block when you try to add an element to a full queue. C. A blocking queue causes a thread to block when you try to remove an element from an empty queue. D. The BlockingQueue interface is the base interface for all concrete blocking queue classes. E. The BlockingQueue interface provides the synchronized put and take methods for adding an element to the head of the queue and for removing an element from the tail of the queue,

You can use the _________ method to temporarily release time for other threads.

A. sleep(long milliseconds) B. yield()

Which of the following statements are true?

An exception would occur if no thread is waiting on the object when the notify() method is invoked on the object.

When you run the following program, what will happen? public class Test extends Thread { public static void main(String[] args) { Test t = new Test(); t.start(); t.start(); } public void run() { System.out.println("test"); } }

An illegal java.lang.IllegalThreadStateException may be thrown because you just started thread and thread might have not yet finished before you start it again.

Which of the following are correct statements to create a Lock?

B. Lock lock = new ReentrantLock(); C. Lock lock = new ReentrantLock(true); D. Lock lock = new ReentrantLock(false);

Which of the following statements are true?

B. The javax.swing.SwingUtilities.invokeAndWait method runs the code in the event dispatcher thread. C. The javax.swing.SwingUtilities.invokeLater method runs the code in the event dispatcher thread and doesn't return until the event-dispatching thread has executed the specified code. D. GUI event handling is executed in the event dispatcher thread.

Which of the following methods in the Thread class are deprecated?

B. stop(); C. resume(); D. suspend();

Which of the following statements are defined in the Object class?

B. wait() C. notify() D. notifyAll() E. toString()

How do you create a condition on a lock?

Condition condition = lock.newCondition();

How do you create a cached thread pool?

ExecutorService executor = Executors.newCachedThreadPool();

Suppose there are three Runnable tasks, task1, task2, task3. How do you run them in a thread pool with 2 fixed threads?

ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(task1); executor.execute(task2); executor.execute(task3);

Which of the following are correct statements to create a Lock so the longest-wait thread will obtain the lock first?

Lock lock = new ReentrantLock(true);

Why does the following class have a syntax error? import java.applet.*; public class Test extends Applet implements Runnable { public void init() throws InterruptedException { Thread t = new Thread(this); t.sleep(1000); } public synchronized void run() { } }

The init() method is defined in the Applet class, and it is overridden incorrectly because it cannot claim exceptions in the subclass.

Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Test t = new Test(); } public Test() { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } }

The program compiles and runs and displays test.

Analyze the following code: public abstract class Test implements Runnable { public void doSomething() { }; }

The program compiles fine.

Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Test t = new Test(); t.start(); } public void run() { } }

The program does not compile because the start() method is not defined in the Test class.

Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } }

The program does not compile because this cannot be referenced in a static method.

Analyze the following code. // Test.java: Define threads using the Thread class import java.util.*; public class Test { private Stack stack = new Stack(); private int i = 0; /** Main method */ public static void main(String[] args) { new Test(); } public Test() { // Start threads new Producer().start(); new Consumer().start(); } class Producer extends Thread { public void run() { while (true) { System.out.println("Producer: put " + i); stack.push(new Integer(i++)); synchronized (stack) { notifyAll(); } } } } class Consumer extends Thread { public void run() { while (true) { synchronized (stack) { try { while (stack.isEmpty()) stack.wait(); System.out.println("Consumer: get " + stack.pop()); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } } }

The program will throw an exception because the notifyAll() method in the Producer class is not invoked from the stack object.

What is the output of the following code? // Test.java: Define threads using the Thread class public class Test { /** Main method */ public static void main(String[] args) { new Test(); } public Test() { // Create threads PrintChar printA = new PrintChar('a', 4); PrintChar printB = new PrintChar('b', 4); // Start threads printA.run(); printB.run(); } class PrintChar implements Runnable { private char charToPrint; // The character to print private int times; // The times to repeat /** Construct a thread with specified character and number of times to print the character */ public PrintChar(char c, int t) { charToPrint = c; times = t; } /** Override the run() method to tell the system what the thread will do */ public void run() { for (int i = 0; i < times; i++) System.out.print(charToPrint); } } }

aaaaabbbbb

Which of the following methods can be used to obtain a permit from a Semaphore s?

acquire()

Which method on a condition should you invoke to causes the current thread to wait until the condition is signaled?

condition.await();

Which method on a condition should you invoke to wake all waiting threads?

condition.signalAll();

You should always invoke the unlock method in the finally clause.

false

You can use the ________ method to force one thread to wait for another thread to finish.

join()

Which of the following expressions must be true if you create a thread using Thread = new Thread(object)?

object instanceof Runnable

Which of the following methods can be used to return a permit to a Semaphore s?

send()

Which of the following method is a static in java.lang.Thread?

sleep(long)

Which of the following methods in Thread throws InterruptedException?

sleep(long)

The keyword to synchronize methods in Java is __________.

synchronized

Given the following code, which set of code can be used to replace the comment so that the program displays time to the console every second? import java.applet.*; import java.util.*; public class Test extends Applet implements Runnable { public void init() { Thread t = new Thread(this); t.start(); } public void run() { for(; ;) { //display time every second System.out.println(new Date().toString()); } } }

try { Thread.sleep(1000); } catch(InterruptedException e) { }


Conjuntos de estudio relacionados

Chapter 1: Foundations of Structural Kinesiology

View Set

PSYC 2013, FINAL EXAM, Chen, Auburn

View Set

APUSH - Period 4, 75 College Board Questions

View Set

Science 908 lifepac test study guide

View Set

CISCO CCNA I - Module 11 - IPv4 Addressing (Test Notes)

View Set

Alzheimer's disease, dementia and cognition

View Set